How to deploy SAM stack with localstack?
Asked Answered
D

3

9

I've written a SAM stack and I can build, package and deploy it on AWS. I can also use start-local or invoke to test. Now I need to test to other AWS resources. I've added those resources to my SAM template. It works well on AWS but I'm searching for a way to deploy my SAM stack in localstack (local dynamodb e.g.). Now I have to create the resources with the CLI and after that I can deploy my SAM stack (only lambda, API GW in this case). How can I do this?

Danby answered 5/2, 2020 at 14:3 Comment(6)
What resources do you need specifically? And how are you trying to access them? If it's through code can you publish the relevant code snippet?Periodate
I'm currently using a SAM template with lambda, dyamodb, s3, dynamodb streams and firehose. It works on AWS, but I can not deploy the SAM template with localstack. (github.com/localstack/localstack/issues/632). I'm now searching for a different way to do this. I can deploy a part with normal cloudformation and a part with SAM but then I need to import and export resources and that will not work (export in localstack in docker and import in SAM)Danby
I think this is kind of a misuse of both tools. You're supposed to run the SAM separately, sam local start-api ...<options>. And then in your lambda code refer to your localstack resources by their local address, i.e. DynamoDB at localhost:4569 . You shouldn't try to deploy the SAM application to a local stack, that's not what it's for.Periodate
Basically you can spin up your SAM with the sam-cli plus docker, then in localstack create the dynamodb streams and firehose. You can only get lambda + api gateway in sam local I believe.Periodate
You can deploy using cloudformation aws cloudformation deploy ..., but honestly I would not suggest using localstack, is very buggy.Recrudesce
Yeah, LocalStack is extremely buggy... I don't think it wasn't supposed to handle deployments. It claims to support CloudFormation and supports serverless AWS, so I'd expect it to handle deployments of SAM apps nicely. Turns out it can't, so I have to fallback to manually coding the AWS SDK to deploy my resources locally.Salep
C
8

I've just gone through this. I think the following is correct.

AWS SAM is a wrapper round Cloudformation. So your SAM template is actually a Cloudformation template. Your Cloudformation template defines your lambdas and dynamodb etc. When you deploy to AWS all of your lambdas and dynamodb go into AWS and you can test in the cloud.

When you run AWS SAM locally you run your lambda locally (in a docker container) but it still access resources in the AWS cloud.

LocalStack does have a CloudFormation interface, so it should be possible to deploy your CloudFormation template file. But I ran into a few issues with this and gave up.

Serverless Framework is similar to AWS SAM in that it is a framework to develop your serveless (lambda) code and to deploy that to AWS. Serverless has it's own yaml specification for defining a stack. Converting from Cloudformation to Serverless yaml is a little bit of work.

There is a serverless plugin for localstack. It is then possible to deploy your code to localstack. If you have API Gateway infront of your lambdas then there will be a local url you can hit which triggers your lambdas. This is fully within localstack and not using AWS SAM.

At this point you may find your code still hits the real AWS services. So you need to change the endpoint-url to point locally, as Andrew A. mentioned. For this, and to keep your code the same for test and production you can use environment variables for each of the service endpoints.

As Andrew A. mentions it should be possible to run code using SAM local which accesses resources provided by localstack. However, it may be preferrable to keep to one tool, if this was done within a testing pipeline say.

Congratulation answered 18/2, 2020 at 22:50 Comment(2)
The only way it works for me is using the AWS SDK to create resources manually in LocalStack. Couldn't get a SAM template deployment to work on LocalStack.Salep
pip install aws-sam-cli-local then samlocal deployTact
T
4

The makers of Localstack have created a lightweight wrapper for the sam cli. Simply use pip to install samlocal as a Python library on your machine:

pip install aws-sam-cli-local

You can then deploy to local stack using samlocal deploy in place of the regular sam deploy. Something like the following:

samlocal deploy --stack-name sam-app --capabilities CAPABILITY_IAM --guided

For further detail of the options available on sam deploy see the docs here.

Tact answered 20/5, 2021 at 21:13 Comment(0)
A
2

After some research, the most reliable way is to

  1. sam build and zip your code in .aws-sam/build
$ cd .aws-sam/build
$ zip -qr MyLambda.zip MyLambda
$ tree .aws-sam -L 2
.aws-sam
├── build
│   ├── MyLambda.zip
│   ├── MyLambda
│   └── template.yaml
└── build.toml

2 directories, 3 files
  1. Deploy the zipped code to localstack lambda, say I have a nodejs codebase
aws --endpoint-url=http://localhost:4566 lambda create-function \
    --function-name "my-lambda" --runtime=nodejs12.x --role dummy \
    --zip-file=fileb:.aws-sam/build/MyLambda.zip \
    --handler MyLambda/src/index.handler
Alsacelorraine answered 20/4, 2021 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.