Amazon Web Services (AWS) - Update and Sync your Serverless Application Model (SAM) app using the sam sync command

by
Jeremy Canfield |
Updated: November 06 2023
| Amazon Web Services (AWS) articles
This assumes you have already:
- Downloaded and Installed the AWS Serverless Application Model (SAM) CLI on Linux.
- Initialize Serverless Application Model (SAM) app using the sam init command
- Built your Serverless Application Model (SAM) app using the sam build command
- Deployed Serverless Application Model (SAM) app using the sam deploy command
Let's say the template.yaml file for your SAM app is located at /tmp/sam-app/template.yaml.
]$ ll /tmp/sam-app
total 20
drwxrwxr-x 2 ec2-user ec2-user 24 Nov 16 02:13 events
drwxrwxr-x 2 ec2-user ec2-user 63 Nov 16 02:13 myapp
-rw-rw-r-- 1 ec2-user ec2-user 0 Nov 16 02:13 __init__.py
-rw-rw-r-- 1 ec2-user ec2-user 8329 Nov 16 02:13 README.md
-rw-rw-r-- 1 ec2-user ec2-user 679 Nov 16 02:13 samconfig.toml
-rw-rw-r-- 1 ec2-user ec2-user 1681 Nov 16 02:13 template.yaml
drwxrwxr-x 4 ec2-user ec2-user 80 Nov 16 02:13 tests
The sam list endpoints command should return both the /Prod and /Stage URLs of your Serverless Application Model (SAM) apps.
]$ sam list endpoints --template /tmp/sam-app/template.yaml --output json
[
{
"LogicalResourceId": "myFunction",
"PhysicalResourceId": "sam-app-myFunction-MgYCNMibayEZ",
"CloudEndpoint": "-",
"Methods": "-"
},
{
"LogicalResourceId": "ServerlessRestApi",
"PhysicalResourceId": "zulgntw90l",
"CloudEndpoint": [
"https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Prod",
"https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Stage"
],
"Methods": [
"/api/v1['get']"
]
}
]
curl can be used to submit a GET request to the /Prod and /Stage URLs. In this example, "hello world" is returned.
~]$ curl --request GET --url https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Stage/api/v1
{"message": "hello world"}
~]$ curl --request GET --url https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Prod/api/v1
{"message": "hello world"}
Let's say /tmp/sam-app/myapp/app.py contains the logic that returns JSON "hello world". Let's add "yo": "whats up"
import json
def lambda_handler(event, context):
return {
"body": json.dumps({
"message": "hello world",
"yo": "whats up?"
}),
}
The use the sam sync command to sync this change to AWS Cloud. This is like deploying an updated version of your app.
sam sync --template /tmp/sam-app/template.yaml --stack-name sam-app --no-watch
Now curl should return "dude, what's up?".
~]$ curl --request GET --url https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Stage/api/v1
{"message": "hello world", "yo": "whats up?"}
~]$ curl --request GET --url https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Prod/api/v1
{"message": "hello world", "yo": "whats up?"}
Did you find this article helpful?
If so, consider buying me a coffee over at