Deploying a simple Lambda Function using SAM in AWS
Gabe Olokun
Building Khaime to help businesses create websites in just 5 minutes, sell products, and manage customers & payments efficiently. | Let's connect and innovate together!
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.
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
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
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
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.
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
领英推荐
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
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
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
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:
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.
Snr Azure Devops Engineer | Cloud Engineer & Snr SQL Database Administrator
3 年Thanks for sharing! Easy simple steps! ????
Solution Architect
3 年This was very informative?? Thanks! for sharing
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 ????????
2X AWS |Cloud Engineer | Devops | Security
3 年This is great