# generator-aws-lambda-code [![NPM version][npm-image]][npm-url]
> **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
* <a href="#installation">Installation</a>
* <a href="#usage">Usage</a>
* <a href="#deploy-with-aws-cli">Deploy with AWS CLI</a>
* <a href="#deploy-using-jenkins">Deploy Using Jenkins</a>
* <a href="#running-the-lambda-using-sam-local">Running Lambda Locally</a>

# Installation

First, install [Yeoman](http://yeoman.io) and generator-aws-lambda-code using [npm](https://www.npmjs.com/) (we assume you have pre-installed [node.js](https://nodejs.org/)).

```bash
npm install -g yo
npm install -g generator-aws-lambda-code
```

# Usage
Generate your new project:

```bash
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
```bash
npm run build
```
* Create s3 bucket for the deployment artifacts
```bash
aws s3 mb s3://<<Bucket_name>>
```
* Install SAM local. For instructions please follow the [link](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)

* Run the following commands
```bash
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**

    ![alt text](https://kedari-git-readme-images.s3.amazonaws.com/nodejs_version_configure.png)

* 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

    ![alt text](https://kedari-git-readme-images.s3.amazonaws.com/aws_credentials_configure.png)

* Create a pipeline job and provide git repository details and credentials to connect to git.

    ![alt text](https://kedari-git-readme-images.s3.amazonaws.com/pipeline_creation.png)

    ![alt text](https://kedari-git-readme-images.s3.amazonaws.com/providing_git_repository_details.png)

* 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.

```bash
npm run build
sam local start-api -t ./sam.yml
```

## License

MIT © [nagakedari]()


[npm-image]: https://badge.fury.io/js/generator-aws-lambda-code.svg
[npm-url]: https://npmjs.org/package/generator-aws-lambda-code
[travis-image]: https://travis-ci.org/nagakedari/generator-aws-lambda-code.svg?branch=master
[travis-url]: https://travis-ci.org/nagakedari/generator-aws-lambda-code
[daviddm-image]: https://david-dm.org/nagakedari/generator-aws-lambda-code.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/nagakedari/generator-aws-lambda-code
[coveralls-image]: https://coveralls.io/repos/nagakedari/generator-aws-lambda-code/badge.svg
[coveralls-url]: https://coveralls.io/r/nagakedari/generator-aws-lambda-code

