Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/digipie/nodejs-mysql-cloudformation
Example for deploying a NodeJS-ExpressJS-MySQL app to Beanstalk using AWS CloudFormation.
https://github.com/digipie/nodejs-mysql-cloudformation
aws cicd cloudformation elasticbeanstalk expressjs iac mysql nodejs
Last synced: 12 days ago
JSON representation
Example for deploying a NodeJS-ExpressJS-MySQL app to Beanstalk using AWS CloudFormation.
- Host: GitHub
- URL: https://github.com/digipie/nodejs-mysql-cloudformation
- Owner: DigiPie
- License: mit
- Created: 2020-11-03T10:10:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-11-17T06:58:10.000Z (about 4 years ago)
- Last Synced: 2024-10-24T08:12:24.856Z (2 months ago)
- Topics: aws, cicd, cloudformation, elasticbeanstalk, expressjs, iac, mysql, nodejs
- Homepage:
- Size: 417 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nodejs-mysql-cloudformation
An example of how you can perform Infrastructure-As-Code (IaC) using AWS CloudFormation and Continuous-Integration/Continuous-Deployment (CI/CD) using AWS CodePipeline.
We will be deploying the NodeJS-ExpressJS-MySQL Create-Read-Update-Delete (CRUD) application at [DigiPie/nodejs-mysql-aws-beanstalk](https://github.com/DigiPie/nodejs-mysql-aws-beanstalk), by using the CloudFormation IaC templates in this repository and setting up a simple AWS CodePipeline.
## Content
- [Architecture](#architecture)
- [Getting started](#getting-started)
- [Continuous-Deployment](#continuous-deployment)
- [References](#references)## Architecture
![Architecture diagram](architecture_diagram.png "Architecture diagram")
_Figure 1: Architecture diagram created using Lucidchart. Original diagram available [here](https://lucid.app/invitations/accept/fd990180-b2a9-4089-81ce-3f4758d11c42)._
This architecture was set up with the following [architecture requirements](https://github.com/DigiPie/nodejs-mysql-cloudformation/issues/1) and [case study](https://gist.github.com/houdinisparks/b8dcd1d2b5b1179b45b0afe68351e027) in mind.
VPC stack resources
- VPC
- Public Subnet 1 and 2: Resources inside these subnets are visible to the Internet
- Private Subnet 1 and 2: Resources inside either subnets can only be reached from other resources within the VPC
- Internet Gateway: Used by resources in the Public Subnet 1 and 2 for Internet-access
- NAT Gateway(s): Used as proxies to the Internet by resources in Private Subnet 1 and 2 (one NAT Gateway per AZ for Production)
- Bastion Security Group: Used by the Bastion stack
- Database Security Group: Used by the Database stack
- ELB and App Security Groups: Use by the Elastic Beanstalk stackBastion stack resources
- Bastion Host: An EC2 instance in the Public Subnet 1 used for proxying access to resources in Private Subnet 1 and 2
- Cloudwatch Alarms: For detecting invalid SSH attempts to the BastionDatabase stack resources
- Master Database: Master RDS instance used for storing application data (Note: in a real-world scenario, use Amazon Aurora instead for better performance, which is sadly not available in free-tier)
- Read Replica: Read Replica RDS instance used for heavy read operations on the database
- Standby Database: Standby RDS instance in the other Private Subnet/Availability Zone (AZ) for redundancy sake (only for Production)
- Database Subnet Group: For association with VPC Private Subnet 1 and 2Elastic Beanstalk stack resources
- Elastic Beanstalk Application: Pulls application from S3 bucket for initialization, and from GitHub via CodePipeline subsequently
- Elastic Beanstalk Environment (can create an environment for Development and another for Production)
- Auto-Scaling Group(s): Automatic scaling based on CPU utilization (2 ASGs in both AZ for Production)
- Elastic Load Balancer: Only allows HTTP traffic; can be configured to be Network Load Balancer (Layer-4) or Application Load Balancer (Layer-7)Other services
These services are not set up by the CloudFormation templates:
- S3 Bucket:
- Used to store the initial app to be deployed to Elastic Beanstalk when the CloudFormation stack is built
- Used to store reports generated by the Batch Job
- CodePipeline Pipeline: Used to continuously deploy the updated app from a GitHub repository to Elastic Beanstalk
- Batch Job: Used for daily generation of reports which incur heavy read operations; reads from the Read Replica to mitigate impact on Master Database and the Web Application## Getting started
Pre-requisites:
- [EC2 key pair](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)
- [npm](https://www.npmjs.com/get-npm)
- [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git/)
- [MySQL Workbench](https://www.mysql.com/products/workbench/) (optional)### Setup VPC, Bastion and Database stacks
1. Clone this repository to your local disk.
1. Open the [AWS Cloudformation console](https://console.aws.amazon.com/cloudformation).
1. Click **Create stack** > **With new resources (standard)**.
1. Select **Prepare template**: 'Template is ready'.
1. Select **Template source**: 'Upload a template file'.
1. Before clicking **Create stack** at the final step for any stack you create, make sure you select **Capabilities**: 'I acknowledge that AWS CloudFormation might create IAM resources' if it appears. Otherwise your stack deployment will fail.
1. Launch the `vpc.cfn.yml` stack first and fill in the parameters accordingly. Keep track of what you fill as some of these values will be referenced later on.
1. After the VPC stack has a 'CREATE_COMPLETE' status, launch the `bastion.cfn.yml` and `db.cfn.yml` stacks. You can do so concurrently given the Bastion and Database stacks do not rely on each other; they only rely on the VPC stack.### Setup Database table
1. Connect to the main RDS database via the bastion host with a database client such as [MySQL Workbench](https://www.mysql.com/products/workbench/).
- You cannot connect to the main and read replica databases directly given they are in the VPC's private subnets. You need to connect through the bastion host in the public subnet.
- See the _Connecting to Your Instances and Database_ section of [AWS: Building a VPC with the AWS Startup Kit](https://aws.amazon.com/blogs/startups/building-a-vpc-with-the-aws-startup-kit/).
- You can find the required parameters for your database connection in the [AWS Cloudformation console](https://console.aws.amazon.com/cloudformation) > Select your Database stack > **Outputs**.
1. After connecting to the main RDS database, execute the following SQL script to setup the table used by the Web Application:```sql
CREATE DATABASE test;USE test;
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
name varchar(100) NOT NULL,
age int(3) NOT NULL,
email varchar(100) NOT NULL,
PRIMARY KEY (id)
);
```### Prepare your app for deployment
1. Clone the app repository [DigiPie/nodejs-mysql-aws-beanstalk](https://github.com/DigiPie/nodejs-mysql-aws-beanstalk) to your local disk.
1. In the app's root directory, zip the code by running the following npm command:```shell
npm run zip
```1. Open the [AWS S3 Bucket console](https://s3.console.aws.amazon.com/s3/home).
1. Click **Create bucket**.
1. Click **Create bucket** using all default settings.
1. Upload the created zip file on the bucket.
1. Note down the bucket and zip file name (e.g. 'my-bucket-evan' and 'nodejs-mysql-crud.zip' respectively). You will use them as parameters for your Elastic Beanstalk stack.### Setup Elastic Beanstalk stack
1. Return to the [AWS Cloudformation console](https://console.aws.amazon.com/cloudformation).
1. Launch the `elastic-beanstalk.cfn.yml` stack and fill in parameters as follows:- **AppS3Bucket**: Name of the S3 bucket that contains the app zip file (e.g. 'my-bucket-evan')
- **AppS3Key**: Name of the app zip file (e.g. 'nodejs-mysql-crud.zip')
- **NetworkStackName**: Name of your VPC stack
- **DatabaseStackName**: Name of your DB stack
- **SSLCertificateArn**: Optional, leave it empty for this walkthrough1. Before launching your stack, select **Capabilities**: 'I acknowledge that AWS CloudFormation might create IAM resources'.
1. After your Elastic Beanstalk stack has 'CREATE_COMPLETE' status, visit your application at `Output > EnvironmentURL`.You are done!
## Continuous Deployment
To continuously deploy your application from GitHub to Elastic Beanstalk automatically on push to `master`, you can use either AWS CodePipeline or GitHub Action. In this case, we will be using AWS CodePipeline. For GitHub Action, refer to [DigiPie/nodejs-mysql-aws-beanstalk](https://github.com/DigiPie/nodejs-mysql-aws-beanstalk) for more instructions.
1. Open the [AWS CodePipeline console](http://console.aws.amazon.com/codepipeline).
1. Click **Create pipeline**.
1. Name your Pipeline and use the auto-filled Role name (e.g. 'AWSCodePipelineServiceRole-ap-southeast-1-test').
1. Select **Source provider**: 'GitHub (Version 1)'.
1. Click **Connect to GitHub**.
1. Select the **Repository** and **Branch**.
1. Use the default detection option: 'GitHub webhooks (recommended)'.
1. Click **Skip build stage**.
1. Select **Deploy provider**: 'AWS Elastic Beanstalk'.
1. Choose the appropriate **Region**, **Application name** and **Environment name**.
1. Click **Create pipeline**.
1. Test your pipeline by pushing a commit to `master`.## References
This repository was only possible thanks to the following resources:
- Sample application: [Node.js, Express & MySQL: Simple Add, Edit, Delete, View (CRUD)](http://blog.chapagain.com.np/node-js-express-mysql-simple-add-edit-delete-view-crud/)
- Sample AWS Elastic Beanstalk project: [amazon-archives/startup-kit-nodejs](https://github.com/amazon-archives/startup-kit-nodejs)
- Sample AWS CloudFormation templates: [aws-samples/startup-kit-templates](https://github.com/aws-samples/startup-kit-templates)
- Continuous Deployment pipeline: [AWS: Set up a Continuous Deployment Pipeline](https://aws.amazon.com/getting-started/hands-on/continuous-deployment-pipeline/)