tip minersTip Miners

Connect a MySQL AWS RDS from an AWS Lambda-hosted Express JS App

Connect a MySQL - AWS RDS from an AWS Lambda-hosted Express JS AppWritten by Miguel

To be able to connect to MySQL Database hosted on Amazon AWS ( AWS RDS ) from an Express JS App that is being served from an AWS Lambda function you have to run the app under the same security group of your DB VPC

Steps

  1. Create the database on AWS , To do that you need to use the service Amazon RDS : Use the standard creation method for an easy configuration experience. Use the free tier template if you don’t want to run into expenses. Save the password you set in a safe place or use AWS Secrets Manager to keep it secret. Set public access to No to keep your db at a healthy security level. If it’s the first db you set on your account, you must create a VPC and VPC security group. Under the Additional Configuration field group set up an initial database name.
  1. Go to the module where you set up the connection to your DB, and base your config on environment variables :
// get the client
const mysql = require('mysql2');
// Create the connection pool. The pool-specific settings are the defaults
const pool = mysql.createPool({
  host: process.env.MYSQL_HOST,
  user: process.env.USERNAME,
  database: process.env.DB_NAME,
  password: process.env.PASSWORD,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0,
  port: process.env.MYSQL_PORT,
  multipleStatements:true,
  
});
module.exports = pool;
  1. Now, On your serverless.yml file, you have to allow this config to be passed from your lambda function to your app process environment :
provider:
  name: aws
  region: us-east-2
  runtime: nodejs14.x
  lambdaHashingVersion: '20201221'
  environment:
        #mysql
        MYSQL_HOST: ${self:custom.MYSQL.HOST}
        MYSQL_PORT: ${self:custom.MYSQL.PORT}
        #common
        DB_NAME: ${self:custom.DB_NAME}
        USERNAME: ${self:custom.USERNAME}
        PASSWORD: ${self:custom.PASSWORD}
custom:
  DB_NAME: YOURDBNAME
  USERNAME: ADMINUSERNAME
  PASSWORD: ADMINPASSWORD
  MYSQL:
    HOST: GRAB-THE-HOST-FROM-AWS
    PORT: GRAB-THE-PORT-FROM-AWS 
  1. Same on your  serverless.yml, add your VPC config, so your lambda function runs under the same security group:
provider:
  name: aws
  ....
  ....
  vpc:
    securityGroupIds:
      - sg-################
    subnetIds:
      - subnet-################
      - subnet-################
      - subnet-################
  1. Deploy your service

👍🏼 Dos

  • Be careful of the region where you add your db, it has to be the same where you run your lambda function.
  • Add the initial DB name when creating your MySQL instance, if you don’t then do it, Amazon will not create db, it’s complicated to connect from a MySQL client.
  • Note that your VPC and security group information after you create it is on the connectivity and security tab of the details of the DB on the AWS dashboard

👎🏼 Dont’s

  • Don’t make your DB public, yes is more complicated to manage but if safer.

More articles of AWS - Amazon Web Services

to deploy your express JS app to a lambda function you need to make us of serverless framwork with serverless-http package

Deploy Express Js Application to AWS Lambda