How to use AWS secret manager and SES with AWS SAM!!!!
Build a proxy rest service with Lambda Node.js and integrating Simple Email Service, Secret Manager Service and much more….
AWS Serverless Application Model (AWS SAM) is an open source framework for building serverless application using Infrastructure as code (IaC). It provides developers declare AWS CloudFormation resources or SAM specific resource, property reference and SAM transforms and expands the SAM syntax into AWS CloudFormation
syntax, enabling you to build Serverless Applications faster.
In this post, we will use AWS SAM to build a rest service that calls Weather API to get the current weather data by zipcode, country code and we will store the API token in secret manager and email the current weather data once the rest API is called.
Problem Statement:
We will be creating a Serverless proxy Rest API endpoint using AWS SAM to get the current weather data by zip code and country code with the following details :
- Method: GET
- Query Parameters: zip code and country
- SAM policy templates to scope the permissions of the Lambda functions to the Secret Manager and Simple Email Service resources.
Here is the complete GITHUB project :
https://github.com/SandeepKumarYaramchitti/aws-nodejs-rest-service
Step 1: Create a SAM project
Create the SAM project with some boilerplate code or clone my git repo.Select Nodejs14 as run time and follow the site here for details steps to install AWS SAM cli
Step 2: lets begin to code
Once you generated the project, it will have little bit of bolierplate code and you can start your development from here on. Here is a snippet for the generated code.
function stats (file) {
return new Promise((resolve, reject) => {
fs.stat(file, (err, data) => {
if (err) {
return reject (err)
}
resolve(data)
})
})
}
Promise.all([
stats('file1'),
stats('file2'),
stats('file3')
])
.then((data) => console.log(data))
.catch((err) => console.log(err))