Skip to content

Instantly share code, notes, and snippets.

@sandromello
Created July 31, 2024 17:47
Show Gist options
  • Save sandromello/52eb4fbd3c0636a49095b96469ebb871 to your computer and use it in GitHub Desktop.
Save sandromello/52eb4fbd3c0636a49095b96469ebb871 to your computer and use it in GitHub Desktop.
lb-controller.tf
provider "aws" {
region = local.region
}
data "terraform_remote_state" "eks" {
backend = "s3"
config = {
bucket = "tfstate"
key = "infra/eks"
region = "us-east-1"
}
}
locals {
cluster_name = data.terraform_remote_state.eks.outputs.cluster_name
cluster_endpoint = data.terraform_remote_state.eks.outputs.cluster_endpoint
cluster_certificate_authority_data = data.terraform_remote_state.eks.outputs.cluster_certificate_authority_data
oidc_provider_arn = data.terraform_remote_state.eks.outputs.oidc_provider_arn
region = data.terraform_remote_state.eks.outputs.region
vpc_id = data.terraform_remote_state.eks.outputs.vpc_id
}
module "lb_role" {
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
version = "5.34.0"
role_name = "${local.cluster_name}_eks_lb"
attach_load_balancer_controller_policy = true
oidc_providers = {
main = {
provider_arn = local.oidc_provider_arn
namespace_service_accounts = ["kube-system:aws-load-balancer-controller"]
}
}
}
provider "kubernetes" {
host = local.cluster_endpoint
cluster_ca_certificate = base64decode(local.cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1"
args = ["eks", "get-token", "--cluster-name", local.cluster_name]
command = "aws"
}
}
provider "helm" {
kubernetes {
host = local.cluster_endpoint
cluster_ca_certificate = base64decode(local.cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1"
args = ["eks", "get-token", "--cluster-name", local.cluster_name]
command = "aws"
}
}
}
resource "kubernetes_service_account" "service-account" {
metadata {
name = "aws-load-balancer-controller"
namespace = "kube-system"
labels = {
"app.kubernetes.io/name" = "aws-load-balancer-controller"
"app.kubernetes.io/component" = "controller"
}
annotations = {
"eks.amazonaws.com/role-arn" = module.lb_role.iam_role_arn
"eks.amazonaws.com/sts-regional-endpoints" = "true"
}
}
}
resource "helm_release" "lb" {
name = "aws-load-balancer-controller"
repository = "https://aws.github.io/eks-charts"
chart = "aws-load-balancer-controller"
version = "1.7.1"
namespace = "kube-system"
max_history = 3
depends_on = [
kubernetes_service_account.service-account
]
set {
name = "region"
value = local.region
}
set {
name = "vpcId"
value = local.vpc_id
}
set {
name = "image.repository"
value = "602401143452.dkr.ecr.eu-west-2.amazonaws.com/amazon/aws-load-balancer-controller"
}
set {
name = "serviceAccount.create"
value = "false"
}
set {
name = "serviceAccount.name"
value = "aws-load-balancer-controller"
}
set {
name = "clusterName"
value = local.cluster_name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment