This project will implement a zero-downtime Blue-Green Deployment strategy using Terraform, AWS ALB, Auto Scaling Groups (ASG), and AWS Lambda for traffic shifting. The goal is to deploy applications with zero downtime while ensuring a smooth rollback mechanism.
Here's a high-level Terraform script for implementing the Blue-Green Deployment:
provider "aws" {
region = "us-east-1"
}
# Create ALB
resource "aws_lb" "main" {
name = "blue-green-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_sg.id]
subnets = aws_subnet.public[*].id
}
# Blue Target Group
resource "aws_lb_target_group" "blue" {
name = "blue-target-group"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
}
# Green Target Group
resource "aws_lb_target_group" "green" {
name = "green-target-group"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
}
# ALB Listener
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.main.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.blue.arn
}
}
# Auto Scaling Group - Blue
resource "aws_autoscaling_group" "blue" {
name = "blue-asg"
min_size = 1
max_size = 3
desired_capacity = 1
vpc_zone_identifier = aws_subnet.public[*].id
target_group_arns = [aws_lb_target_group.blue.arn]
launch_configuration = aws_launch_configuration.blue.id
}
# Auto Scaling Group - Green
resource "aws_autoscaling_group" "green" {
name = "green-asg"
min_size = 1
max_size = 3
desired_capacity = 1
vpc_zone_identifier = aws_subnet.public[*].id
target_group_arns = [aws_lb_target_group.green.arn]
launch_configuration = aws_launch_configuration.green.id
}
# Lambda Function to Switch Traffic
resource "aws_lambda_function" "traffic_shifter" {
filename = "lambda.zip"
function_name = "TrafficShifter"
role = aws_iam_role.lambda_role.arn
handler = "index.lambda_handler"
runtime = "python3.8"
}
This AWS Lambda function dynamically updates ALB listener rules to switch traffic.
import boto3
def lambda_handler(event, context):
elbv2 = boto3.client('elbv2')
listener_arn = "arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/blue-green-alb/1234567890abcdef"
# Fetch current rules
rules = elbv2.describe_rules(ListenerArn=listener_arn)
for rule in rules['Rules']:
if 'Conditions' in rule and 'TargetGroupArn' in rule:
new_target_group_arn = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/green-target-group/abcdef123456"
elbv2.modify_listener(
ListenerArn=listener_arn,
DefaultActions=[
{
'Type': 'forward',
'TargetGroupArn': new_target_group_arn
}
]
)
return {
'statusCode': 200,
'body': "Traffic shifted to Green environment."
}
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/example/repo.git'
}
}
stage('Deploy to Green') {
steps {
sh 'terraform apply -var environment=green -auto-approve'
}
}
stage('Test') {
steps {
sh 'curl -f http://your-alb-endpoint'
}
}
stage('Switch Traffic') {
steps {
sh 'aws lambda invoke --function-name TrafficShifter /dev/null'
}
}
}
}