-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example: AWS SAM #716
Comments
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). |
Expected that would be the case based on the wording of how it was described. Until then though, it will be nice to be able to leverage the existing abstractions SAM offers. The CLI tie in will be nice. |
Is the @aws-cdk/aws-sam library what you were looking for or did you look for some richer APIs on top of it? |
As per my example above (probably edited original issue as I worked it all out), it is, but this issue was more about making a canonical example of using it so that others don't have to dig through everything to figure it out. |
One issue I saw when I attempted to deploy my SAM resources via CDK:
I ran
If I use an asset to upload the code, and pass the s3 information:
It succeeds, but now I can't use |
My current workaround:
I run |
Hey, thanks for this! We are looking at streamlining this experience. Stay tuned :-) |
What @eladb said:
That would be super useful. I don't see much purpose on using SAM when using CDK, but currently the biggest pain point by doing so is not having support for SAM CLI (or not having other similar local development toolkit); So basically there isn't a sane local development setup. Of course one can always build something custom around it, but reinventing the wheel isn't really what I want 😅 … and also as we are using a compiled language (Golang) to write our functions that also makes writing "local invoke wrappers" harder than with - say- NodeJS. We're currently developing with AppSync, Golang Lambdas and CDK. I know, I know CDK docs say not to use it for production, but it's just exactly the tooling we've been looking for 🙂 and we do vendor in the synthetized CloudFormation templates to Github (just to be on the safe side). |
Chucking this here as it may be useful to others looking to combine these tools. The following works just fine: const fn = new lambda.Function(this, "Func", {
..
});
new cdk.Output(this, "FunctionName", { value: fn.functionName }); Then on deploy:
And in your terminal:
|
@mipearson thanks for this. Useful for us to learn about this use case. Copying @sam-goodwin |
Let me know if you have some info that we want to put into the guide. |
Not sure if there's a better spot for this comment, but with respect to SAM, I think the biggest feature is Lambda safe deployments, and the orchestration it does with CodeDeploy applications and canary cutover. @eladb in your discussions have there been talk of native support of this in CDK? This is currently the only reason I see to output SAM templates and run a |
The @aws-cdk/aws-codedeploy package supports AWS Lambda traffic shifting. Can you take a look there and let us know if this is what you were looking for? We should document this in the @aws-cdk/aws-lambda package |
In my opinion
|
Local invokes and Lambda logs are supported by SAM CLI. We are working on documenting this and @jfuss can provide some details. |
To reword my question - cdk doesn't run I found #1938 which seems to have my answer. I'll track that one. Thanks! |
@cortopy You can use SAM CLI with CDK today. When you author a template with CDK, you can 'sam local [invoke|start-lambda]' or
Some notes/current gotchas:
|
@jfuss that indeed works and have been using it. The problem is that I don't feel like the cdk can replace any of my usual serverless development flows at this moment.
So I have to rather do I suspect (pure guess at this stage) this is because the output of Also, as a side note, the |
FYI: The CDK Developer Guide currently has a short description of using the SAM CLI to test Lambda functions at https://docs.aws.amazon.com/cdk/latest/guide/tools.html#sam. |
Hey @cortopy. If you run The CDK is using a SAM feature for this that is explained here: https://github.com/awslabs/aws-sam-cli/blob/master/designs/resource_metadata_overriding.md |
Hi, I'm just wondering if there is an estimate for when |
@cpmech CDK currently only supports generating the raw API Gateway resources. So the two paths to the support would be:
I know there is an issue for the first, but I wouldn't expect this to cover all customers, given the current state. On the second, we are currently under going some work on support API Gateway Resources. Design PR. We plan on supporting RestApi with swagger to start, while we try to understand the full need for support the wider API Gateway Resources (such as |
@cpmech Circling back here. We released v0.21.0 earlier this week. This has support for reading and understanding @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. |
Fantastic! Thanks a lot! |
An issue feels like a suboptimal way of addressing this, I think this is a great case for having a SAM example in the examples repo. 😸 |
A `--no-staging` syth sets the asset path to the local Lambda deployment packager, (rather than CDK's asset bundle), which local Lambda honours. See: aws/aws-cdk#716 (comment)
@jfuss Thank you very much for the SAM Local support of the additional |
Nice thread so... sorry for asking this here but couldn't find any reliable place to write my question. Why would someone use CDK to generate SAM? Thank you :) |
A little rough... but I ended up skipping SAM all-together and wrote a little invoke script like so: process.env = {
...process.env,
JWT_TOKEN_SECRET_NAME: 'blah',
JWT_EXPIRATION_TIME: '5m'
}
import { handler } from './src/app'
import { APIGatewayProxyEvent } from 'aws-lambda'
const events: { [key: string]: APIGatewayProxyEvent } = {
createSession: {
requestContext: {
httpMethod: 'POST',
resourcePath: '/sessions'
},
body: JSON.stringify({
username: 'user',
password: 'pass'
})
} as APIGatewayProxyEvent
}
;(async () => {
console.log(await handler(events[process.argv[2]]))
})()
"scripts": {
"local": "npx ts-node local.ts $2"
} executed with: $ yarn local createSession |
The setup I use for developing lambda functions locally is: https://gist.github.com/ranguard/3befa8344678c9fe90f34a81e3a5ca8d this is just a small step on from the documented approach. All other resources (Dyn/S3 etc) are deployed and referenced from there. |
Closing this issue since we don't have anything concrete to do here at this time. In general, better interplay between the CDK and SAM would be to an advantage. However, we don't have anything to offer just yet. |
I think it could be useful to have a basic canonical example of how to use SAM from CDK (See also: #703, aws/aws-sam-cli#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:
Which synthesises to:
CDK References:
SAM References:
The text was updated successfully, but these errors were encountered: