aws-sam

The AWS Serverless Application Model (AWS SAM) is an open-source framework for building serverless applications. AWS SAM is natively supported by AWS CloudFormation and provides a simplified way of defining the Lambda functions, APIs, databases, and event source mappings needed by your serverless applications.

The AWS SAM template has

And when you need also others AWS resources and properties, you can add them and deploy the template by AWS CloudFormation or Sceptre. The unique requirement is to keep the Transform section, for AWS macros conversion.

AWS SAM prerequisite are Docker and, for Linux and Mac, also Homebrew (see how to install AWS SAM CLI) and then, by shell, you can install AWS SAM CLI with the command below

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

The available commands of aws-sam-cli are a lot, and you can manage all your serverless stacks with these commands.

By shell

AWS SAM service manages building, testing, deploying and more of what you need by a template and your code. The command for validate your template,

export AWS_PROFILE=your-account
export STACK_FILENAME=your-template-file
sam validate -t $STACK_FILENAME

For building and testing your application,

export AWS_PROFILE=your-account
export EVENT=your-event-data-json
export SOURCE=your-source-code-folder
export STACK_FILENAME=your-template-file
export PARAMETERS=your-parameters-string
sam build -u -s $SOURCE -t $STACK_FILENAME --parameter-overrides $PARAMETERS
sam local invoke -e $EVENT -t .aws-sam/build/template.yaml --parameter-overrides $PARAMETERS

For deploying your stack,

export AWS_PROFILE=your-account
export STACK_NAME=your-stack-name
export STACK_FILENAME=your-template-file
export PARAMETERS=your-parameters-string
sam deploy --stack-name $STACK_NAME -t $STACK_FILENAME --parameter-overrides $PARAMETERS --capabilities CAPABILITY_NAMED_IAM

For deleting your stack,

export AWS_PROFILE=your-account
export STACK_NAME=your-stack-name
aws cloudformation delete-stack --stack-name $STACK_NAME

Below you can find a simple sample of deployment of a lambda function by AWS SAM CLI.

cd sam/
git clone https://github.com/bilardi/aws-saving
export AWS_PROFILE=your-account
export STACK_NAME=test_aws_saving
export STACK_FILENAME=templates/lambda-basic.yaml
export PARAMETERS=$(tr '\n' ' ' < config/basic/lambda.txt)
sam deploy --stack-name $STACK_NAME -t $STACK_FILENAME --parameter-overrides $PARAMETERS --capabilities CAPABILITY_NAMED_IAM

For deploying other features and also an EC2, you need to use AWS CloudFormation or another tool that it supports AWS SAM resources and properties, like Sceptre.

By a bash script

An ad hoc script is only necessary if you need to manage

  • more parameters and / or dynamic variables like the name of other stacks like AWS CloudFormation
  • an exception as for deleting a stack with a S3 bucket not empty or managing more templates

Below you can find an example of deployment of a lambda function by a bash custom script.

cd sam/
export AWS_PROFILE=your-account
bash aws-sam.sh deploy sam

And for managing the other commands described above, you can use the same bash script

cd sam/
bash aws-sam.sh # print the commands list

Please, pay attention: in the config files, there are some identifiers that you need to change before running the bash script!

Remember

When you use AWS SAM,

  • you can test your application on your client by the commands sam local
  • you can manage AWS macros for Serverless, a little list of the AWS resources and properties and the functions Fn::Sub and Fn::If, but for all the others you have to manage with AWS CloudFormation or another tool that it supports AWS SAM resources and properties, like Sceptre