Overcoming Challenges in EC2 Setup and CI/CD Pipeline with Windows CMD/PowerShell

Recently, I found myself in a situation where I couldn't use my usual Linux terminal or WSL environment and I had no choice but to rely on Windows CMD and PowerShell to complete a series of tasks for setting up an EC2 instance and configuring a CI/CD pipeline. This experience was challenging, but it provided valuable insights into managing AWS, as well as my frustrations, Git operations, Docker and Docker Hub by using different tools.

1. Setting Up the EC2 Instance Manually

Without access to a Linux terminal, I had to set up my EC2 instance manually using AWS Management Console and command-line tools in PowerShell.

- Generating a Key Pair: Instead of using ssh-keygen in Linux, I had to create a key pair directly from the AWS Console and download the .pem file.

- Launching the EC2 Instance: I launched an EC2 instance using the AWS Console, specifying the key pair I just created.

2. SSH into the EC2 Instance

SSH-ing into the EC2 instance from Windows required using PowerShell with the following command:

```bash

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

```

3. Setting Up the EC2 Environment with Bash Script

After logging in, I used a bash script to install necessary packages and set up the environment. Here’s how I executed the bash script from within PowerShell:

```bash

bash your-setup-script.sh

```

4. Installing Git and Configuring User Details

Installing Git on the EC2 instance was straightforward:

```bash

sudo apt-get update

sudo apt-get install git

```

Next, I configured my Git username and email:

```bash

git config --global user.name "Your Name"

git config --global user.email "your.email@example.com"

```

5. Cloning the Repository and Making Code Changes

I cloned the repository directly onto the EC2 instance:

```bash

git clone https://github.com/yourusername/your-repo.git

```

After making the necessary code changes, I initialized the code directory for Git:

```bash

git init

```

6. Setting Up a New GitHub Repository and Pushing Code

Since I was working with a new repository, I had to set it up and push the changes:

```bash

git remote add origin https://github.com/yourusername/new-repo.git

git add .

git commit -m "Initial commit"

git push -u origin main

```

7. Building the CI/CD Pipeline with Docker

With the code pushed, I moved on to configuring the CI/CD pipeline. Since I needed to build Docker images conditionally based on whether the code changed in the backend or frontend, I ran into some challenges. Additionally, due to new security measures, I had to use a GitHub token instead of a password for authentication.

- Docker Login: I used the Docker Hub username and token for authentication:

```bash

docker login -u ${{ secrets.DOCKER_NAME }} -p ${{ secrets.DOCKER_TOKEN }}

```

- Conditionally Building Images: I worked on setting up the CI/CD pipeline to build and push Docker images only when changes were made in the respective directories.

This experience highlighted the importance of adaptability and the ability to use different tools effectively. While working without my usual Linux terminal was initially frustrating, it ultimately provided a deeper understanding of the flexibility offered by CMD/PowerShell and how to manage cloud infrastructure from a Windows environment.

Switching between different tools and environments is part of the reality of DevOps work, and being comfortable with these transitions can make a significant difference in productivity and problem-solving.