admin管理员组

文章数量:1287088

Could someone please help out on how they are approaching local dev with CDK and SAM? I'm trying to assist our devs to improve the local development experience and iteration of Lambda code.

Previously we used Serverless framework v3 with the serverless-offline plugin etc. It was a decent experience for local dev, including hot reload, straight forward to test etc.

We are now considering a move to CDK + SAM for local dev (since sls framework is dying) but it seems the ability to develop against localhost with hot reload is barely supported.

For example here is the commands I understand one to run:

cdk synth --yaml > cdk.out/LambdaStack.template.yaml
sam local start-api --template cdk.out/LambdaStack.template.yaml  --warm-containers EAGER

Once running, SAM will recognise and hot reload when the actual LambdaStack.template.yaml file changes ... But not when a dev makes a change to Typescript Lambda code locally within their IDE. Not great.

Instead, a dev is forced to run cdk synth each time any local Lambda code changes which can take upwards of 5-10 seconds regenerate the template yaml.

Could someone please enlighten me here since I must be missing something obvious. There is surely a better pipeline for quick local iteration right? It feels we've gone backwards a decade or two

I'm concerned the only alternative is cdk watch. But that's hardly satisfactory for a feedback loop involving deployment of lambda code directly to a staging environment etc. This is a step back to early-mid 2000s reminiscent of yolo changing PHP or Ruby scripts directly on a web server via SFTP!

Could someone please help out on how they are approaching local dev with CDK and SAM? I'm trying to assist our devs to improve the local development experience and iteration of Lambda code.

Previously we used Serverless framework v3 with the serverless-offline plugin etc. It was a decent experience for local dev, including hot reload, straight forward to test etc.

We are now considering a move to CDK + SAM for local dev (since sls framework is dying) but it seems the ability to develop against localhost with hot reload is barely supported.

For example here is the commands I understand one to run:

cdk synth --yaml > cdk.out/LambdaStack.template.yaml
sam local start-api --template cdk.out/LambdaStack.template.yaml  --warm-containers EAGER

Once running, SAM will recognise and hot reload when the actual LambdaStack.template.yaml file changes ... But not when a dev makes a change to Typescript Lambda code locally within their IDE. Not great.

Instead, a dev is forced to run cdk synth each time any local Lambda code changes which can take upwards of 5-10 seconds regenerate the template yaml.

Could someone please enlighten me here since I must be missing something obvious. There is surely a better pipeline for quick local iteration right? It feels we've gone backwards a decade or two

I'm concerned the only alternative is cdk watch. But that's hardly satisfactory for a feedback loop involving deployment of lambda code directly to a staging environment etc. This is a step back to early-mid 2000s reminiscent of yolo changing PHP or Ruby scripts directly on a web server via SFTP!

Share Improve this question asked Feb 24 at 6:21 wired00wired00 14.5k8 gold badges76 silver badges77 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I mostly familiar with the general concepts and not with the details of Typescript and your use case, but I hope it helps.

This depends basically on how your Lambda function source code is being built. If you're using CDK's NodeJsFunction, then your typescript code is transpiled during cdk synth and turned into a single deployment package (zip file).

This zip file is normally uploaded to S3 to be able to deploy it later. If you check your template file, you would see that the AWS::Lambda::Function resource's Code points to an S3 location. However, there's a local version of that built code that CDK keeps, and it's visible in the generated template under your function's Metadata section. There should be something like:

Metadata:
  aws:cdk:path: ..
  aws:asset:path: asset:<hash>

That aws:asset:path should be the name of a directory inside cdk.out where the built code actually is, and that's the code that SAM CLI actually uses to do the local invoke or start-api.

So you need to update the files inside that directory for your local function to get updated. I don't think CDK has a build watch that can rebuild your code when it changes, but maybe you can figure some other way to send your changes to inside that directory faster than synth

本文标签: aws cloudformationLocal dev quick iteration for serverless lambda using CDK and SAMStack Overflow