Testing serverless applications in the cloud with SAM Accelerate
TL;DR: AWS introduces SAM Accelerate (Beta) to speed up serverless deployment.
Whenever we introduce serverless to customers who are unfamiliar with it, we’re normally asked "how do I run this locally"?
And my answer used to be: “You don’t”.
Whilst you could run serverless applications locally, using tools like sam local, I just didn’t see the need for it. My preferred workflow was to:
- develop iteratively with TDD (Test Driven Development).
- deploy to the cloud to check integration and IAM permissions. I used our build process for this as it spun up a new stack for a pull request.
Unfortunately sam local couldn’t help with either iterative development or cloud deployment, so I saw it as wasted effort. Our CI process was fairly quick (less than 10 mins, for some things 5 mins, subsequent builds were quicker), so it was always felt that the tradeoff in speed was more than offset by the advantages of running it in the cloud.
Now with SAM Accelerate we also have speed - deployments which previously took five minutes are now taking under 5 seconds. Add that up over the multiple deployments for each feature, and you’ve got a considerable time saving!
SAM (Serverless Application Model)
For those unfamiliar with SAM it is a framework to develop serverless applications on AWS. It (greatly simplifying) provides CloudFormation extensions to define resources as well as a CLI to build, locally test, and deploy with a CloudFormation stack.
There are the usual docs provided by AWS:
However, I prefer the GitHub page: https://github.com/aws/serverless-application-model
SAM Accelerate was introduced in v1.34.1 of the aws-sam-cli and is currently in beta.
As of writing these are the available resource types:
It introduced several new features:
- sam sync --stack-name XXX
This will setup some Cloudformation stacks:
- sam stack (aws-sam-cli-managed-default)
- Infrastructure stack
- Layer stack (if you have lambdas with dependencies)
- sam sync --stack-name XXX --code
Deploy code changes but not CloudFormation infrastructure
- sam sync --stack-name XXX --code --resource AWS::Serverless::Function
Deploy a set of resources (for example all Lambda functions)
- sam sync --stack-name XXX --code --resource-id YYY
Deploy a specific resource
- sam sync --stack-name --watch
This is the most interesting one. The sam sync --watch option tells AWS SAM to monitor for file changes and automatically synchronise when changes are detected. If the changes include configuration changes, AWS SAM performs a standard synchronisation equivalent to the sam sync command. If the changes are code only, then AWS SAM synchronises the code with the equivalent of the sam sync --code command.
- sam build --cached
This change separates the cache for dependencies from the cache for the code. The build command now evaluates these separately and only builds artifacts that have changed.
- sam logs --stack-name XXX --include-traces --tail
include-traces is a new option that will include API Gateway logs (make sure you have a logging role setup in API Gateway) and X-Ray traces. Combined with the tail option this will give instant feedback in the console when functions are invoked.
This is a good representation of a developer workflow using these features:
If you want to get started using SAM Accelerate there is a great tutorial available on the AWS blog.
The best place to test is in the cloud
"When testing serverless applications, developers must get to the cloud as soon as possible. AWS SAM Accelerate helps developers escape from emulating the cloud locally and move to the fidelity of testing in the cloud." - AWS
As I said at the start I completely agree with this approach. There are certain things you can ONLY test in the cloud, for example your AWS service integrations.
AWS SAM Accelerate enables you to test your code quickly against production AWS services in the cloud. The faster you get there, the earlier you will find issues - and the quicker they’ll be resolved. Combined with browser based IDE’s I think this will be the future of serverless development.
Learn more about serverless
Practical Immersion Days
If you want to develop yours and your teams' knowledge around serverless application development, we offer half- and full-day Immersion Days.
Immersion Days combine presentations, practical labs and discussions to enable participants to really get to grips with the subject matter, and the content can be tailored to suit the needs and skills of your team.
Read about how FinTech Origo benefitted from Serverless Immersion Days and get in touch to discuss your own requirements.
Listen to the podcast
This episode featuring AWS Serverless Hero Yan Cui (aka, the Burning Monk), asks: What kinds of applications are suited to serverless? How do you get started? And why should you consider adopting a serverless architecture?