generator-aws-lambda-code
v1.0.8
Published
auto generates aws lambda boilerplate code
Downloads
17
Maintainers
Readme
generator-aws-lambda-code
generator-aws-lambda-code helps in generating scaffold code for AWS lambda function. The module will generate skeleton code for interacting with MongoDB cluster and performing basic CRUD operations. It has ability to generate the code both in Typescript and Javascript based on the user selection. The module uses AWS Serverless Application Model (SAM) framework to build the lambda on AWS. The lambda function will be deployed behind an APIGateway and will be scured using AWS API_Key service. The module enables you to implement Continous Integration and Continuous Deployment(CICD) using jenkins by generating Jenkinsfile . It also includes template code for unit testing using Mocha and integration testing, code coverage using istanbul.
Contents
- Installation
- Usage
- Deploy with AWS CLI
- Deploy Using Jenkins
- Running Lambda Locally
Installation
First, install Yeoman and generator-aws-lambda-code using npm (we assume you have pre-installed node.js).
npm install -g yo
npm install -g generator-aws-lambda-code
Usage
Generate your new project:
yo aws-lambda-code
Answer the questionnaire.
Note: For the question Name of the default resource/collection (please provide the singular form of collection name) , Please provide the mongo collection name on which your lambda function performs CRUD operations
Create and configure the following parameters as text strings under SSM parameter store in your AWS account
1. mongo_user
2. mongo_password
3. mongo_cluster
4. mongo_db_name
Note: Generator code has mongo_password parameter in environment variables which will get resolved during deployment phase. For the security purpose, configure mongo_password SSM parameter as KMS encrypted string instead of plain text and read the value dynamically during runtime using AWS SDK rather than putting it in environment variables.
Deploy with AWS CLI
- Build the code archive
npm run build
- Create s3 bucket for the deployment artifacts
aws s3 mb s3://<<Bucket_name>>
Install SAM local. For instructions please follow the link
Run the following commands
sam package --template-file sam.yml --output-template-file ./<<Output-Template-File-Name.yml>> --s3-bucket <<Bucket_name>>
sam deploy --template-file <<Output-Template-File-Name.yml>> --stack-name <<Aws-Stack-Name>> --capabilities CAPABILITY_IAM
Deploy Using Jenkins
Install nodejs in jenkins Manage Jenkins -> Manage Plugins -> Available Tab -> select NodeJs and install
Configure nodejs version globally in jenkins Manage Jenkins -> Global Tool Configuration -> NodeJS -> click Add NodeJS
Install CloudBees AWS Credentials and AWS SAM plugins in jenkins Manage Jenkins -> Manage Plugins -> Available Tab. Search for CloudBees AWS Credentials and AWS SAM and install them.
Configure AWS access key and secret key in jenkins
Create a pipeline job and provide git repository details and credentials to connect to git.
Open the pipeline job and click build now. This should create your lambda function and other related resources in your aws account
Running the lambda using SAM Local
We assume you have installed AWS SAM Local on to your commputer.
npm run build
sam local start-api -t ./sam.yml
License
MIT © nagakedari