This repository contains Terraform configurations to automate the deployment of a Jenkins server on AWS. It provisions infrastructure such as VPC, EC2 instances, S3 backend for state management, and Route 53 for domain configuration.
👉 GitHub Repository: https://github.com/tukue/terraform-jenkins-aws
- Jenkins Deployment: Automates the setup of a Jenkins server on an EC2 instance.
- S3 Backend: Stores Terraform state files securely in an S3 bucket.
- Networking: Configures VPC, subnets, and security groups.
- Load Balancer: Sets up an Application Load Balancer (ALB) for traffic routing.
- Domain Management: Integrates with Route 53 for DNS and SSL certificate management.
- AWS account with necessary permissions.
- Terraform installed on your local machine.
- SSH key pair for accessing the EC2 instance.
This project uses an S3 bucket as the backend to store Terraform state files securely. Each environment (dev
, QA
, production
) has its own backend configuration file.
backend-config-dev.hcl
: Backend configuration for thedev
environment.backend-config-qa.hcl
: Backend configuration for theQA
environment.backend-config-prod.hcl
: Backend configuration for theproduction
environment.
To initialize the backend for a specific environment, use the -backend-config
flag with the terraform init
command.
terraform init -backend-config="backend-config-dev.hcl"
terraform init -backend-config="backend-config-qa.hcl"
terraform init -backend-config="backend-config-prod.hcl"
-
Clone the repository:
git clone https://github.com/tukue/terraform-jenkins-aws.git cd terraform-jenkins-aws
-
Create and switch to a workspace: Terraform workspaces allow you to manage multiple environments (e.g.,
dev
,QA
,production
) using the same configuration.-
Create a workspace for
dev
:terraform workspace new dev
-
Create a workspace for
QA
:terraform workspace new QA
-
Create a workspace for
production
:terraform workspace new production
-
Switch between workspaces:
terraform workspace select <workspace-name>
-
-
Use environment-specific
.tfvars
files: Each environment has its own.tfvars
file to manage configurations. Use the-var-file
flag to specify the appropriate file when running Terraform commands.-
For
dev
:terraform plan -var-file="terraform.tfvars" terraform apply -var-file="terraform.tfvars"
-
For
QA
:terraform plan -var-file="terraform.qa.tfvars" terraform apply -var-file="terraform.qa.tfvars"
-
For
production
:terraform plan -var-file="terraform.prod.tfvars" terraform apply -var-file="terraform.prod.tfvars"
-
-
Plan the infrastructure:
terraform plan -var-file="terraform.<env>.tfvars"
-
Apply the configuration:
terraform apply -var-file="terraform.<env>.tfvars"
Replace
<env>
withtfvars
,qa.tfvars
, orprod.tfvars
based on the environment.
After applying the configuration, Terraform will output the following:
- Jenkins EC2 instance public IP.
- Load balancer DNS name.
- Hosted zone ID.
- ACM certificate ARN.
- Ensure that
.tfvars
files are added to.gitignore
to avoid committing sensitive data. - Use the
terraform.tfvars
file fordev
,terraform.qa.tfvars
forQA
, andterraform.prod.tfvars
forproduction
environments. - Add
backend-config-*.hcl
files to.gitignore
to avoid committing backend configuration files.
# Ignore backend configuration files
backend-config-*.hcl
This project uses Ansible to configure the Jenkins server after provisioning the infrastructure with Terraform. The Ansible playbook installs necessary tools like Docker, Git, and Jenkins on the EC2 instance.
-
Install Ansible: Ensure that Ansible is installed on your local machine. If not, install it using the following command:
sudo apt update sudo apt install ansible -y
-
Install Required Python Libraries: Install the boto3 and botocore Python libraries, which are required for the AWS EC2 dynamic inventory plugin:
pip install boto3 botocore
- Configure the Dynamic Inventory: The dynamic inventory is configured in the file ansible/inventory/aws_ec2.yml. Below is the configuration:
plugin: aws_ec2 regions:
- aws-region filters: tag:Name: ec2-instance-tag-name keyed_groups:
- key: tags.Name prefix: tag_Name_ compose: ansible_host: public_ip_address
plugin: aws_ec2: Enables the AWS EC2 dynamic inventory plugin. regions: Specifies the AWS region to query (e.g., eu-north-1). filters: Filters EC2 instances based on the Name tag (e.g., Jenkins:Ubuntu-Linux-EC2). compose: Ensures Ansible uses the public IP address for SSH connections.
- Update the Ansible Configuration: Ensure the ansible.cfg file is configured to use the dynamic inventory and the correct SSH key: [defaults] inventory = ./inventory/aws_ec2.yml host_key_checking = False remote_user = ubuntu private_key_file = ssh key
[inventory] enable_plugins = aws_ec2
- Test the Dynamic Inventory: Verify that the dynamic inventory is working and fetching the correct EC2 instances:
ansible-inventory -i ansible/inventory/aws_ec2.yml --list
ansible -i ansible/inventory/aws_ec2.yml tag_Name__ec2_tag_name -m ping --private-key ~/.ssh/ssh-private-key --user ubuntu
- Run the Ansible Playbook: Execute the Ansible playbook to configure Jenkins and other tools on the EC2 instance
ansible-playbook -i ansible/inventory/aws_ec2.yml ansible/playbook/jenkins-setup.yml --private-key ~/.ssh/ssh-key --user ubuntu
+-----------------------------+ | AWS Account | +-----------------------------+ | v +-----------------------------+ | VPC | | CIDR: 10.0.0.0/16 | +-----------------------------+ | | v v +-----------+ +-----------+ | Public | | Private | | Subnets | | Subnets | | (2) | | (2) | +-----------+ +-----------+ | | v v +-----------------------------+ | Internet Gateway | +-----------------------------+ | v +-----------------------------+ | Application Load Balancer | | - HTTP (80) | | - HTTPS (443) | +-----------------------------+ | v +-----------------------------+ | Target Group | | - Port: 8080 | +-----------------------------+ | v +-----------------------------+ | Jenkins EC2 Instance | | - Public IP Enabled | | - Security Groups: | | - SSH (22), HTTP (80), | | HTTPS (443), Jenkins | | (8080) | +-----------------------------+
+-----------------------------+ | Route 53 Hosted Zone | | - DNS Records | +-----------------------------+
+-----------------------------+ | ACM Certificate | | - SSL for HTTPS | +-----------------------------+
+-----------------------------+
| S3 Bucket |
| - Stores Terraform State |
| - Versioning Enabled |
+-----------------------------+
https://github.com/user-attachments/assets/f481888c-decf-407a-b788-1dbdbcd7bc9f