Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create service account role module for EKS #636

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
32 changes: 32 additions & 0 deletions aws-iam-service-account-eks/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
locals {
iam_path = coalesce(var.iam_path, "/${var.eks_cluster.cluster_id}/")
oidc_provider_url = replace(var.eks_cluster.cluster_oidc_issuer_url, "https://", "")
name = "${var.tags.service}-${var.tags.env}-${var.tags.project}"
}

data "aws_iam_policy_document" "assume-role" {
statement {
principals {
type = "Federated"
identifiers = [var.eks_cluster.oidc_provider_arn]
}

condition {
test = "StringLike"
variable = "${local.oidc_provider_url}:sub"
values = ["system:serviceaccount:${var.k8s_namespace}:${var.service_account_name}"]
}

actions = ["sts:AssumeRoleWithWebIdentity"]
}
}

resource "aws_iam_role" "role" {
name = local.name
description = "Service account role for ${local.name}"
assume_role_policy = data.aws_iam_policy_document.assume-role.json
path = local.iam_path
max_session_duration = var.max_session_duration
permissions_boundary = var.role_permissions_boundary_arn
}

1 change: 1 addition & 0 deletions aws-iam-service-account-eks/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package test
11 changes: 11 additions & 0 deletions aws-iam-service-account-eks/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "iam_role_name_with_path" {
value = "${substr(local.iam_path, 1, -1)}${aws_iam_role.role.name}"
}

output "iam_role" {
value = aws_iam_role.role.name
}

output "iam_role_arn" {
value = aws_iam_role.role.arn
}
9 changes: 9 additions & 0 deletions aws-iam-service-account-eks/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.14"
}
}
required_version = ">= 1.3"
}
48 changes: 48 additions & 0 deletions aws-iam-service-account-eks/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
variable "eks_cluster" {
type = object({
cluster_id : string,
cluster_arn : string,
cluster_endpoint : string,
cluster_ca : string,
cluster_oidc_issuer_url : string,
cluster_version : string,
worker_iam_role_name : string,
worker_security_group : string,
oidc_provider_arn : string,
})
description = "eks-cluster module output"
}

variable "k8s_namespace" {
description = "Kubernetes namespace that the service account is in"
type = string
}

variable "iam_path" {
type = string
default = ""
description = "IAM path for the role."
}

variable "role_permissions_boundary_arn" {
description = "Permissions boundary ARN to use for IAM role"
type = string
default = ""
}

variable "max_session_duration" {
description = "Maximum CLI/API session duration in seconds between 3600 and 43200"
type = number
default = 3600
}

variable "service_account_name" {
type = string
default = "*"
description = "Specified Service Account Name in case you want to customize it"
}

variable "tags" {
type = object({ project : string, env : string, service : string, owner : string, managedBy : string })
description = "Basic metadata about the service account"
}
Loading