
This application creates several AWS resources, including a Lambda function and an API Gateway. We defined these resources in the template.yaml file in this project. This project includes the following files and folders:
The SAM CLI extends the AWS CLI that adds functionality for building and testing serverless applications. It contains features for building your application locally, deploying it to AWS, and emulating AWS services locally to support automated unit tests.
To use the SAM CLI, you need the following tools.
Use the following command to build your application locally:
# build your application locally using a container
sam build --use-container
The SAM CLI installs dependencies defined in src/requirements.txt, creates a deployment package, and saves it in the .aws-sam/build folder. Read the documentation.
Use the following command to deploy your application package to AWS:
# deploy your application to the AWS cloud
sam deploy --guided
After running this command you will receive a series of prompts:
Stack Name: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. Use apigw-lambda as the stack name for this project.
AWS Region: The AWS region you want to deploy your app to.
Confirm changes before deploy: If set to yes, SAM CLI shows you any change sets for manual review before deployment. If set to no, the AWS SAM CLI will automatically deploy application changes.
Allow SAM CLI IAM role creation: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, SAM CLI scopes these down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or changes IAM roles, the CAPABILITY_IAM value for capabilities must be provided. If you don't provide permission through this prompt, you must explicitly pass --capabilities CAPABILITY_IAM to the sam deploy command.
Save arguments to samconfig.toml: If set to yes, SAM CLI saves your choices to a configuration file inside the project, so that in the future you can just re-run sam deploy without parameters to deploy changes to your application.
You will see several output values displayed after the deployment. Take note of the API Gateway Endpoint URL and the function name of the Lambda function (it is the last part of the generated ARN value) . On subsequent deploys you can run sam deploy without the --guided flag. Read the documentation.