Deploying a simple Lambda Function using SAM in AWS

Deploying a simple Lambda Function using SAM in AWS

I have underestimated the power of AWS SAM until recently when I had to jump on a project that required me to build a data Ingestion pipeline that leverages the combination of lambda, API Gateway, State Machine, RDS and Glue.

  1. The entire infrastructure was lambda heavy and one of the biggest benefits of SAM for me (in my project) is the ability to invoke my functions locally for testing.
  2. Another advantage was leveraging shared dependencies amongst all the resources I was deploying without having to repeatedly use the same block of templates for multiple stacks.
  3. SAM also gave me the flexibility to detach the complexity in my CloudFormation -template by leveraging nested stack

This Article will focus on deploying a simple lambda function using SAM CLI and will be divided into the 2 sections

  • Downloading Source Code and understanding the directories & templates
  • Setting up your development environment
  • Building and Deploying SAM

Downloading Source Code and understanding the directories & templates

Log into your AWS account and create a programatic IAM user. For the sake of this project, give it a full IAM permission (Please do not do this in a production environment). You should have your ACCESS KEY and SECRET KEY, keep them somewhere safe.

I have created all the required directory structures, and templates needed for this project in my Gitlab. Visit my Gitlab here and download the Zip file to your local machine

Once you land on the repo, click the download button on top left and select zip

On the top right, click download and then select Zip.

The zip file named sam-deploy-example-main.zip should be available in your downloads.

Navigate to your downloads and unzip the file named sam-deploy-example-main.zip

Once done, you can delete the zip file and open the unzipped folder named sam-deploy-example-main. The directory looks similar to the below image

No alt text provided for this image






  • lambda: the directory created to store the lambda function we will be working with. It contains the source code of the lambda we want to deploy. The actual code is found in lambda_function file in the src folder. Its contains a simple function that returns "I work"
  • requirements.txt file: this file contains the 2 packages (aws-sam-cli & awscli) we will install on virtual environment to enable us run our sam deployment smoothly
  • template.yaml file: this is the file used by SAM to run its build process. It's probably the most important file in this project. SAM knows by default to find a file named "template.yaml" in the root of our project in order to build except we specify otherwise.

Below is the content of template.yaml which is a CloudFormation template that creates a resource named myLambda. Line 8 specifies the location of the stack it should create. As the directory already shows, we have a file named mylambda.yaml in nested-stack directory.

No alt text provided for this image





  • nested-stack directory: This contains the CloudFormation template (mylambda.yaml) that will deploy our lambda function code explained above.
  • mylambda.yaml file: this is the template file that was referred to from our template.yaml file. let's examine the contents

No alt text provided for this image






This template is creating a resource named sampleLambda, a lambda function whose source code is located 2 directories backward in lambda/sample_lambda/src/ directory. The actual file name is lambda_function and the function used will be lambda_handler (aka handler). We are using python3.7 runtime with a memory size of 128mb and we want our function to only timeout after running for 60 seconds. The lambda function will be named my-sample-lambda and we also gave it a short description. In case your python version is not python3.7, go ahead and edit mylambda.yaml file, and change the runtime to your python version.

Now that we understand the structure of our directory and the templates in them, let's get our hands dirty!

Setting up your development environment for build process

  1. Open your terminal (windows users should open powershel)
  2. Navigate to your download directory. Mine looks like the below:

No alt text provided for this image

3. Change into the directory of the unzipped folder above named sam-deploy-example-main by running the below command

cd sam-deploy-example-maind
sam-deploy-example-main        

By now, you should be in the directory of our project.

4. While you are in the project root directory, create a virtual environment named venv and activate the virtual environment by running the commands below. (Make sure python 3 is installed on your local machine. )

Mac users can run the below command:

>> python3 -m venv venv
>> source venv/bin/activate        

Window users can run the below command:

>> py -m venv venv
>> venv\Scripts\Activate        

