Deploy Node-based SAM App on AWS

Software Guide
digital tech articles
4 min readNov 28, 2022

--

In this article, I will demonstrate creating a basic AWS SQS queue and AWS lambda as its subscriber via an automation process from AWS SAM.

Photo by Mehmet Ali Peker on Unsplash

Table of contents

  1. Prerequisites
  2. Setting up SAM and project
  3. Creating AWS Codecommit repo & S3 Bucket for storing Artifacts
  4. Push the first commit in the master branch
  5. Setting up SAM pipeline configuration — dev, prod
  6. Push initial commit to verify the deployment

Prerequisites

Before diving deep into the article, I will request everyone to please go through a basic understanding of the following key points.

  1. IAM Role, Policy
  2. What is serverless framework and SAM
  3. Setup AWS SAM CLI

Setting up SAM and project

  1. First, we need to download & install SAM CLI via link — https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html
  2. After installing SAM CLI from step1, start setting up projects by following the commands mentioned in the screenshot

In the above screenshot, you can see the following commands that will help you in setting up sam based nodejs project in your local system.

  1. sam init — This command will initialize your project
  2. cd sam-app-test && sam pipeline init — bootstrap-This command will setup the required cloud formation stack for your code pipeline like user roles and make it more automotive to update the deployment pipeline

After setting up the project as mentioned in previous screenshots, this is how your folder structure will look like. There is a file in the above screenshot which is buildspec.yaml. Which is nothing but the one shown below.

Key Points on build phases from the above file

  1. install — This command will set up a basic framework or runtime tool like java, nodejs, or python. For more clarity, please refer to the link — https://docs.aws.amazon.com/codebuild/latest/userguide/runtime-versions.html
  2. pre_build- This command will execute your basic unit tests
  3. build- This command will build your artifacts based on the language you are using
  4. post_build- This command will basically zip your entire artifact as a zip or an image based on your requirement and push those on the S3 bucket which will be provided as a runtime variable.

CCreating AWS Codecommit repo & S3 Bucket for storing Artifacts

After this, navigate to the AWS console and create one code repository in AWS Codecommit (Only if you are using AWS Code commit as your VCS)

After this create an S3 bucket which will be used to store artifacts created as part of sam package command.

Push the first commit in the master branch

After creating the repo in the previous step and execute the following commands on your project created in step 2 above.

  1. git init
  2. git remote add origin https://your-repo-url.com
  3. git add .
  4. git commit -m” Add Initial commit”
  5. git push -u origin master

Setting up SAM pipeline configuration — dev, prod

This step is one of the most important steps to configure. Please refer to the steps below very carefully. There are two pipelines can be created for one or two stages based on your configuration in step 3 above.

  1. Navigate to the AWS Code pipeline service
  2. Give pipeline a name
  3. Create code build project with configuration mentioned in next steps.
  4. Choose the image as ubuntu or Amazon Linux 2 based on the runtime you want to choose- Please refer to the link- https://docs.aws.amazon.com/codebuild/latest/userguide/runtime-versions.html
  5. Add an environment variable in the building project as S3_BUCKET, which is nothing but the name of the S3 bucket created in one of the steps above.
  6. Add the Deployment step by configuring the provider as AWS Cloudformation as in the below screenshot.

Push initial commit to verify the deployment

After that, One can push the code to the code commit repo and once you push the code in this repo, you may see this entire pipeline running from the build to the deployment stage.

One may see that pipeline getting succeeded and you may see the required lambda and AWS SQS getting created. you can verify the same and test SQS and lambda connectivity as well.

Conclusion

This article will help all the architects/developers who may be having challenge in setting up SAM based pipelines where you can deploy AWS lambda or any other resources.

--

--

Software Guide
digital tech articles

This publication is regarding latest technology trends and a place where you can learn lot more about technology which is far beyond the books.