DEV Community

Piero Bozzolo
Piero Bozzolo

Posted on • Originally published at Medium

Accelerate serverless development cycle with SAM Accelerate

Accelerate serverless development cycle with SAM Accelerate

One of the most common objections about serverless is that the development cycle and the test release can be extremely slow.

When you start with a serverless test project the first releases are fast and streamlined, but one may encounter problems once resources are added.

As the project expands we will probably start to need a CI / CD environment. This is an excellent method to release the application code in a staging or production environment. Still, when we have to run the pipeline just for test or to update one single function it can be uncomfortable, awkward, and a time-wasting activity. The pipeline steps can be executed locally from your machine, but you will only save a few seconds. The deployment time grew with the project.

We would need a feature in serverless frameworks that streamlines this process in the development phase.

For some time this feature has existed in the AWS Serverless Application Framework (hereinafter AWS SAM) framework in the form of a beta feature and is now available to everyone.

Let’s take a short step back and try to understand what SAM is and how it works. If you already know SAM you can go directly to the next section.

Serverless Application Model

A serverless application is made up of code and configurations. How can we manage the project and how can we describe my applications so that can be released in multiple environments? We need a tool that packages my code and all his stuff and releases it in the cloud.

In AWS one tool is AWS SAM. AWS SAM is strictly related to AWS CloudFormation with whom it shares syntax and introduces an easy way to create and configure serverless resources (i.e. Lambda Function, Bucket S3, or DynamoDB tables).

Let’s see an example: We need a lambda function invoked when an object is created on an S3 bucket.

Our code folder will contain the following files:

template.yaml #application definitions
onupload_function/app.js #application code
Enter fullscreen mode Exit fullscreen mode

The content of the *template.yam*l will be:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31Resources:ArticlesS3Bucket:
  Type: AWS::S3::BucketOnUploadFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: app.lambdaHandler
    Runtime: nodejs14.x
    CodeUri: onupload_function/
    Events:
      OnFileCreated:
        Type: S3
        Properties:
          Bucket: !Ref ArticlesS3Bucket
          Events: s3:ObjectCreated:*
Enter fullscreen mode Exit fullscreen mode

In the onupload_function/app.js we will write a function that reacts to the upload event on the S3 bucket. In the example, we will just log the event payload:

exports.lambdaHandler = async (event, context) =>{
 console.log(JSON.stringify(event));
};
Enter fullscreen mode Exit fullscreen mode

To release our application it will be enough from the console to run the following commands:

$ sam build
$ sam deploy
Enter fullscreen mode Exit fullscreen mode

As you can see many seconds are needed before having the whole infrastructure released. In the deployment phase, you will also choose a CloudFormation stack name to which the application belongs. Keep it noted.

This is a mandatory step that must be completed one time also with the new feature.

How to streamline the deployment phase

As we saw the deployment phase is easy and not complex, but it can only become slow. Every time we add or make changes in the infrastructure the whole code is packaged, uploaded, and released. The build and deployment process can last 20 minutes.

For speeding up the deployment process we can use the new “sam sync” feature which, unlike “sam deploy”, can release only the differences that we have done to the code and to the infrastructure. If we want to use that feature we must complete at least one time the “sam deploy” phase.

Sync also comes with a very handy file watcher that is able to monitor our template and apply the changes when we save the file.

To use sync and its watcher file we need to run this command:

sam sync --stack-name sam-app --watch
Enter fullscreen mode Exit fullscreen mode

the stack name will be the name we gave to the stack in the deployment phase.

Our application will be updated in seconds and resources such as Lambda function, API definitions in API Gateway, Step Function and other infrastructure components will be ready to be truly tested in the AWS infrastructure.

If you are interested in the serverless world, don’t miss the Claranet live shows on Twitch https://www.twitch.tv/claranet_it and the videos on my YouTube channel https://www.youtube.com/channel/UCSC2n9Q4fRacTr0jGOdcO0g

Top comments (0)