AWS-IaC
AWS-SAM
Intro
AWS Serverless Application Model (AWS SAM) is an open-source framework for building serverless applications using infrastructure as code (IaC). With AWS SAM's shorthand syntax, developers declare CloudFormation resources and specialized serverless resources that are transformed to infrastructure during deployment. When working with AWS SAM, you will interact with:
-
AWS SAM CLI - A command-line tool that helps you develop, locally test, and deploy your serverless applications.
-
AWS SAM Template - An extension of CloudFormation that provides simplified syntax for defining serverless resources.
AWS SAM is an ideal IaC solution for scenarios where you want simplified serverless development with the full power of CloudFormation. For example, you can use SAM for:
-
Serverless applications: You can use SAM to quickly define AWS Lambda functions, Lambda durable functions, Amazon API Gateway APIs, Amazon DynamoDB tables, and other serverless resources with minimal code.
-
CloudFormation enhancement: You can combine SAM with existing CloudFormation templates to add serverless components to traditional infrastructure. SAM resources work alongside standard CloudFormation resources in the same template.
-
Local development and testing: You can use the SAM CLI to test Lambda functions locally, simulate API Gateway endpoints, and debug serverless applications on your development machine before deploying to AWS.
-
CI/CD for serverless: You can build deployment pipelines using SAM templates that automatically generate the CloudFormation infrastructure needed for staging and production environments.
-
Migration from console-created resources: You can convert Lambda functions and API Gateway resources created in the AWS Management Console into infrastructure as code using SAM templates.
Comparing AWS SAM with other IaC tools
-
Use SAM instead of CloudFormation to simplify serverless resource definitions while maintaining template compatibility.
-
Use SAM instead of AWS CDK if you prefer a declarative approach to describing your infrastructure rather than a programmatic one.
-
Combine SAM with AWS CDK by using SAM CLI's local testing features to enhance your CDK applications.
Installation
Linux installation
You can install this package via brew:
brew install aws-sam-cli
Basics
- Run the
sam initcommand to create your serverless application and create a project directory. - Modify the
template.yamlfile, which is your AWS SAM template. - Run the
sam buildcommand to update the changes you made in thetemplate.yamlto produce a new CloudFormation output. When you build, the AWS SAM CLI creates a.aws-samdirectory and organizes your function dependencies, project code, and project files there.
WARNING
The installed Python version must match the Runtime property specified in template.yaml. For production workloads, we recommend using Python 3.12 or earlier versions that are fully supported in AWS environments. If the Python versions don't match, you'll encounter build errors.
To resolve version compatibility issues:
- Specify a compatible runtime during initialization:
sam init --runtime python3.9 - Modify the
Runtimeproperty intemplate.yamlafter initialization
File structure
.
├── README.md
├── events // contain sample events
│ └── event.json
├── hello-world // contain lambda image code
│ ├── Dockerfile
│ ├── app.mjs
│ ├── package.json
│ └── tests // for testing lambda with sample event
│ └── unit
│ └── test-handler.mjs
├── samconfig.toml
└── template.yaml
Here are the important top-level files:
template.yaml: A template that defines the application's AWS resources.
And here are the dedicated folders:
hello-world/tests: folder containing nit tests for the application code.events: folder containing sample JSON events that you can use to invoke the function and test it out.
Building and deploying
building
Run the sam build command to build the CloudFormation template from your template.yaml
NOTE
If you don't have Python on your local machine, use the sam build --use-container command instead. The AWS SAM CLI will create a Docker container that includes your function's runtime and dependencies. This command requires Docker on your local machine.
The following is a shortened example of the .aws-sam directory created by the AWS SAM CLI:
.aws-sam
├── build
│ ├── HelloWorldFunction
│ │ ├── __init__.py
│ │ ├── app.py
│ │ └── requirements.txt
│ └── template.yaml
└── build.toml
Some important files to highlight:
-
build/HelloWorldFunction