Accelerating serverless development with AWS SAM Accelerate

Bypass most local emulation by testing serverless applications in the cloud against production services using AWS SAM Accelerate.
20.04.2022
Tags

Introduction

The very first task you have as a developer is to set up your development environment. But how would you set up the entire stack of AWS services (AWS Lambda, SQS, S3…) on your machine so you can run your application locally?

As a developer, you would be writing unit tests and integration tests and executing them locally and you would expect local iterations to be fast and convenient.

The main benefits of using a local development environment are working offline as well as simplicity of debugging.

Today, I’m going to talk about a feature that can significantly improve the development experience for serverless application developers. It’s called the AWS SAM Accelerate.

Challenges of serverless development

Local development environment (LDE) has been an industry standard for years and it’s safe to say that many of our customers are accustomed to it. However, serverless application development is different. It’s composed of multiple remote services. These services cannot be installed locally, they’re native to the cloud. Developers who want to continue to write code and execute tests in a local environment have to account for references to external cloud services in their code. Developers who want to continue to perform local unit testing will typically use emulators or mock frameworks in these cases.

What is an emulator ?

An emulator is a software component that resembles a remote service and often implements its API. Some examples of emulators include AWS SAM local which emulates AWS API Gateway, AWS Step Functions Local, Amazon Dynamodb Local and many more.

What is a mock framework ?

Mock frameworks are software libraries that generate replacement objects for external dependencies like cloud services. There are mock libraries like moto, AWS SDK moc that have been specifically designed for AWS services and then there are more generalized mocking frameworks like Easy Mock, Mockito, Jmock and others.

Mock and emulators Limitations

  • Not all services can be emulated or mocked
  • Emulators as well as Mocks can’t achieve feature parity
  • IAM security policies, service quotas can’t be tested

But testing is not as accurate when it comes to testing local services that can’t be emulated or mocked. The sheer breadth of functionality and rate of change means that any mock/emulation will always struggle to keep up to date, and it won’t take long before you run in to edge cases that break your emulation in a way you could not have seen without intimate knowledge of the platform (e.g. account limits), which mean your efforts to abstract away the implementation are not worth the additional effort.

In the next section I will show how SAM Accelerate can mitigate those limitations as well as boost the testing accuracy.

SAM Accelerate

The AWS Serverless Application Model (SAM) heralds a public preview of AWS SAM Accelerate. The AWS SAM CLI is a developer tool that makes it easier to build, locally test, package, and deploy serverless applications. SAM Accelerate is a new capability of SAM CLI that makes it faster and easier for developers to test code changes made locally to their serverless applications against a cloud-based environment, reducing the time from local iteration to production-readiness. SAM and SAM CLI are open-sourced. SAM Accelerate was launched in Beta in October 2021.

In the sections below, I’m going to present the three main SAM Accelerate features, showing what problem they solve and how you can use them.

SAM sync - Synchronize local code and configuration files with your cloud infrastructure:

Whenever we do a typical build and deploy to the lambda service under the hood there may be quite a few steps involved. you may have to download and assemble your dependency libraries, you may compress your deployment package and upload it to ECR or S3.

SAM typically creates a change set for you in AWS CloudFormation then you execute that change set, which will modify any related services and cause the lambda service to perform an update and pull the deployment package from storage, decompress it and deploy it.

With sam sync you can bypass many of these steps.

Sam sync is capable of performing code-only incremental deployments for faster cloud updates reducing feedback cycles from 30 seconds or more to 7 seconds or less. To get to know how to use these features it’s important to understand how each of the flags works so I’ll be covering some of the most important flags to show you the various new features of sam sync.

Flags:

  1. – stack-name : The name of the AWS CloudFormation stack you’re deploying to.
  2. – code : Sync code resources; This includes Lambda Functions, API Gateway and Step Functions.
  3. – watch : Watch local files and automatically sync with remote.
  4. – resource : Sync code for all types of the resource.

SAM logs - Aggregates multiple log resources into a single local stream:

This command combines log files from multiple sources into a single local flow. These can be: Lambda function logs, API Gateway logs or Step Functions logs.

This feature can:

  • tail logs locally with the --tail flag.
  • include X-ray traces and service graphs with --include-traces flag.
  • Specify a start-time and end-time to retrieve log events in a time frame.

Flags:

  1. –tail : Tail events. This will ignore the end time argument and continue to fetch events as they become available.
  2. –name : Name(s) of your AWS Lambda function. If this function is a part of a CloudFormation stack, this can be the LogicalID of the function resource in the CloudFormation/SAM template.
  3. –stack-name : Name of the AWS CloudFormation stack that the function is a part of.
  4. –start-time : Fetch events starting at this time.
  5. –end-time : Fetch events up to this time.
  6. –include-traces : [Beta Feature] Include the XRay traces in the log output.

SAM traces - Pulls x-ray traces and x-ray service graphs into a local log stream:

This command will allow you to pull AWS XRay traces and service graphs. Using the --tail flag you can tail XRay events, it also can be specific by including the --trace-id flag.

On the other hand, you can specify a start-time and end-time to retrieve events in a time frame using –start-time and –end-time flags.

Flags:

  1. –tail : Tail events. This will ignore the end time argument and continue to fetch events as they become available.
  2. –start-time : Fetch events starting at this time.
  3. –end-time : Fetch events up to this time.
  4. –trace-id : Fetch specific trace by providing its id.

Demonstration:

The application is called the maya app.

Maya is a very good dog most of the time. Sometimes Maya chews my slippers and on those days she is definitely very bad. Maya’s friends and family would like to track the status of her behavior in real time, so I’ve created a simple app using Amazon API Gateway and AWS Lambda to publish Maya’s current status. The app contains an API to inform me of Maya’s behavior: GOOD or BAD.

Now let’s see how I can use sam accelerate to speed up my development of the Maya app.

This is the template.yaml (AWS SAM templates are an extension of AWS CloudFormation templates, with some additional components that make them easier to work with. For the full reference for AWS CloudFormation templates, see AWS CloudFormation Template Reference in the AWS CloudFormation User Guide.) :

template.yaml

This is the app.js (this is where your lambda function is declared.) :

This is the app.js

We will use the SAM sync flags to see what we can achieve using this feature.

  1. sam sync --stack-name:

This command updates all infrastructure and code like the SAM deploy command, and also bypasses the AWS CloudFormation Changeset process. This leads leads to faster cloud updates

after running this command, the result is as shown below:

after running this command, the result is as shown below:

  1. sam sync --code:

Instructs SAM to perform “code only” update for fastest deployments. Capable of performing code only deployments for AWS Lambda functions, AWS Lambda layers, OpenAPI for Amazon API Gateway, and Amazon State Language for AWS Step Functions.

  1. sam sync --watch:

This command watches for any changes in the project files, and also performs a synchronization with each file change. The SAM sync –watch is smart enough to know whether to synchronize code only, or to do an entire deployment. (If we change something in the app.js file then it will be code-only synchronization, but if the changes are made inside the template.yaml file then a full deployment is triggered.)

  1. sam sync --resource:

This command limits the synchronization to the specified resource, and also narrowly scoped synchronizations can be faster. It Uses the logical id of the resource in the SAM template.

Conclusion

Until now, test-driven development was difficult to do in the cloud context. The feedback loop was slow enough to make it impractical, but with AWS SAM Accelerate, test-driven development is now absolutely feasible with a faster feedback loop. As with AWS SAM and the AWS SAM CLI (which includes AWS SAM Accelerate), this project is open source and you can contribute to the repository.