Introduction

Deploying a React and Node.js project to an AWS EC2 instance can be a challenging task, especially if you are new to the process. However, with the help of Bitbucket Pipeline, the deployment process becomes much easier and automated.

Step 1: Set up an AWS EC2 Instance

The first step is to set up an AWS EC2 instance. Log in to your AWS account and navigate to the EC2 dashboard. Click on ‘Launch Instance’ and choose the desired instance type, such as t2.micro.

Once the instance is launched, make sure to note down the public IP address and the key pair used for SSH access.

Step 2: Set up Bitbucket Pipeline

Next, you need to set up Bitbucket Pipeline to automate the deployment process. Go to your Bitbucket repository and navigate to the ‘Settings’ tab. Click on ‘Pipelines’ and enable Pipelines for your repository.

Now, create a file named ‘bitbucket-pipelines.yml’ in the root directory of your project. This file will define the deployment steps and configurations.

Step 3: Configure Bitbucket Pipeline

In the ‘bitbucket-pipelines.yml’ file, you need to define the pipeline steps. Here’s an example configuration:

image: node:latest

pipelines:
  default:
    - step:
        name: Build and Deploy
        script:
          - npm install
          - npm run build
          - scp -i path/to/key.pem -r ./build/ ec2-user@:~/app
          - ssh -i path/to/key.pem ec2-user@ 'cd ~/app && npm install && pm2 start index.js'

In this configuration, we first install the project dependencies, build the React app, and then use SCP to copy the build files to the EC2 instance. Finally, we SSH into the instance and install the Node.js dependencies and start the server using PM2.

Step 4: Add SSH Key to Bitbucket

For the deployment to work, you need to add the SSH key used for accessing the EC2 instance to Bitbucket. Go to your Bitbucket repository and navigate to the ‘Settings’ tab. Click on ‘SSH keys’ and add the public key.

Step 5: Trigger Deployment

Now that everything is set up, you can trigger the deployment by pushing your changes to the Bitbucket repository. Bitbucket Pipeline will automatically run the defined steps and deploy your project to the AWS EC2 instance.

Conclusion

Deploying a React and Node.js project to an AWS EC2 instance can be simplified with the help of Bitbucket Pipeline. By following the steps outlined in this guide, you can automate the deployment process and ensure a smooth and efficient deployment of your project.

Leave A Comment