How to set up a CI/CD Pipeline on AWS for a Node.js Express.js Project

Learn CI/CD

Sharing is caring!

128 Views -

To automate the deployment of your Node.js Express.js project from AWS CodeCommit to an EC2 instance running Nginx, you can follow these steps:

  1. Preparing your EC2 instance
  • Launch an EC2 instance with the desired configuration, such as the appropriate instance type, security group, and key pair.
  • Install Nginx on the EC2 instance. You can do this by connecting to the instance via SSH and running the necessary commands
sudo apt update

sudo apt install nginx
  1. Setting up your CodeCommit repository
  • Create a new CodeCommit repository or use an existing one.
  • Clone the repository to your local machine using the following command.
git clone <repository_url>
  1. Creating an IAM role for your CodePipeline
  • Go to the AWS Management Console and navigate to IAM.
  • Create a new IAM role with the necessary permissions to access CodeCommit, EC2, and other services involved in your pipeline.
  • Attach the appropriate policies, such as AWSCodeCommitFullAccess, AmazonEC2FullAccess, and AWSCodeDeployFullAccess, to the IAM role.
  1. Defining the buildspec.yml file
  • In the root directory of your project, create a file named buildspec.yml. This file defines the build and deployment steps for your pipeline.
  • An example buildspec.yml file for a Node.js project can look like this
version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 12
    commands:
      - npm install
  build:
    commands:
      - npm run build
  post_build:
    commands:
      - zip -r myapp.zip .
artifacts:
  files:
    - myapp.zip
  1. Creating an AWS CodePipeline
  • Go to the AWS Management Console and navigate to AWS CodePipeline.
  • Click on “Create pipeline” and provide a name for your pipeline.
  • Choose your source provider as AWS CodeCommit and select the repository you created or want to use.
  • Configure the build stage
    • Build provider: AWS CodeBuild
    • Region: Select the appropriate region.
    • Build project: Create a new CodeBuild project or choose an existing one.
  • Configure the deploy stage
    • Deployment provider: Amazon EC2
    • Region: Select the appropriate region.
    • Input artifacts: Select the output artifact from the build stage.
    • Instance type: Choose the desired EC2 instance type.
    • Instance name: Provide a name for your instance.
    • Deployment command: Enter the necessary commands to deploy your project, such as
    • sudo unzip myapp.zip -d /var/www/html
      
      sudo systemctl restart nginx

      Review your pipeline settings and click on “Create pipeline.”

  • Testing your pipeline
    • Push your code changes to the CodeCommit repository. This will trigger the pipeline
    • Monitor the pipeline execution in the AWS CodePipeline console.
    • Once the pipeline completes successfully, your Node.js Express.js project will be automatically deployed to your EC2 instance running Nginx.

Remember to adapt the instructions and commands based on your specific project setup and requirements. Additionally, ensure that you have the necessary AWS permissions and configuration in place to perform these steps.

5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Morgan Mastrangelo
Morgan Mastrangelo
4 months ago

But there is no such options like EC2 in deployment stage.