12
votes

Is it possible to deploy to localstack with the aws-cdk? Thought about switching from serverless to the cdk, but could not find any ohter local testing option except aws SAM..

2
Not quite clear what you mean by "switching from serverless to CDK". The CDK is for stack development, essentially on top of CFN. You can develop serverless stacks in CDK (i.e. deploy Lambda etc). - Marakai
@Marakai Probably means the framework. - Miles Elam
@MilesElam Yeah I see OP accepted the answer given, so it became clear then. - Marakai
Why switch? We often recommend using Serverless Framework and AWS CDK together. Serverless’s strength is working with Lambda and API Gateway. So a good pattern here is to use CDK for the rest of your infrastructure, and Serverless for your Lambda functions. –– We created a tool to do exactly this - Serverless Stack Toolkit (SST), which allows you to combine CDK and Serverless Framework - github.com/serverless-stack/serverless-stack –– So you can do sls deploy --stage dev and sst deploy --stage dev. - Frank

2 Answers

16
votes

I'm working through the same challenges right now. The way I'm getting by is taking the synthesized CloudFormation template from CDK and loading that into my LocalStack CloudFormation:

$ cdk synth my-stack > my-stack.template.json
$ awslocal cloudformation create-stack --stack-name my-stack --template-body file://./my-stack.template.json

This is working for me with very simple stacks that do not require artifacts/assets. What I'm still working on here is transposing what CDK does internally with S3 bucket based assets as CloudFormation parameters (e.g. Lambda functions, etc.).

Hopefully this gets you down the road a bit. I feel it's the first part of the solution, given what CDK can do today.

1
votes

I would point you towards the aws-cdk-local package. This allows you to deploy CDK IaC to your localstack. See aws-cdk-local link