|
################################################################################ |
|
## Terraform |
|
################################################################################ |
|
|
|
terraform { |
|
required_version = "~> 0.12.6" |
|
} |
|
|
|
################################################################################ |
|
## Variables |
|
################################################################################ |
|
|
|
variable "kubernetes_worker_role" { |
|
description = "kubernetes worker role" |
|
type = string |
|
} |
|
|
|
variable "region" { |
|
description = "aws region" |
|
type = string |
|
} |
|
|
|
variable "teams" { |
|
description = "map of teams by name" |
|
type = map(object({ |
|
name = string, |
|
iam_policies = object({ |
|
managed = map(string), |
|
custom = map(list(object({ |
|
actions = list(string), |
|
effect = string, |
|
resources = list(string), |
|
conditions = list(object({ |
|
test = string, |
|
variable = string, |
|
value = list(any), |
|
})), |
|
}))), |
|
}), |
|
})) |
|
} |
|
|
|
################################################################################ |
|
## Constants |
|
################################################################################ |
|
|
|
locals { |
|
policies_custom = flatten([ |
|
for name, team in var.teams : [ |
|
for alias, statements in team.iam_policies.custom : { |
|
id = "${name}-${alias}" |
|
team = name, |
|
alias = alias, |
|
statements = statements |
|
} |
|
] |
|
]) |
|
|
|
policies_managed = flatten([ |
|
for name, team in var.teams : [ |
|
for alias, arn in team.iam_policies.managed : { |
|
id = "${name}-${alias}" |
|
team = name, |
|
alias = alias, |
|
arn = arn |
|
} |
|
] |
|
]) |
|
|
|
tags = { |
|
BillingGroup = "v7" |
|
component = "platform-argo" |
|
terraform = "true" |
|
workspace = terraform.workspace |
|
} |
|
} |
|
|
|
################################################################################ |
|
## Providers |
|
################################################################################ |
|
|
|
provider "aws" { |
|
region = var.region |
|
} |
|
|
|
################################################################################ |
|
## Resources |
|
################################################################################ |
|
|
|
# define trust policy |
|
data "aws_iam_policy_document" "argo_trust_policy" { |
|
statement { |
|
actions = ["sts:AssumeRole"] |
|
effect = "Allow" |
|
principals { |
|
type = "AWS" |
|
identifiers = [var.kubernetes_worker_role] |
|
} |
|
principals { |
|
type = "Service" |
|
identifiers = ["ec2.amazonaws.com"] |
|
} |
|
} |
|
} |
|
|
|
# provision argo role for each team |
|
resource "aws_iam_role" "argo" { |
|
for_each = var.teams |
|
name = "argo-${each.value.name}" |
|
description = "argo workflow role for ${each.value.name} team" |
|
assume_role_policy = data.aws_iam_policy_document.argo_trust_policy.json |
|
tags = local.tags |
|
} |
|
|
|
# provision custom argo policies |
|
resource "aws_iam_policy" "argo_custom" { |
|
for_each = { |
|
for policy in local.policies_custom : policy.id => policy |
|
} |
|
|
|
name = "argo-${each.value.id}" |
|
description = "argo role policy for ${each.value.team} team" |
|
policy = data.aws_iam_policy_document.argo_custom[each.value.id].json |
|
} |
|
|
|
# define custom argo policies |
|
data "aws_iam_policy_document" "argo_custom" { |
|
for_each = { |
|
for policy in local.policies_custom : policy.id => policy |
|
} |
|
|
|
dynamic "statement" { |
|
for_each = each.value.statements |
|
|
|
content { |
|
actions = statement.value.actions |
|
effect = statement.value.effect |
|
resources = statement.value.resources |
|
|
|
dynamic "condition" { |
|
for_each = statement.value.conditions |
|
|
|
content { |
|
test = condition.value.test |
|
variable = condition.value.variable |
|
values = condition.value.values |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
# attach custom argo policies to argo role |
|
resource "aws_iam_role_policy_attachment" "argo_custom" { |
|
for_each = { |
|
for policy in local.policies_custom : policy.id => policy |
|
} |
|
|
|
role = aws_iam_role.argo[each.value.team].id |
|
policy_arn = aws_iam_policy.argo_custom[each.value.id].arn |
|
} |
|
|
|
# attach managed argo policies to argo role |
|
resource "aws_iam_role_policy_attachment" "argo_managed" { |
|
for_each = { |
|
for policy in local.policies_managed : policy.id => policy |
|
} |
|
|
|
role = aws_iam_role.argo[each.value.team].id |
|
policy_arn = each.value.arn |
|
} |
|
|
|
|
|
################################################################################ |
|
## Outputs |
|
################################################################################ |
|
|
|
output "roles" { |
|
value = { |
|
for team in var.teams : team.name => aws_iam_role.argo[team.name] |
|
} |
|
} |