sam-cli
Quick Intro to SAM-CLI
sam-cli
sam-cli ( Serverless Application Model ) is an open source framework that enables you to build serverless applications on AWS.
aws sam comes in 2 parts - sam templates - sam cli
This tutorial focuses on :
- sam cli
- sam local
- sam build
- sam deploy
- sam tips & tricks
sam templates
aws sam is an abstract of aws CloudFormation. On deployment the sam template is converted to a CloudFormation template
sam serverless resources
- Function
- Api
- httpApi
- SimpleTable
- LayerVersion
- Application
- StateMachine
Lambda function event sources
- S3
- SNS
- Kinesis
- DynamoDB
- SQS
- Api
- HttpApi
- Schedule
- CloudWatchEvent
- CloudWatchLogs
- IotRule
- AlexaSkill
- Cognito
- EventBridgeRule
- MSK
- MQ
We'll be focussing on the lambda functions using sam.
Note : Secrets should be stored in AWS Secrets Manager or in AWS Systems Manager Parameter Store
Initializing a sam app is really easy, all you have to do is to type the command below :
-
sam init lets us select the template type we want. We can either select a quick start template or a custom template.
-
The next option is the package type, you can choose zip to save it to s3 or choose Image to save it as a container image in ECR.
-
Next we're presented with the available runtimes. These include the natively included runtimes.
-
Next we're asked to choose the project name and the template.
The generated project contains an event folder for mocking events in the local environment.
Let's look at the template.yml file.
This confirms that it is a sam template.
The resources section is where to define the resources we want to spin up on aws.
The CodeUri parameter points to the directory containing the lambda function.
To create a sam application that packages the application as a container image, we can follow the above steps with a slight difference in the step.
You'll be asked to select from quick start templates or custom templates. We'll go ahead with the quick start templates.
Once you've done that i'll ask you to select the runtime like before.
You'll notice differences in the template.yml file. Instead of the CodeUri parameter pointing to the directory containing the lambda function, you'll see PackageType as Image.
sam build
AWS supports two types of builds for Lambda Functions
- Native Run time builds that include :
- Node
- Python
- Ruby
- Java
- .Net Core
Custom build
- Makefile
The build type can be set using the Metadata property with the key BuildMethod
Steps in Native runtime build
- Install Dependencies
-
Code compiles to binaries
-
If packaging method is set to zip - Artifact copied to output folder
-
If packaging method is set to image - Docker image create
-
Updated templates file copied to output directory
To build the sam app enter the command below :
The above command creates a ".aws-sam" directory in the root directory.
The sam build has a flag parallel. This is to build all the resources in the templates in parallel.
sam local
sam local lets us mimic the actual environment locally.
The command that invokes a lambda function locally using sam is
If you have only 1 function in the template, then you can skip the FunctionName.
For functions that are triggered on some event happening, we can pass an event to the sam local invoke command which is stored as a json object.
If you don't have the event.json for the event ( for eg : SQS event ), it can be generated with the following command
This will take you to a prompt and let's you choose the suitable event for your use case.
For eg for sqs :
sam deploy
sam deploy steps when using the zip packaging type.
- The artifacts for any lambda functions in the template are zipped into a file.
- The zip file is uploaed to an s3 buckets.
- The sam template is updated with the S3 location of the zip file.
- The sam template is submitted to aws cloudformatoin for deployment
- The application infrastructure is created or updated.
sam deploy steps when using the image packaging type.
- The container images for each lambda function is uploaded to Amazon ECR.
- The SAM template is updated with the ECR location of container images.
- The SAM tempalte is submitted to AWS CloudFormation for deployment.
- The application infrastructure is created of updated.
To deploy the application using sam deploy :
The --guided flag prompts you for the deployment settings like stack-name, any environment variables you've set ( for eg : db config ).