Deploy a NodeJS Application to AWS Lambda and automate it using Continuous Deployment using GitHub Actions

What We’ll Be Doing

In this article we’ll be automating the deployment process of your code with AWS Lambda using NodeJS using CD with GitHub Actions.You can scroll down all the way to the bottom to see the final code if you want

What is Continuous Deployment(CD)?

This Article does a great job of explaining it.

What is GitHub Actions?

For more information about GitHub Actions checkout this article

Getting Started

To deploy code you would have to use the Serverless CLI We’ll be automating this process using GitHub Actions.

const express = require('express');
const app = express();
require('dotenv').config();
app.get('/', (req, res) => { res.send('hello world');}) app.get('/secret', (req, res) => { res.send(process.env.API_KEY)}) app.listen(process.env.PORT || 3000);

Setting up GH Actions

Setting up GitHub Actions is quite simple first a folder named .github, inside that create a workflows folder and inside that folder create a yaml file with any name of your choice, we're doing deploy.yaml. It should look something like this:

Setting up Serverless

First install the Serverless CLI:

service: Article_Test
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
functions:
hello:
handler: handler.hello
const express = require('express');
const app = express();
const serverless=require('serverlesshttp');
require('dotenv').config();
app.get('/', (req, res) => { res.send('hello world');}) app.get('/secret', (req, res) => { res.send(process.env.API_KEY)}) module.exports.handler = serverless(app) app.listen(process.env.PORT || 3000);
service: Article-Test
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
functions:
app:
handler: app.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
Serverless: Stack update finished...
Service Information
service: Article-Test
stage: dev
region: us-east-1
stack: Article-Test-dev
resources: 12
api keys:
None
endpoints:
ANY - https://ob74bjk993.execute-api.us-east-1.amazonaws.com/dev ANY - https://ob74bjk993.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
functions:
app: Article-Test-dev-applayers: None ***********************************************************************************************************************Serverless: Announcing an enhanced experience for running Express.js apps: https://github.com/serverless-components/express.
on:  
push:
tags:
- 'v*.*.*'
jobs:  
serverless-deploy-production:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
steps:      
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- name: Install serverless CLI        
run: npm install -g serverless
- name: Install npm dependencies
run: npm install
- name: set environment variables
run: |
touch .env
echo "API_KEY=${{secrets.API_KEY}}" >> .env
- name: deploy
run: |
serverless config credentials --provider aws --key ${{secrets.AWS_ACCESS_KEY}} --secret ${{secrets.AWS_SECRET_ACCESS_KEY}}
serverless deploy --stage production
const express = require('express');
const app = express();
const serverless = require('serverless-http');
require('dotenv').config();

app.get('/', (req, res) => {
res.send('hello world');
})

app.get('/secret', (req, res) => {
res.send(process.env.API_KEY)
})

module.exports.handler = serverless(app)

app.listen(process.env.PORT || 3000);
service: Article-Test
frameworkVersion: '2'

provider:
name: aws
runtime: nodejs12.x

functions:
app:
handler: app.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
on:
push:
tags:
- 'v*.*.*'
jobs:
serverless-deploy-production:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- name: Install serverless CLI
run: npm install -g serverless
- name: Install npm dependencies
run: npm install
- name: set environment variables
run: |
touch .env
echo "API_KEY=${{secrets.API_KEY}}" >> .env
- name: deploy
run: |
serverless config credentials --provider aws --key ${{secrets.AWS_ACCESS_KEY_ID}} --secret ${{secrets.AWS_SECRET_ACCESS_KEY}}
serverless deploy --stage production
on:
push:
tags:
- 'v*.*.*'
jobs:
serverless-deploy-production:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
- name: Install serverless CLI
run: npm install -g serverless
- name: Install npm dependencies
run: npm install
- name: set environment variables
run: |
touch .env
echo "API_KEY=${{secrets.API_KEY}}" >> .env
- name: Configure AWS credentials for account with session token
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID}}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{secrets.AWS_SESSION_TOKEN}}
aws-region: us-east-1
- name: deploy
run: serverless deploy --stage production

Conclusion

Congratulations! You’ve (hopefully) successfully deployed your code to the cloud with Continuous Deployment using GitHub Actions!

I'm a JavaScript one trick pony developer in High School. I love coding, playing videogames, and sleeping.