This repository contains a simple Node.js application that demonstrates how to deploy to AWS Elastic Beanstalk, which in turn deploys the application on an EC2 instance.
You need an AWS account. Sign up at AWS official website.
- Install the AWS CLI.
- Setup your IAM user by following the instructions here
- Add necessary permissions for the IAM user. Follow the instructions here. The following would be some of the permissions required.
arn:aws:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalk
arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkService
- Install and configure the AWS CLI. Instructions can be found here. Run the command to configure the CLI
$ aws configure --profile your_profile_name
AWS Access Key ID [None]: your_access_key_id
AWS Secret Access Key [None]: your_secret_access_key
Default region name [None]: ap-southeast-2
Default output format [None]: json
- To use the named profile with the AWS CLI, specify the profile using the
--profile
option in your commands
- Install the EB CLI. Instructions can be found here. You will need to also have Go through the troubleshooting guide on their repo if you face any errors.
- Configure the Elastic Beanstalk CLI. Instructions can be found here. Run the command to configure the CLI
$ eb init --profile your_profile_name
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : Europe (Ireland)
5) eu-central-1 : Europe (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ...
(default is 3): x
You have not yet set up your credentials or your credentials are incorrect.
You must provide your credentials.
(aws-access-id): your_access_key_id
(aws-secret-key): your_secret_access_key
Select an application to use
1) [ Create new Application ]
(default is 1): 1
Enter Application Name
(default is "eb"): eb-node-ec2
Application eb has been created.
Select a platform.
1) Node.js
(default is 1): 1
Do you want to set up SSH for your instances?
(y/n): y
Select a keypair.
1) [ Create new KeyPair ]
(default is 1): 1
-
Clone the repository:
git clone https://github.com/sebinbenjamin/node-aws-beanstalk-demo.git cd node-beanstalk-demo
-
Install dependencies:
npm install
-
Initialize Elastic Beanstalk in your project directory:
eb init --profile aws-demo-user Select a default region 1) us-east-1 : US East (N. Virginia) 2) us-west-1 : US West (N. California) 3) us-west-2 : US West (Oregon) 4) eu-west-1 : EU (Ireland) 5) eu-central-1 : EU (Frankfurt) 6) ap-south-1 : Asia Pacific (Mumbai) 7) ap-southeast-1 : Asia Pacific (Singapore) 8) ap-southeast-2 : Asia Pacific (Sydney)
Follow the prompts to configure your Elastic Beanstalk application. You will need to select your AWS region and provide your access credentials.
-
Create an Elastic Beanstalk environment and deploy your application:
eb create --profile your_aws_profile_name
-
Create subnets using the AWS CLI
aws ec2 create-subnet --vpc-id vpc-1-demo-fs-node --cidr-block 10.0.1.0/24 --region ap-southeast-2 aws ec2 create-subnet --vpc-id vpc-1-demo-fs-node --cidr-block 10.0.2.0/24 --region ap-southeast-2
-
Deploy the application after updating
.ebextensions/rds.config
with the subnets created.eb deploy --profile your_profile_name eb status --profile your_profile_name # use to monitor the app eb open --profile your_profile_name # use to open the app eb logs --profile your_profile_name # to view the logs
-
You can set any environment variables using
eb --profile your_profile_name setenv KEY=value
- You can also setup environment variables in
.ebextensions\environment.config
like in this sample project.
This application is a simple Node.js Express server that responds with "Hello, World!" when accessed.
When you deploy an application using Elastic Beanstalk, AWS automatically handles the deployment details of capacity provisioning, load balancing, scaling, and monitoring. Specifically, Elastic Beanstalk uses Amazon EC2 instances to run your application. Here’s what happens behind the scenes:
- Environment Creation: Elastic Beanstalk creates an environment, which includes one or more EC2 instances to host your application.
- Application Deployment: The application is deployed to the EC2 instances in the environment.
- Load Balancing and Auto Scaling: Elastic Beanstalk automatically sets up load balancing and auto-scaling to ensure your application can handle varying amounts of traffic. In this particular example, we are just setting up a single instance.
By using Elastic Beanstalk, you can focus on writing code and not worry about the underlying infrastructure, while still having full control over the AWS resources powering your application.
You can deploy your Node.js application to AWS Elastic Beanstalk using GitHub Actions. This allows you to automate the deployment process as part of your CI/CD pipeline. The following are the steps to do the same
- Refer to AWS CLI config.
- Attach the policies
AWSElasticBeanstalkFullAccess
andAmazonS3FullAccess
to the IAM user. - Generate and save the
access key
andsecret key
for this IAM user.
- In your GitHub repository, go to
Settings > Secrets and variables > Actions
, make sure you add the following secrets
AWS_ACCESS_KEY_ID: your_access_key_id
AWS_SECRET_ACCESS_KEY: your_secret_access_key
AWS_REGION: ap-southeast-2
EB_ENVIRONMENT_NAME: ec2-node-env
EB_APPLICATION_NAME: eb-node-ec2
This project also includes the setup of an RDS MySQL database instance as part of the Elastic Beanstalk environment. The database setup is automated using CloudFormation templates specified in .ebextensions
directory.
.ebextensions/network.config
- sets up the VPC, subnet, and security groups required for the environment.
.ebextensions/rds.config
- sets up the RDS MySQL database instance.
Using the Database in Your Application
The JDBC connection string for the database will be set as an environment variable DATABASE_URL
in your Elastic Beanstalk environment. This file is used to configure it .ebextensions\environment.config
. You can access this environment variable in your Node.js application to connect to the database.