Building an environment to build and deploy Python applications with AWS SAM CLI
This page has been translated by machine translation. View original
Introduction
Hello, I'm Shota Yamamoto.
Currently, I'm working on a development project using Python as a training assignment.
Since one of the requirements is to deploy using AWS SAM, I've compiled the steps for setting up an environment to build and deploy using the AWS SAM CLI.
This article assumes the following environment and conditions:
- You are using macOS
- Homebrew is already installed
- You have already obtained an AWS account and access key/secret key
This article will omit how to install Homebrew.
Also, we will be building with the following components:
- pyenv 2.6.23
- Python 3.12.12
- AWS CLI 2.32.0
- SAM CLI 1.154.0
Since this article starts with Python installation procedures, you can skip the relevant sections if you have already installed them.
1. What is AWS SAM
First, let me briefly explain SAM.
SAM (Serverless Application Model) is a framework provided by AWS for building serverless applications.
Based on CloudFormation, it allows you to efficiently build and deploy Lambda, API Gateway, etc. on AWS.
SAM CLI is a command-line tool for building and deploying SAM applications.
You can easily deploy to AWS using SAM templates.
2. Installing Python
First, we'll install pyenv to install Python.
Open the terminal and enter the following command. (You don't need to enter the % symbol)
% brew install pyenv
After installation is complete, enter the following command to confirm the version is displayed.
% pyenv --version
pyenv 2.6.23
Once you've confirmed the version is displayed, run the following commands to add it to your path.
% echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
% echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
% echo 'eval "$(pyenv init -)"' >> ~/.zshrc
The above commands append the pyenv path to .zshrc.
To apply these settings to your current terminal, run the following command:
% source ~/.zshrc
Next, we'll use the installed pyenv to install Python.
You can get a list of installable versions with the following command:
% pyenv install --list
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
2.4.2
...
There are many versions, but this time we'll install 3.12.12.
You can install it with the following command. Also, specify the version we just installed as the default.
% pyenv install 3.12.12
% pyenv global 3.12.12
After installation is complete, check if it's installed correctly and verify the version with the following command:
% python --version
Python 3.12.12
3. Installing AWS CLI
Next, we'll install the AWS CLI so SAM can deploy to AWS.
Enter the following commands to install and check the version:
# Installation
% brew install awscli
# Version check
% aws --version
aws-cli/2.32.0 Python/3.13.12 Darwin/25.2.0 source/arm64
If aws-cli/2.32.x is displayed, the installation was successful.
After installation is complete, set up authentication. When you run aws configure, you'll be prompted to enter your secret key and other information, so enter each one.
Deployment will be performed using the authentication information you entered.
% aws configure
AWS Access Key ID : AKIAxxxxxxxxxxxxxxxx
AWS Secret Access Key : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name : ap-northeast-1
Default output format : json
Run the following command and if the authentication information you set is displayed, it was successful.
% aws sts get-caller-identity
Note that this method directly inputs access keys, and the authentication information entered is stored in plaintext locally, so caution is required.
From a security perspective, authentication using IAM Identity Center (AWS SSO) or using short-term credentials is recommended if possible. For more details, please refer to the official documentation on AWS CLI authentication settings.
Also, in environments where MFA is enabled for authentication, you may need to obtain authentication information separately. Please check as appropriate.
4. Installing the SAM CLI
Next, we'll install the SAM CLI.
Install using the following command, just as we did when installing the AWS CLI.
# Installation
% brew install aws-sam-cli
# Installation verification
% sam --version
SAM CLI, version 1.154.x
5. Testing Build and Deploy
Once all installations are complete, let's test building and deploying.
Project Initialization
Navigate to your project folder and run the following command:
# Set the version to use for the project
% pyenv local 3.12.12
# Initialize SAM project
% sam init
When you initialize, selection items will appear as follows. Choose according to your needs.
As an example, I'm also listing what I selected:
# Do you want to use an official template?
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1 # Use the official template
# Which template to use
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Data processing
...
Template: 1
# Use the default runtime?
Use the most popular runtime and package type? (python3.14 and zip) [y/N]: n # Selected "n" to specify the version
# Which language environment
Which runtime would you like to use?
1 - dotnet10
2 - dotnet8
...
Runtime: 19 # Choose according to the Python version installed. 3.12 in this case
# Lambda function deployment method
What package type would you like to use?
1 - Zip
2 - Image
Package type: 1 # Package the code in Zip format
# Enable X-Ray tracing?
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: n
# Enable CloudWatch Application Insights monitoring?
Would you like to enable monitoring using CloudWatch Application Insights?
For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: n
# Use JSON format for logs?
Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]: n
# Project name
Project name [sam-app]: python-project
After all selections are complete, the project will be generated with a folder structure like the following (structure may differ depending on the template selected):
project_name/
├── template.yaml
├── samconfig.toml
├── hello_world/
│ ├── app.py
│ └── requirements.txt
├── tests/
└── README.md
Build and Deploy
Once initialization is complete, you can build and deploy as follows:
# Build
% sam build
# Deploy (first time)
% sam deploy --guided
# Deploy (subsequent times)
% sam deploy
For the first deployment only, enter the following items:
Stack Name : Any stack name
AWS Region : ap-northeast-1 # Region to use
Confirm changes : y # Whether to confirm changes before deployment
Allow IAM role creation : y # Whether to automatically create roles
Disable rollback : N # Whether to disable rollback when errors occur
Save to samconfig.toml : y # Save the above settings
Let's check if the deployment is successful in the AWS Management Console.
If the deployment is successful, you will see that a stack with the name you set has been created.

Finally, here's how to delete resources.
You can delete them with the following command. When prompted, enter "y" for both options.
% sam delete --stack-name (stack name)
Are you sure you want to delete the stack (stack name) in the region ap-northeast-1 ? [y/N]: y
Do you want to delete the template file xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.template in S3? [y/N]: y
Conclusion
That completes the process.
In this article, I introduced the procedure for setting up an environment to deploy using SAM.
While I didn't cover it this time, you can also test Lambda functions locally using Docker. Please give it a try.
I hope this article has been helpful to you.
Thank you for reading to the end.
References
About Classmethod Operations Inc.
We are the operations company of the Classmethod Group.
Our specialized teams in operations, maintenance development, support, information systems, and back office provide expert services from business outsourcing to problem-solving and high value-added services through mechanisms that fully utilize IT and AI.
We are recruiting members for various positions.
If you are interested in our culture, mechanisms, and work styles that realize both "Operational Excellence" and "Work and live in your own way," please visit the Classmethod Operations Inc. corporate website. *We changed our name from Annotation Inc. in January 2026.
