AWS SAM Basics
Basic configuration and application deployment using AWS SAM
AWS SAM
Serverless Application Model (SAM) is an extension to Cloudformation and used to define serverless applications. It provides a simplified syntax for defining resources like lambda functions, APIs, storages, permissions and integration between them. Allows with minimum lines configure a ready to go POC of application.
Boilerplate code expansion and transformation hapens during deployment process, SAM transforms it’s syntax into AWS CloudFormation one.
SAM template
Example template with s3 bucket as resource to be created in scope of this app.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
App01
SAM basics
Parameters:
Env:
Type: String
Description: "Logical environment like dev, test, prod etc"
Default: dev
Resources:
AppBucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub ${Env}-app01
SAM guided configuration
SAM can perform deployment through configuration flow with multiple questions before deploying the app. Resulting config can be saved for future reuse.
Guided flow
Resulting config after guide
version = 0.1
[dev]
[dev.deploy]
[dev.deploy.parameters]
stack_name = "app01"
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1f82bz09qi9s4"
s3_prefix = "app01"
region = "eu-west-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides = "Env=\"dev\""
Notes
There were no deployment bucket so SAM created one:
aws-sam-cli-managed-default-samclisourcebucket-1f82bz09qi9s4
To make SAM use defferent bucket s3_bucket parameter should be changed in the configuration or passed dusing guided deployment stage.
SAM bucket after deployment
Name contains a generated segment to make it unique for collision avoiding
Bucket contains a “folder” dedicated to application SAM has deployed
Application “folder” contains app template
SAM Cloudformation stack
Aside from creating a deployment bucket, SAM has it own stack deployed also.
With resource like s3 bucket SAM created, and policy for that bucket
SAM custom configuration
Start from creating custom s3 bucket SAM will use for future deployments
aws s3 mb s3://basicappdeployment --region eu-west-1
Example configuration for deployment to different environments like dev, test
version = 0.1
[dev]
[dev.deploy]
[dev.deploy.parameters]
stack_name = "dev-app01"
s3_bucket = "basicappdeployment"
s3_prefix = "dev-app01"
region = "eu-west-1"
confirm_changeset = true
capabilities = "CAPABILITY_NAMED_IAM"
parameter_overrides = "Env=\"dev\""
[test]
[test.deploy]
[test.deploy.parameters]
stack_name = "test-app01"
s3_bucket = "basicappdeployment"
s3_prefix = "test-app01"
region = "eu-west-1"
confirm_changeset = true
capabilities = "CAPABILITY_NAMED_IAM"
parameter_overrides = "Env=\"test\""
Notes
In order to disable configrmation for change set - remove or set to false confirm_changeset parameter
SAM deploy with custom config
Deployment using specific profile and logical environment (dev, test etc).
sam deploy --profile default --config-env dev --no-fail-on-empty-changeset
Notes
When deploynig using customly created s3 bucket SAM does not create a dedicated cloudformation stack.
Conclusions
Nice tool, easy to use, configure and control