Deploying a Node.js application on AWS EC2 using CTO.ai involves setting up a continuous integration and deployment pipeline and deploying the CTO.ai workflow EC2 infrastructure. This blog provides a step-by-step guide, including code snippets and configurations, to help you through the process.

Prerequisites

  • Basic knowledge of Node.js, AWS, and CTO.ai.
  • An AWS account.
  • A GitHub account for repository hosting.
  • A CTO.ai account linked to your GitHub.
  • CTO.ai CLI

Set up the EC2 Workflow Infrastructure

The AWS EKS EC2 ASG Auto scaling Workflow is open source on GitHub, and you can install it by cloning the repository.

git clone [email protected]:workflows-sh/aws-eks-ec2-asg-cdk.git


cd aws-eks-ec2-asg-cdk

Create Secret from Settings

Back in your CTO.ai dashboard, create your secrets by selecting Settings and Secrets.

Secrets are encrypted environment variables that CTO.ai utilizes within your workflow to build and run your application and deployments.

You will create four secrets:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_ACCOUNT_NUMBER
  • GITHUB_TOKEN
  • To create your AWS SECRET KEY AND ACCESS KEY. Log into your AWS Account, select the Identity and Access Management (IAM) dashboard, create a new user, copy the Access Key ID and Secret access key, and paste it into your secret dashboard on CTO.ai.
  • Your AWS ACCOUNT NUMBER can be obtained from your User ID on the top right corner of your AWS Console.

Generate GitHub Token

Generate your GITHUB_TOKEN from Github by going to Settings → Developer settings → Personal access tokens → Generate new token on your Github profile.

  • Back in your CTO.ai Secrets dashboard create your secret key/value pairs.

  • Back in your AWS EKS EC2 ASG workflow, build and set up your infrastructure using the ops run -b . command. This will provision your AWS-EKS EC2 ASG stacks using Cloud Formation.

Book-a-Consultation_v2

View EC2 instances on AWS

Back in your AWS console, you can see your EC2 instances running. The AWS EC2 Workflow creates four instances for you:

  • dev-aws-eks-ec2-asg/dev-aws-eks-ec2-asg-asg
  • dev-aws-eks-ec2-asg/dev-aws-eks-ec2-asg-asg
  • dev-aws-eks-ec2-asg/dev-aws-eks-ec2-asg-asg

You can use any of the EC2 instance(s) to deploy your Node App.

Deploy Node App on AWS

We were able to set up our workflow infrastructure above, now we are going to deploy our Node App on the EC2 instance running above. First of all, we need to connect to the instance using our terminal or an SSH client with the command.

ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip

Replace /path/to/your-key.pem with the path to your downloaded key, and your-ec2-public-ip with the public IP of your EC2 instance.

Setting Up Node.js Environment

  • Update Packages: Once connected, update the package list:
sudo yum update -y
  • Install Node.js: Install Node.js and npm (Node.js package manager)
sudo yum install -y nodejs

Deploying Your Node.js App

Transfer your Node.js App to your EC2 instances, and install the dependencies. If your code is in a Git repository (like GitHub), you can clone the repository directly onto your EC2 instance.

  • SSH Into Your EC2 Instance: First, connect to your EC2 instance via SSH
ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip
  • Install Git: If Git isn't installed on your EC2 instance, install it:
sudo yum install git -y  # For Amazon Linux/RedHat based systems
  • Clone the Repository: Use the git clone command to clone your repository
git clone https://github.com/yourusername/your-repo-name.git

Replace the URL with the URL of your Git repository.

  • Change into the directory containing your Node.js application:
cd your-repo-name

Replace your-repo-name with the name of the cloned repository.

Install Node.js (If Not Already Installed)

  • Install Node.js on your EC2 instance:
sudo yum install -y nodejs  # For Amazon Linux/RedHat based systems

Install App Dependencies

Inside your app directory, install the necessary dependencies:

npm install

Start the Node.js Application

Start your application. If your app's entry point is app.js, use:

node app.js

Access the Application

Your Node.js application should now be running on the EC2 instance. You can access it using the public IP of the EC2 instance in your web browser:

http://your-ec2-public-ip:port

Replace your-ec2-public-ip with your instance's IP and port with the port your Node.js app is running on (commonly port 3000 for Node.js apps, unless specified otherwise).


Node.js App on AWS EC2 with CTO.ai Integration

Discover the efficiency of deploying Node.js applications on AWS EC2 with the power of CTO.ai. With our guide, enhance your deployment process today. Your journey toward a scalable app deployment starts here. Don’t wait – embrace the future of deployment now with CTO.ai Workflows!

Ready to unlock the power of CTO.ai for your team? Schedule your consultation now with one of our experts today!