Bootstrap FreeKB - Amazon Web Services (AWS) - Deploy Serverless Application Model (SAM) app using the sam deploy command
Amazon Web Services (AWS) - Deploy Serverless Application Model (SAM) app using the sam deploy command

Updated:   |  Amazon Web Services (AWS) articles

This assumes you have already:

Let's say your SAM app was built in the /tmp/sam-hello-world/sam-app/.aws-sam/ directory. From the /tmp/sam-hello-world/sam-app directoy, the sam deploy --guided command can be used to deploy the app to AWS API Gateway.

]$ sam deploy --guided

Configuring SAM deploy
======================

        Looking for config file [samconfig.toml] :  Found
        Reading default arguments  :  Success

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [sam-app]:
        AWS Region [us-east-1]:
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [Y/n]: n
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: Y
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: y
        HelloWorldFunction has no authentication. Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: Y
        SAM configuration file [samconfig.toml]:
        SAM configuration environment [default]:

 

If the deploy is successfully, the following should be at the end of the output.

Successfully created/updated stack - sam-app in us-east-1

 

And Outputs should contain something like this. Notice this has URL https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Prod/hello/.

Key                 HelloWorldFunctionIamRole
Description         Implicit IAM Role created for Hello World function
Value               arn:aws:iam::123456789012:role/sam-app-HelloWorldFunctionRole-RiTFI3yH1db7

Key                 HelloWorldApi
Description         API Gateway endpoint URL for Prod stage for Hello World function
Value               https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Prod/hello/

Key                 HelloWorldFunction
Description         Hello World Lambda Function ARN
Value               arn:aws:lambda:us-east-1:123456789012:function:sam-app-HelloWorldFunction-MgYCNMibayEZ

 

And you should see your app in the AWS API Gateway console.

 

The deploy will also create an S3 Bucket, which can be seen using the aws s3api list-buckets command.

{
    "Buckets": [
        {
            "Name": "aws-sam-cli-managed-default-samclisourcebucket-123456789",
            "CreationDate": "2023-11-16T02:50:50+00:00"
        }
    ]
}

 

Or in the AWS console.

 

The deploy will also create an IAM (Identity and Access Management) role which can be seen using the aws iam list-roles command.

~]$ aws iam list-roles
[
    {
        "Path": "/",
        "RoleName": "sam-app-HelloWorldFunctionRole-RiTFI3yH1db7",
        "RoleId": "AROA2MITL76GAKGBEMHB3",
        "Arn": "arn:aws:iam::123456789012:role/sam-app-HelloWorldFunctionRole-RiTFI3yH1db7",
        "CreateDate": "2023-11-16T02:51:35+00:00",
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Principal": {
                        "Service": "lambda.amazonaws.com"
                    },
                    "Action": "sts:AssumeRole"
                }
            ]
        },
        "Description": "",
        "MaxSessionDuration": 3600
    }
]

 

Or in the AWS console.

 

The deploy will also create a Lambda Function which can be seen using the aws lambda list-functions command.

~]$ aws lambda list-functions
{
    "Functions": [
        {
            "TracingConfig": {
                "Mode": "PassThrough"
            },
            "Version": "$LATEST",
            "CodeSha256": "/ur2jHd199ldQbP+Y20sw3kIHhaWwnPY6CQvSwZ6Kco=",
            "FunctionName": "sam-app-HelloWorldFunction-MgYCNMibayEZ",
            "MemorySize": 128,
            "RevisionId": "741c094a-cff2-4cdb-a5b6-110a900e1a43",
            "CodeSize": 529507,
            "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:sam-app-HelloWorldFunction-MgYCNMibayEZ",
            "Handler": "app.lambda_handler",
            "Role": "arn:aws:iam::123456789012:role/sam-app-HelloWorldFunctionRole-RiTFI3yH1db7",
            "Timeout": 3,
            "LastModified": "2023-11-16T02:52:04.978+0000",
            "Runtime": "python3.9",
            "Description": ""
        }
    ]
}

 

Or in the AWS console.

 

Likewise, the sam list endpoints command should return both the /Prod and /Stage URLs of your SAM apps.

]$ sam list endpoints --template /tmp/sam-hello-world/sam-app/template.yaml --output json
[
  {
    "LogicalResourceId": "HelloWorldFunction",
    "PhysicalResourceId": "sam-app-HelloWorldFunction-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": [
      "/hello['get']"
    ]
  }
]

 

curl can be used to submit a GET request to the /Prod and /Stage URLs and JSON "hello world" should be returned.

~]$ curl --request GET --url https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Stage/hello
{"message": "hello world"}

~]$ curl --request GET --url https://zulgntw90l.execute-api.us-east-1.amazonaws.com/Prod/hello
{"message": "hello world"}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 929d90 in the box below so that we can be sure you are a human.