DEV Community

Deploying CI/CD For NodeJS Serverless Applications Workshop: Part III

Before we start building a fully automated continous delivery pipeline, in this part you will learn how to build, package and deploy a Serverless application by using the AWS SAM CLI.


Even though this workshop series is about automating deployments, this part is very important to learn the foundation of how to package and deploy a Serverless application.

Learning how to perform manual deployments is also useful for developers who want to create their own personal stack and deploy it often to test changes before commiting them to the source control repository.


Artifacts refer to the output of your build process in the context of CI/CD. Artifacts are typically in the form of a zip/tar file, a jar or a binary. You then take these artifacts and deploy them onto your different environments (i.e. Dev, Test, Prod).

For Serverless projects, artifacts must be uploaded to an S3 bucket for the Lambda service to pick them up. The SAM CLI takes care of managing this process of uploading artifacts to S3 and referencing them at deployment time.

The Zip file

The first artifact that gets generated in a Serverless project is your codebase, which gets compressed in a zip file and uploaded to an S3 bucket automatically by the SAM CLI during the package phase

The Packaged Template

The second artifact that SAM CLI generates during the package phase is the packaged template. Which is basically a copy of your project’s template.yaml, except that it references the location of the zip file (first artifact) in the S3 bucket.

The following image shows an example of a packaged template.


Notice how the CodeUri references the zip file on an S3 bucket, rather than on a local directory. This is how AWS Lambda is able to pull your code at deployment time.


To build a SAM project, we are going to use the sam build command. This command iterates through the functions in your application, looking for the manifest file (such as requirements.txt or package.json) that contains the dependencies, and automatically creates deployment artifacts.

From the root of the sam-app folder, run the following command in the terminal:

cd ~/environment/sam-app
sam build
Enter fullscreen mode Exit fullscreen mode

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.


Explore the build folder

Take a moment to explore the content of the build folder.

Notice that the unit tests are automatically excluded from it. This is because they are specified in the sam-app/hello-world/.npmignore file.



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
Enter fullscreen mode Exit fullscreen mode

It will ask for certain input parameters, like so:


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.


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:


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.


If you want to learn more about guided deployments and the samconfig.toml file, here is a good Blog Post:


Now that your Serverless application is deployed to an AWS account, let’s check if it is running as expected.

Navigate to the AWS CloudFormation console, make sure you are in the same region where you have been working on so far. You should see the new stack sam-app in the CREATE_COMPLETE status.


Click on the sam-app stack and then go to the Outputs tab. In this tab, you will see the API Gateway URL, the Lambda function ARN and the IAM Role ARN for the function. To test our application, copy the API Gateway endpoint URL and paste it in a new browser window.


When you navigate to this URL in the browser, API Gateway is invoking the Hello World Lambda function. If everything works, you will see Lambda response:



You have successfully deployed a Serverless application! Let’s automate deployments now!

Discussion (0)