Jenkins CI/CD Pipeline for AWS Infrastructure
Automated Terraform Deployment with Jenkins & Docker
📌 Project Overview This project demonstrates how to automate AWS infrastructure deployment using Jenkins running in Docker to trigger Terraform scripts. The pipeline is designed to create AWS networking resources dynamically with infrastructure as code.
🛠 Tools & Technologies Used
- Terraform (Infrastructure as Code to provision AWS networking resources)
- AWS (VPC, Subnets, Internet Gateway, Transit Gateway, Route Tables, Security Groups, and EC2)
- GitHub (Source control for Terraform scripts, integrated with Jenkins via Webhooks)
- Docker (Containerized Jenkins environment for portability and automation)
- Jenkins (Running in a Docker container locally to automate the pipeline)
- Jenkinsfile (Defines pipeline stages for infrastructure deployment)
📂 Terraform Infrastructure Setup
Now we need to copy the url of our repo in the "Code" drop down. Then we can go into VScode and remote into the repo from our local device. The command we need to run in our VSCode terminal is "git remote add origin <your_repos_url>"

Our terraform files will now be uploaded to our GitHub repo. We need to run the following commands:
"git init" (To start git on our local folder)
"git add ." (This will add all the files in our current working directory. If you don't want to add all files there are two ways to resolve this. We could use a .gitignore file
OR "git add <name_of_the_files_you_want_to_add" (Notice there is no "." after "add" this means we need to name them 1 by 1)
Next we need to run "git commit -m <Your_message_here>" (The message is one way we can keep track of the changes we make with each commit)
Lastly for this step "git push -u origin <name_of_your_branch>"

Now our local files are now in our GitHub repository!!!
Next lets set up our Jenkins instance using docker
First make sure you have docker installed and have created an acccout[https://www.docker.com]

Navigate to the "Docker Desktop" tab

Now we need to run a command in terminal to pull the docker image for Jenkins. We will run the command "docker pull jenkins/jenkins:latest"

We now have to most current image of Jenkins on our machine. We now need to to start the image as a container. Lets run the following command "docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:latest"
Key point!: " -p : " this tells docker which ports we need open for the image we are running.
Without this we will not have access to our Jenkins image. In a browser we need to type in "localhost:8080"

Perfect, we can now access our Jenkins instance. Back in docker click on the name of your container. This will take you to the logs and there you can navigate where it has your "username" and "password" We are now logged in!
Now we will set up plugins in Jenkins and AWS credentials in Jenkins
Once in Jenkins go to dashboard>>Manage Jenkins>>Plugins>>Available plugins This link will show you the plugins needed it's a long list It may take some time to install these.
Now our plugin list should look something like this to start with.

Next lets go to our AWS console

On the top right it shows our user name. Click the drop down arrow then "Security credentials" Navigate down to "Access Keys" click "create new" and copy your "access key id" and "secret id" we need these for Jenkins.
Now back in Jenkins navigate to Dashboard>>Manage Jenkins>>Credentials at the bottom of the screen "Stores scoped to Jenkins"

Click "+ Add credentials" on the right hand side.

Under the "Kind" drop down select "AWS Credentials"

ID: A name of your choosing. (Remember this name EXACTLY how you spelled it)
Description: Any description
Access Key ID: This HAS to be the "Access Key ID" we got from AWS
Secret Access Key: This HAS to be the "Secret Access Key" we got from AWS
Click create.
No we need to install and move terraform from the terminal:
In terminal/gitbash/docker-terminal run the command "docker exec -it <your_container_name> bash"
Now let's install the awscli "apt update && apt install -y awscli"
Next we need to update our packages "apt update && apt install -y curl unzip"
Next make a directory for our terraform files "mkdir -p /home/jenkins/bin"
Now we need to download the terraform Binary. Mind you this command is for Apple Silicon(ARM64) Chipset "wget https://releases.hashicorp.com/terraform/1.10.5/terraform_1.10.5_linux_arm64.zip"
For x86 you will need a different binary.
Now we need to extract and Move Terraform to the correct location with this command: "unzip terraform_1.10.5_linux_arm64.zip mv terraform /usr/local/bin/ chmod +x /usr/local/bin/terraform"
Next lets check if terraform is working with "terraform -version" We should see an output some like this "Terraform v1.10.5 on linux_arm64"
Now we will setup the pipeline and deploy to AWS!
Now back in our browser we need to navigate to the "dashboard" and click "new item"
Enter a name and select "Pipeline"
Enter your description of your choice

Scroll down to "Pipeline" under the "Definition" drop down select "Pipeline script from SCM">>Under the "SCM" drop down select "Git"
Now under Repositories>>URL enter the URL of the repo we created earlier!
Under "Branches to Build">>"Branch Specifier" add the name of the branch you pushed to in Github

Make sure "Script Path is 'Jenkinsfile'" Then click save.
Looks good now let's scroll to the bottom and click "Deploy"




