Deploy your app

The sam deploy command deploys your application by launching a CloudFormation stack. This command has a guided interactive mode, which you enable by specifying the –guided parameter. It is recommended to deploy with guided mode for the first time as it will capture the configuration for future deployments.

Run the following command in the same directory level where the template.yaml is located:

sam deploy --guided

It will ask for certain input parameters, like so: ask

Select Y for options that require Y or N.
Besides, our region is ap-southeast-1, so please change the region from default us-east-1 to ap-southeast-1.

Build completed

When the build finishes successfully, you should see a new directory created in the root of the project named .aws-sam. It is a hidden folder, so if you want to see it in the IDE, make sure you enable Show hidden files in Cloud9 to see it. build

Confirm deployment

At some point, SAM will ask for deployment confirmation. This is possible because it first creates a CloudFormation ChangeSet, and then it asks for confirmation to execute it. This is kind of like a dry run deployment and is a best practice when doing deployments via CloudFormation. Type y to confirm. confirm

Deployment completed

This command might take a few minutes to finish because it is creating the resources (Lambda function, API Gateway and IAM roles) on the AWS account. When it completes successfully, you should see an output similar to the following: completed

What just happened?

The guided deployment does few things for you. Let’s take a quick look at what happened under the hood during the guided deployment to understand this process better.

  1. Your codebase gets packaged in a zip file.
  2. SAM creates an S3 bucket in your account, if it doesn’t already exist.
  3. Zip file is uploaded to the S3 bucket.
  4. SAM creates the packaged template that references the location of the zip file on S3.
  5. This template is also uploaded to the S3 bucket.
  6. SAM starts the deployment via CloudFormation ChangeSets.

The first time you do a guided deployment, a new file samconfig.toml is created in the root of your project with your specified deployment parameters, this is so that the next time you execute sam deploy, it uses the same parameters without having you to enter them again. samconfig.toml