This task focuses on building immutable infrastructure using Ansible, Packer, and Terraform. The goal is to create immutable server images with Packer, automate the provisioning of infrastructure using Terraform, and manage configurations using Ansible.
sudo apt update && sudo apt install -y unzip wget
sudo apt install -y ansible
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt update && sudo apt install -y terraform packer
terraform --version
packer --version
ansible --version
aws configure
Create an Ansible playbook (playbook.yml) to install required packages.
---
- name: Configure Web Server
hosts: all
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
enabled: yes
- name: Copy website content
copy:
src: index.html
dest: /var/www/html/index.html
Create a file named packer-template.json.
{
"variables": {
"aws_region": "us-east-1",
"ami_name": "immutable-webserver"
},
"builders": [{
"type": "amazon-ebs",
"region": "{{user `aws_region`}}",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": ["099720109477"],
"most_recent": true
},
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "{{user `ami_name`}}-{{timestamp}}"
}],
"provisioners": [{
"type": "ansible",
"playbook_file": "playbook.yml"
}]
}
Run the following command:
packer init .
packer build packer-template.json
This will create an AMI ID, which will be used in Terraform.
Create a Terraform configuration file (main.tf).
provider "aws" {
region = "us-east-1"
}
variable "ami_id" {}
resource "aws_instance" "webserver" {
ami = var.ami_id
instance_type = "t2.micro"
tags = {
Name = "Immutable-Web-Server"
}
}
output "instance_ip" {
value = aws_instance.webserver.public_ip
}
terraform init
terraform apply -var="ami_id=ami-xxxxxx"
(Replace ami-xxxxxx with your Packer-generated AMI ID)
(Optional) Use Jenkins to trigger Packer and Terraform workflows.
pipeline {
agent any
stages {
stage('Build AMI') {
steps {
sh 'packer build packer-template.json'
}
}
stage('Deploy Infra') {
steps {
sh 'terraform apply -var="ami_id=ami-xxxxxx" -auto-approve'
}
}
}
}
Modify Terraform to use an Auto Scaling Group (ASG) and Load Balancer (ALB) for high availability.
resource "aws_launch_template" "webserver" {
name_prefix = "webserver-template"
image_id = var.ami_id
instance_type = "t2.micro"
}
resource "aws_autoscaling_group" "webserver_asg" {
desired_capacity = 2
max_size = 3
min_size = 1
vpc_zone_identifier = ["subnet-xxxxxx"]
launch_template {
id = aws_launch_template.webserver.id
version = "$Latest"
}
}
Deploy with:
terraform apply -var="ami_id=ami-xxxxxx"
This end-to-end implementation follows a truly immutable infrastructure approach, ensuring reliable, scalable, and automated deployments using Ansible, Packer, and Terraform.