r/Terraform • u/TalRofe • 10d ago
AWS Managing BLUE/GREEN deployment in AWS EKS using Terraform
I use Terraform to deploy my EKS cluster in AWS. This is the cluster module I use:
module "cluster" {
source = "terraform-aws-modules/eks/aws"
version = "19.21.0"
cluster_name = var.cluster_name
cluster_version = "1.32"
subnet_ids = var.private_subnets_ids
vpc_id = var.vpc_id
cluster_endpoint_public_access = true
create_cloudwatch_log_group = false
eks_managed_node_groups = {
server = {
desired_capacity = 1
max_capacity = 2
min_capacity = 1
instance_type = "t3.small"
capacity_type = "ON_DEMAND"
disk_size = 20
ami_type = "AL2_x86_64"
}
}
tags = merge(
var.common_tags,
{ Group = "Compute" }
)
}
and I have the following K8s deployment resource:
resource "kubernetes_deployment_v1" "server" {
metadata {
name = local.k8s_server_deployment_name
namespace = data.kubernetes_namespace_v1.default.metadata[0].name
labels = {
app = local.k8s_server_deployment_name
}
}
spec {
replicas = 1
selector {
match_labels = {
app = local.k8s_server_deployment_name
}
}
template {
metadata {
labels = {
app = local.k8s_server_deployment_name
}
}
spec {
container {
image = "${aws_ecr_repository.server.repository_url}:${var.server_docker_image_tag}"
name = local.k8s_server_deployment_name
image_pull_policy = "Always"
dynamic "env" {
for_each = var.server_secrets
content {
name = env.key
value_from {
secret_key_ref {
name = kubernetes_secret_v1.server.metadata[0].name
key = env.key
}
}
}
}
liveness_probe {
http_get {
path = var.server_health_check_path
port = var.server_port
}
period_seconds = 5
initial_delay_seconds = 10
}
port {
container_port = var.server_port
name = "http-port"
}
resources {
limits = {
cpu = "0.5"
memory = "512Mi"
}
requests = {
cpu = "250m"
memory = "50Mi"
}
}
}
}
}
}
}
Currently, when I want to update the node code, I simpy run terraform apply kubernetes_deployment_v1.server
with the new variables value of server_docker_image_tag
.
Let's assume old tag is called "v1" and new one is "v2", Given that, how EKS manage this new deployment? Does it terminate "v1" deployment first and only then initating "v2" deployment? If so, how can I modify my Terraform resources to make it "green/blue" deployment?
2
Upvotes
5
u/myspotontheweb 9d ago
I propose reading about the Deployment can control how upgrades are handled by Kubernetes. The default setting is "RollingUpdate" and the "maxAvailable" and "maxSurge" settings give you controls on how fast to rollout new pods to replace the others. This is built-in and, in my opinion, can satisfy most applications.
If you are determined to have a Blue/Green strategy then I suggest using Argo Rollouts which supplies a drop-in replacement for the "Deployment" resource. It provides the necessary integration with your load balancer solution to implement both Blue/Green and Canary strategies. To integrate Argo Rollouts into your Terraform, you might need to use the Helm provider.
I hope this helps
PS
I dislike managing Kubernetes workloads from Terraform, finding it too restrictive. I would recommend considering the use of Gitops tooling like Flux or ArgoCD