5. Next, install aws-sam-cli, we cannot build our SAM without installing this package in our virtual environment. We also need to install awscli in order to configure AWS on our local computer to access the services. requirements.txt contains both packages and running the command below in terminal or shell will automatically install both packages

>> pip install -r requirement.txt        

You should have a pattern similar to the image below in your terminal

No alt text provided for this image

6. Run the command below to configure aws cli

>> aws configure        

You will be prompted to enter your ACCESS_KEY_ID, SECRET_KEY_ID, region and default output. Make sure you enter the access and secret key aforementioned. Choose the region of your json and leave the default out put blank

Building and Deploying SAM

Once the above set and installations are done, we are ready to build SAM.

1. Run the command below to build your SAM Application

>> sam build        

What this does is to use the template.yaml file at the root of this project to create a build including any dependencies associated.

You should see a pattern like the image below once you run the command

No alt text provided for this image

2. . To Deploy what was built above. Run the command below:

>> sam deploy --stack-name my-sam-deploy-stack --resolve-s3 --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM        

The command above deploys our build as a CloudFormation stack. It requires us to provide stack name (my-sam-deploy-stack), we also provided an argument --resolve-s3. This will help to create a managed default bucket to store deployment artifacts. Lastly, we provided capabilities argument (CAPABILITY_IAM?and?CAPABILITY_AUTO_EXPAND) because our template includes resources that can affect permissions in our AWS account. Our template also contains macros that performs custom processing on templates (simple actions like find-and-replace operations).

You should see a display similar to the image below after running your command

No alt text provided for this image

Yessssss! You did it!

It's time to visit our AWS Console to confirm this worked.

Open your Cloudformation service console and you should see something similar to the below image:

No alt text provided for this image

Open the lambda service console in your AWS account and confirm that it was successfully deployed.

I hope this was helpful!

Leave a comment! Tell me what you think. Do you have any questions, concerns or need something to be clarified further? Don't hesitate to drop a comment.

Paul Obalonye

Snr Azure Devops Engineer | Cloud Engineer & Snr SQL Database Administrator

3 年

Thanks for sharing! Easy simple steps! ????

Christian Nnachi

Solution Architect

3 年

This was very informative?? Thanks! for sharing

Tolu Daramola

Cloud Consultant | Cloud Platform Engineer | AWS Certified Solutions Architect | AWS Certified Cyber Security Architect | ?? 3*AWS CERTIFIED

3 年

Aws-ome..... AWESOME Bro, easy to read and follow with images ????????

Busari Babatunde

2X AWS |Cloud Engineer | Devops | Security

3 年

This is great

要查看或添加评论,请登录

Gabe Olokun的更多文章

  • Sending Calendar Invitation with javascript via SES Unveiled.

    Sending Calendar Invitation with javascript via SES Unveiled.

    I took a break from writing for a while as I immersed myself in building my company, Techchak. Throughout this…

    4 条评论
  • Add External Python Libraries to AWS Lambda using Lambda Layers

    Add External Python Libraries to AWS Lambda using Lambda Layers

    If you are a Cloud Engineer or Developer who loves to code in python like myself, 7 out of 10 times, you might find…

    53 条评论
  • CREATE YOUR FIRST CICD PIPELINE

    CREATE YOUR FIRST CICD PIPELINE

    In this article, I will demonstrate how to create a simple CI/CD Pipeline in AWS. This is designed for people who are…

    4 条评论
  • GET REAL LIFE EXPERIENCE IN IT

    GET REAL LIFE EXPERIENCE IN IT

    For some years now, I have mentored and taught hundreds of people trying to get into Tech. One of the biggest…

  • Connecting to RDS DATABASE via SSH TUNNEL

    Connecting to RDS DATABASE via SSH TUNNEL

    Let's say you work for an E-commerce company and all your inventories are stored in a Postgres database hosted in AWS…

    12 条评论
  • How Effective Are you Working From Home?

    How Effective Are you Working From Home?

    For those of you who have always wanted to work from home but never got approved, you are probably working from home…

社区洞察

其他会员也浏览了