mirror of
https://github.com/ysoftdevs/terraform-aws-eks.git
synced 2026-01-17 17:22:13 +01:00
174 lines
5.3 KiB
HCL
174 lines
5.3 KiB
HCL
data "aws_partition" "current" {}
|
|
data "aws_caller_identity" "current" {}
|
|
data "aws_region" "current" {}
|
|
|
|
locals {
|
|
create_iam_role = var.create && var.create_iam_role
|
|
|
|
iam_role_name = coalesce(var.iam_role_name, var.name, "fargate-profile")
|
|
iam_role_policy_prefix = "arn:${data.aws_partition.current.partition}:iam::aws:policy"
|
|
|
|
ipv4_cni_policy = { for k, v in {
|
|
AmazonEKS_CNI_Policy = "${local.iam_role_policy_prefix}/AmazonEKS_CNI_Policy"
|
|
} : k => v if var.iam_role_attach_cni_policy && var.cluster_ip_family == "ipv4" }
|
|
ipv6_cni_policy = { for k, v in {
|
|
AmazonEKS_CNI_IPv6_Policy = "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:policy/AmazonEKS_CNI_IPv6_Policy"
|
|
} : k => v if var.iam_role_attach_cni_policy && var.cluster_ip_family == "ipv6" }
|
|
}
|
|
|
|
################################################################################
|
|
# IAM Role
|
|
################################################################################
|
|
|
|
data "aws_iam_policy_document" "assume_role_policy" {
|
|
count = local.create_iam_role ? 1 : 0
|
|
|
|
statement {
|
|
effect = "Allow"
|
|
actions = ["sts:AssumeRole"]
|
|
|
|
principals {
|
|
type = "Service"
|
|
identifiers = ["eks-fargate-pods.amazonaws.com"]
|
|
}
|
|
|
|
condition {
|
|
test = "ArnLike"
|
|
variable = "aws:SourceArn"
|
|
|
|
values = [
|
|
"arn:${data.aws_partition.current.partition}:eks:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:fargateprofile/${var.cluster_name}/*",
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_iam_role" "this" {
|
|
count = local.create_iam_role ? 1 : 0
|
|
|
|
name = var.iam_role_use_name_prefix ? null : local.iam_role_name
|
|
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
|
|
path = var.iam_role_path
|
|
description = var.iam_role_description
|
|
|
|
assume_role_policy = data.aws_iam_policy_document.assume_role_policy[0].json
|
|
permissions_boundary = var.iam_role_permissions_boundary
|
|
force_detach_policies = true
|
|
|
|
tags = merge(var.tags, var.iam_role_tags)
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "this" {
|
|
for_each = { for k, v in merge(
|
|
{
|
|
AmazonEKSFargatePodExecutionRolePolicy = "${local.iam_role_policy_prefix}/AmazonEKSFargatePodExecutionRolePolicy"
|
|
},
|
|
local.ipv4_cni_policy,
|
|
local.ipv6_cni_policy
|
|
) : k => v if local.create_iam_role }
|
|
|
|
policy_arn = each.value
|
|
role = aws_iam_role.this[0].name
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "additional" {
|
|
for_each = { for k, v in var.iam_role_additional_policies : k => v if local.create_iam_role }
|
|
|
|
policy_arn = each.value
|
|
role = aws_iam_role.this[0].name
|
|
}
|
|
|
|
################################################################################
|
|
# IAM Role Policy
|
|
################################################################################
|
|
|
|
locals {
|
|
create_iam_role_policy = local.create_iam_role && var.create_iam_role_policy && length(var.iam_role_policy_statements) > 0
|
|
}
|
|
|
|
data "aws_iam_policy_document" "role" {
|
|
count = local.create_iam_role_policy ? 1 : 0
|
|
|
|
dynamic "statement" {
|
|
for_each = var.iam_role_policy_statements
|
|
|
|
content {
|
|
sid = try(statement.value.sid, null)
|
|
actions = try(statement.value.actions, null)
|
|
not_actions = try(statement.value.not_actions, null)
|
|
effect = try(statement.value.effect, null)
|
|
resources = try(statement.value.resources, null)
|
|
not_resources = try(statement.value.not_resources, null)
|
|
|
|
dynamic "principals" {
|
|
for_each = try(statement.value.principals, [])
|
|
|
|
content {
|
|
type = principals.value.type
|
|
identifiers = principals.value.identifiers
|
|
}
|
|
}
|
|
|
|
dynamic "not_principals" {
|
|
for_each = try(statement.value.not_principals, [])
|
|
|
|
content {
|
|
type = not_principals.value.type
|
|
identifiers = not_principals.value.identifiers
|
|
}
|
|
}
|
|
|
|
dynamic "condition" {
|
|
for_each = try(statement.value.conditions, [])
|
|
|
|
content {
|
|
test = condition.value.test
|
|
values = condition.value.values
|
|
variable = condition.value.variable
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_iam_role_policy" "this" {
|
|
count = local.create_iam_role_policy ? 1 : 0
|
|
|
|
name = var.iam_role_use_name_prefix ? null : local.iam_role_name
|
|
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
|
|
policy = data.aws_iam_policy_document.role[0].json
|
|
role = aws_iam_role.this[0].id
|
|
}
|
|
|
|
################################################################################
|
|
# Fargate Profile
|
|
################################################################################
|
|
|
|
resource "aws_eks_fargate_profile" "this" {
|
|
count = var.create ? 1 : 0
|
|
|
|
cluster_name = var.cluster_name
|
|
fargate_profile_name = var.name
|
|
pod_execution_role_arn = var.create_iam_role ? aws_iam_role.this[0].arn : var.iam_role_arn
|
|
subnet_ids = var.subnet_ids
|
|
|
|
dynamic "selector" {
|
|
for_each = var.selectors
|
|
|
|
content {
|
|
namespace = selector.value.namespace
|
|
labels = lookup(selector.value, "labels", {})
|
|
}
|
|
}
|
|
|
|
dynamic "timeouts" {
|
|
for_each = [var.timeouts]
|
|
content {
|
|
create = lookup(var.timeouts, "create", null)
|
|
delete = lookup(var.timeouts, "delete", null)
|
|
}
|
|
}
|
|
|
|
tags = var.tags
|
|
}
|