question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I think it could be useful to have a basic canonical example of how to use SAM from CDK (See also: #703, https://github.com/awslabs/aws-sam-cli/issues/663)

If it would be useful, I could probably knock together what I figured out here into a PR to examples? If so… what would be the best naming/etc for it? Potentially could choose one of the simpler examples from the following and translate it to CDK:

I found the following, so should be doable:

But when I was exploring the docs, it didn’t look like I could do much without dropping into the cloudformation namespace (which is probably fine, as this is ‘lower level/direct mapping’):

So I decided to try and make a basic function example from that, just to see how it would work. It took me a little while to figure the intricacies of the types used and their interrelations:

import sam = require('@aws-cdk/aws-serverless');
const helloWorld = new sam.cloudformation.FunctionResource(this, "HelloWorld", {
            functionName: "HelloWorld",
            description: "Greeting the world",
            codeUri: "./target",
            handler: "hello-world",
            runtime: lambda.Runtime.Go1x.name,
            tracing: "active",
            events: {
                CatchAll: {
                    type: "Api",
                    properties: {
                        path: "/{proxy+}",
                        method: "ANY",
                        // restApiId: ""
                    }
                }
            }
        });

Which synthesises to:

Transform: 'AWS::Serverless-2016-10-31'
Resources:
    HelloWorld:
        Type: 'AWS::Serverless::Function'
        Properties:
            CodeUri: ./target
            Handler: hello-world
            Runtime: go1.x
            Description: 'Greeting the world'
            Events:
                CatchAll:
                    Properties:
                        Method: ANY
                        Path: '/{proxy+}'
                    Type: Api
            FunctionName: HelloWorld
            Tracing: active
    CDKMetadata:
        Type: 'AWS::CDK::Metadata'
        Properties:
            Modules: '@aws-cdk/assets=0.9.0,@aws-cdk/aws-cloudwatch=0.9.0,@aws-cdk/aws-codepipeline-api=0.9.0,@aws-cdk/aws-ec2=0.9.0,@aws-cdk/aws-elasticloadbalancing=0.9.0,@aws-cdk/aws-events=0.9.0,@aws-cdk/aws-iam=0.9.0,@aws-cdk/aws-kms=0.9.0,@aws-cdk/aws-lambda=0.9.0,@aws-cdk/aws-s3=0.9.0,@aws-cdk/aws-s3-notifications=0.9.0,@aws-cdk/aws-serverless=0.9.0,@aws-cdk/aws-sqs=0.9.0,@aws-cdk/cdk=0.9.0,@aws-cdk/cx-api=0.9.0,js-base64=2.4.5,poc-aws-cdk=0.1.0'

CDK References:

SAM References:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:30 (16 by maintainers)

github_iconTop GitHub Comments

28reactions
eladbcommented, Sep 16, 2018

Eventually, you should be able to achieve the same results you currently achieve with the SAM resources by simply using the AWS Construct Library.

The CDK already has support for Lambda via the @aws-cdk/aws-lambda module, and we are working on an API Gateway module (see #665) which will allow you to implement the above example like this:

const handler = new lambda.Function(this, 'HelloWorldHandler', {
  description: 'Greeting the world',
  code: apigw.Lambda.directory('./target'),
  handler: 'hello-world',
  runtime: lambda.Runtime.Go1x,
  tracing: lambda.Tracing.Active
});

const api = new apigw.RestApi(this, 'HelloWorldApi');
api.root.onMethod('ANY', handler);

Furthermore, we are looking at ways to allow people to use SAM CLI with CDK Apps, in order to enable local debugging of SAM apps written using the CDK (whether or not they use the SAM resources).

19reactions
jfusscommented, Aug 30, 2019

@cpmech Circling back here. We released v0.21.0 earlier this week. This has support for reading and understanding AWS::ApiGateway::Methods, AWS::ApiGateway::RestApi, AWS::ApiGateway::Resource, and AWS::ApiGateway::Stage. This will allow you to do a cdk synth > template.yaml and then sam local start-api to test your functions.

@eladb Might be able to close this out. Could be useful to expand the docs though. I don’t have direct bandwidth to tackle a doc update right now though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: Deploying a Hello World application
In this guide, you download, build, and deploy a sample Hello World application using AWS SAM. You then test the application in the...
Read more >
serverless-projects/aws-sam-examples - GitHub
AWS SAM Examples. To work with the example applications in this repository, first ensure that you've installed and configured the requirements listed below....
Read more >
How to Build a Serverless Application using AWS SAM
SAM uses the AWS Command Line Interface (CLI) behind the scenes to deploy the project. If you haven't used AWS's CLI before to...
Read more >
Build a Serverless Application using AWS SAM
Step 1: Initialize Serverless App with SAM · 1 - Hello World Example · 2 - Step Functions Sample App (Stock Trader) ·...
Read more >
SAM template walkthrough - AWS Workshop Studio
x and timeout as 30 seconds, you can do that for all the Lambda functions in the Globals section. In this example, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found