Kit Wai Chan - Fotolia

Tip

Use AWS SAM to consistently manage serverless apps

AWS Serverless Application Model can simplify the management of serverless apps. Use this step-by-step example to get started with the service.

Whether you're ready to write your first AWS Lambda function or want to add another to your collection of application components, you need a way to write, test, launch and deploy serverless code and infrastructure. And while you can do all of this from AWS Management Console, it's a cumbersome process.

Rather than manually launch and maintain AWS serverless components via the console, you can use AWS Serverless Application Model (SAM) for a consistent and traceable way to manage the code and infrastructure behind your serverless applications. The AWS SAM framework lets you define your serverless components using a combination of command-line interface (CLI) tools and configuration files.

AWS SAM provides a number of benefits. You can use a CLI tool and configuration files to execute common tasks, such as packaging or deploying code as many times as you need in a predictable way. And you can package those commands and files into automation scripts or deployment pipelines, which can eliminate tedious and error-prone manual tasks. With SAM, you can launch as many serverless environments as needed and get consistent results, which can help reduce anomalies between environments.

Additionally, you can treat configuration files as code, which enables a team to collaborate and track change history with code versioning tools to ease troubleshooting and issue resolution.

Example serverless model

To illustrate how AWS SAM works, let's walk through the steps required to launch a simple application component. Let's say you have a website and want to build a contact form that enables visitors to send a message to your email inbox.

You could put together an HTML form to post messages to an API endpoint that integrates with a Lambda function. This sends you an email using Amazon Simple Email Service (SES).

Amazon API Gateway, Lambda and Simple Email Service work with each other.
Use these three services, plus AWS SAM, to create a contact form.

First, follow these steps to install the SAM CLI on your local system. For a Python project, run:

sam init --runtime python3.6

This automatically creates a basic directory structure and a configuration file called template.yaml. This file contains configurations for Lambda functions and their event sources, such as S3 events, Kinesis, Simple Queue Service and API Gateway.

Use these lines of code to configure the most basic part of the API Gateway-Lambda combo:

 

SendEmailFunction:

    Type: AWS::Serverless::Function

    Properties:

      CodeUri: send_email/

      Handler: app.lambda_handler

      Runtime: python3.6

      Environment:

        Variables:

          EMAIL: [email protected]

      Events:

        ContactApi:

          Type: Api

          Properties:

            Path: /contact

            Method: post

      Policies:

       - AWSLambdaBasicExecutionRole

       - AmazonSESFullAccess

Then, you can create and save a test event file:

{"message": "Great website!" }

Finally, execute a command to test the recently created API and Lambda function locally:

sam local invoke "SendEmailFunction" -e tests/hello.json

Once everything looks good, you can package and deploy the API and Lambda function with two CLI commands: sam package and sam deploy. Previously, this process would take many manual clicks in the console, but with SAM, you can reduce it to one lightweight configuration file and two CLI commands.

While AWS SAM is designed for serverless components, you can declare any type of resource in your SAM templates with CloudFormation syntax. This powerful feature enables your serverless applications to work with a wide range of AWS resource types, as long as they are supported by CloudFormation.

Test functions locally

I often work with Lambda functions written in various runtimes. Before the release of the SAM CLI, I had to find a different tool to test my functions locally. Now, I can locally test any function in any runtime, such as Python, Node.js, Go, Java and C#.

You can test your code locally with realistic output because SAM Local uses a Docker container that resembles the Lambda runtime in the cloud. That output also includes execution time plus allocated and consumed memory, which makes code optimization much easier in a local environment. This enables you to save significant money on your AWS bill and fit those test files into a deployment pipeline.

Additionally, SAM Local has options to debug code, including with Visual Studio, which makes development much easier.

See the big picture

While SAM Local can be a helpful tool, it's not easy to install the CLI because of the number of steps involved, such as installing and configuring Docker on your local workstation. This is a one-time task, but it's not as straightforward as installing, for example, the AWS CLI.

Additionally, if you're just getting started with serverless applications, it might be better to begin with the console. There, you can write a Lambda function with some of the available blueprints and then integrate it with an event source, which you also configure in the AWS console.

But despite the learning curve and multistep installation process, AWS SAM will save you time and money in the long term, as it simplifies the development and maintenance of your serverless apps. And eventually, you should automate as much of the packaging, deployment and testing of your serverless apps as possible. Tools such as AWS CodeCommit, CodeBuild and CodePipeline, in conjunction with SAM, can deliver powerful results.

Also, you should familiarize yourself with CloudFormation before starting with SAM. For example, you could launch a CloudFormation stack with a simple EC2 configuration, which can help give you a broader picture of how SAM fits into AWS' infrastructure-as-code model.

Keep in mind that SAM only supports AWS components. If you need a multi-cloud serverless approach, a tool like Serverless Framework might be a better option.

Dig Deeper on AWS cloud development

App Architecture
Cloud Computing
Software Quality
ITOperations
Close