Serverless - Create, debug and deploy Lambda and API Gateway via AWS SAM and AWS Cloud9

tiamatt

Samira Yusifova

Posted on January 20, 2021

Serverless - Create, debug and deploy Lambda and API Gateway via AWS SAM and AWS Cloud9

Let’s say you want to create and deploy a very basic serverless architecture - an API backend that consists of an Amazon API Gateway endpoint and an AWS Lambda function.

Alt Text

For that you need to provision the following resources:
👉 API Gateway
👉 Lambda function
👉 IAM role

What is the fastest and the most convenient way to provision and deploy these serverless resources using AWS native framework?

AWS Serverless Application Model

And the answer is AWS SAM.

AWS SAM (Serverless Application Model) is meant to build serverless applications. Built on AWS CloudFormation, AWS SAM provides shorthand syntax to declare serverless resources.

Alt Text

These 20 lines of code turn into the following serverless architecture:

Alt Text

I took these screenshots from Eric Johnson’s awesome tech talk
which I would highly recommend to watch.

During deployment, AWS SAM transforms the serverless resources into CloudFormation syntax, enabling you to build serverless applications faster.

With that being said, let’s get our hands dirty!

Step 1. Set up AWS Cloud9 environment

In order to write, run, and debug applications with just a browser, without needing to install or maintain a local IDE, we’ll go with AWS Cloud9 IDE. It preconfigures the development environment with all the SDKs, libraries, and plug-ins needed for serverless development. It also provides an AWS Toolkit extension for locally testing and debugging AWS Lambda functions and API Gateway.

First step is to set up a new AWS Cloud9 environment. Sign in to AWS Management Console, go to AWS Cloud9 and click on Create environment:

Alt Text

Give a name to your new environment (you can optionally add description) and go to the next step:

Alt Text

I’ve selected t2.micro instance type as it is covered by the AWS Free Tier.

Alt Text

Once you are done with environment configurations, review your settings and submit. It should take a couple of minutes to launch a new EC2 instance for a new Cloud9 environment. As you can check, there are a lot of preinstalled runtimes and package managers, such as Docker, AWS CLI, pip:

Alt Text

You can run the following command to get a list of all available packages (it’s a pretty long list!):

yum list installed
Enter fullscreen mode Exit fullscreen mode

Step 2. Create a SAM application

On the upper left hand side of the screen you should see AWS Toolkit's AWS icon. Click on it to open AWS:Explorer window. Expand your region and right-click on Lambda, then Create new SAM Application:

Alt Text

Cloud9 should walk you through the following steps:

Alt Text

Alt Text

Alt Text

Alt Text

Voila! AWS SAM creates a directory with the name that you provided as the project name with all necessary files and folders:

Alt Text

  • hello_world - code for the application's Lambda function
  • events - invocation events that you can use to invoke the function
  • tests - unit tests for the application code
  • template.yaml - a template that defines the application's AWS resources

Step 3. Debug your function locally

Navigate to hello_world/app.py to see AWS Lambda handler logic. We can make a little clean up and uncomment location:

Alt Text

Use Ctrl + S button to save changes in app.py file.

Make sure that you've specified requests dependency in requirements.txt (if you use Node.js functions then check package.json):

Alt Text

Let’s leverage AWS Toolkit to make debugging easier! AWS Toolkit is a very powerful tool that will invoke SAM CLI to build the serverless project, deploy the code to a local Docker instance, invoke the Lambda in Docker, allowing you to step-through your Lambda code. To run the function AWS Toolkit needs launch configuration file. It should be auto-generated during a new SAM app creation - check launch.json file under the .c9 folder.

By the way, if you don't see .c9 folder and some other folders/files then you might need to turn on Show Hidden Files:

Alt Text

Ok, back to launch configuration that gives us more flexibility to configure launchers for our SAM template. As it was mentioned before, launch.json file can be found under the .c9 folder. You can also open the file by Editing Launch Configurations:

Alt Text

In case you are missing launch.json file or if it is empty, open your template file, and click on Add Debug Configuration link. It should automatically set launch configurations for you.

Alt Text

Once launch configuration file is set you should see your function in debugging dropdown:

Alt Text

Set some breakpoints in source code (hello_world/app.py), then select MyFirstSamApp:HelloWorldFunction option from dropdown and click on Run:

Alt Text

Note, in case AWS Toolkit fails to run the function, throwing RuntimeError: Container does not exist, you would need to increase the size of your EBS volume. See how to fix the issue here.

Alt Text

Step-through debugging makes it easier to understand what the code is doing. Thanks to AWS Toolkits it's easy to set breakpoints, execute code line by line, and inspect the values of variables:

Alt Text

After debugging you should get the final response in AWS Toolkit window:

Alt Text

Step 4. Build the project

Building your serverless application involves taking your AWS SAM template file, application code, and any applicable language-specific files and dependencies, and placing all build artifacts in the proper format and location for subsquent steps in your workflow.

First, change into the project directory, where the template.yaml file for the sample application is located.

cd MyFirstSamApp
Enter fullscreen mode Exit fullscreen mode

Then build the project inside a Docker container:

sam build --use-container
Enter fullscreen mode Exit fullscreen mode

AWS SAM builds any dependencies that your application has, and copies your application source code to folders under .aws-sam/build to be zipped and uploaded to Lambda.

Alt Text

Step 5. Deploy the project to the AWS Cloud

After you develop and build your serverless application, you can deploy your application using the following AWS SAM command:

sam deploy --guided
Enter fullscreen mode Exit fullscreen mode

When you specify the --guided flag, the AWS SAM zips your application artifacts, uploads them to Amazon S3, and then deploys your application to the AWS Cloud.

Alt Text

Alt Text

AWS SAM deploys the application using AWS CloudFormation. In the output you can see the changes being made to your AWS CloudFormation stack.

Alt Text

After you've created an AWS CloudFormation stack, you can use the AWS Management Console to view its data and resources:

Alt Text

Results

Time to call our API Gateway endpoint! Go to Outputs sections and click on the link for HelloWorldApi:

Alt Text

When you send a GET request to the API Gateway endpoint, the Lambda function is invoked. This function should return a hello world message with the public IP address:

Alt Text

So, we successfully invoke the function and got the expected response with status code 200.

Now, you know how to configure and use AWS SAM and Cloud9 to build the serverless applications for AWS. Congratulations!

Clean Up

You might be charged for running resources. That is why it is important to clean all provisioned resources once you are done with the stack. Use the AWS CloudFormation command to delete the stack along with all the resources it created:

aws cloudformation delete-stack --stack-name my-first-sam-apps-stack
Enter fullscreen mode Exit fullscreen mode

Summary

Using AWS SAM for serverless application development saves a lot of time by eliminating much of the boilerplate that AWS CloudFormation templates require. It extends AWS CloudFormation with new resource types that follow AWS best practices and are more comfortable to use.

💖 💪 🙅 🚩
tiamatt
Samira Yusifova

Posted on January 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related