Quick introduction to AWS Serverless Application Model

Malgorzata Sebastiampillai
DataPebbles
Published in
4 min readApr 11, 2022
credits: http://aws-cloud.guru/

Recently I’ve been using AWS resources to process the data.
Getting familiar with AWS concepts is essential to set up all necessary configurations.
The task was to create Amazon S3 bucket with a text file and write Amazon lambda function to read each line of text file and send it to SQS queue (Simple Queue Service). Where the second lambda function will read this message and delete it.
Using Amazon Web interface is quite easy to navigate and follow the the steps to set up everything.

But the question is how to write and invoke lambda function from local machine using Python?

Answer for above question is AWS Serverless Application Model (AWS SAM).

SAM is an open-source framework used to build serverless applications on AWS.

A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda function — it can include additional resources such as APIs, databases, and event source mappings.

Prerequisites

1. Created AWS account
2. Configuring IAM permissions and AWS credentials
If you have AWS CLI installed you can use aws configure command

$ aws configure 
AWS Access Key ID [None]: your_access_key_id
AWS Secret Access Key [None]: your_secret_access_key
Default region name [None]:
Default output format [None]:

3. Installed Docker — To run serverless projects and functions locally with the AWS CLI. AWS SAM provides a local environment thats similar to AWS lambda to use as a Docker container.
4. Installed Homebrew — the recommended approach for installing the AWS SAM CLI on macOS
5. Installed the AWS SAM CLI (using Homebrew)

brew tap aws/tap
brew install aws-sam-cli

Quick demo

To start the application with SAM Template you can get familiar with these commands:

#Step 1 - Download a sample application
sam init
#Step 2 - Build your application
cd sam-app
sam build
#Step 3 - Deploy your application
sam deploy --guided

The structure of the SAM application looks like below:

The contents of the project directory

There are three especially important files:

App.py and readsqs.py — Contains AWS Lambda handler logic.
requirements.txt — Contains any Python dependencies the application requires, used for sam build
template.yaml — Contains the AWS SAM template defining application’s AWS resources.

AWS SAM template.yaml file closely follows the format of AWS Cloud Formation template file which can be found in AWS CloudFormation User Guide.

template.yaml

The declaration Transform: AWS::Serverless-2016-10-31 is required for AWS SAM template files. This declaration identifies an AWS CloudFormation template file as an AWS SAM template file.

The Globals section defines properties that are common to all your serverless functions and APIs.

The Resources section can contain a combination of AWS CloudFormation resources and AWS SAM resources. In above case it is a lambda function triggered by S3 bucket and DestinationConfig which is a configuration object that specifies the destination of an event after Lambda processes it-in this case SQS message.

SAM build command and invoke from CLI command :

you can invoke lambda function directly from template

Run lambda function from template.yaml

or from lambda function itself:

Run lambda function from lambda_handler

After running function we can see an successful output where lambda read file from s3 bucket and send each line as sqs message:

To deploy our application on AWS another SAM command can be used:
sam deploy

Using sam deploy command for successful deployment to AWS

To validate that messages are sent successfully, we can check the logs from Amazon CloudWatch.

Conclusion

As you can see it is possible to invoke lambda functions and deploy it to Amazon Web Services from your local machine with no need of use Amazon web interface.

In my next blog I will show the alternative for local AWS development without need of use of Amazon AWS account :)

Till then happy learning!
For any queries reach me @ msebastiampillai@datapebbles.com

--

--