data "aws_iam_policy_document" "fleet-execution" { // allow fleet application to obtain the database password from secrets manager statement { effect = "Allow" actions = ["secretsmanager:GetSecretValue"] resources = concat(var.fleet_config.database.password_secret_arn, values(var.fleet_config.extra_secrets)) } } data "aws_iam_policy_document" "fleet" { statement { effect = "Allow" actions = ["cloudwatch:PutMetricData"] resources = ["*"] } statement { effect = "Allow" actions = [ "elasticfilesystem:ClientMount", "elasticfilesystem:ClientWrite", "elasticfilesystem:ClientRead", ] resources = [aws_efs_file_system.vuln.arn] } } data "aws_iam_policy_document" "assume_events" { statement { effect = "Allow" actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["events.amazonaws.com", "ecs-tasks.amazonaws.com"] } } } data "aws_iam_policy_document" "cloudwatch_task" { statement { effect = "Allow" actions = ["iam:PassRole"] resources = ["*"] } statement { effect = "Allow" actions = ["ecs:RunTask"] resources = ["*"] condition { test = "ArnEquals" variable = "ecs:cluster" values = [var.ecs_cluster] } } } resource "aws_iam_role" "main" { count = var.fleet_config.iam_role_arn == null ? 1 : 0 name = var.fleet_config.iam.role.name description = "IAM role that Fleet application assumes when running in ECS" assume_role_policy = data.aws_iam_policy_document.assume_events.json } resource "aws_iam_policy" "main" { count = var.fleet_config.iam_role_arn == null ? 1 : 0 name = var.fleet_config.iam.role.policy_name description = "IAM policy that Fleet application uses to define access to AWS resources" policy = data.aws_iam_policy_document.fleet.json } resource "aws_iam_role_policy_attachment" "main" { count = var.fleet_config.iam_role_arn == null ? 1 : 0 policy_arn = aws_iam_policy.main[0].arn role = aws_iam_role.main[0].name } resource "aws_iam_role_policy_attachment" "extras" { for_each = toset(var.fleet_config.extra_iam_policies) policy_arn = each.value role = aws_iam_role.main[0].name } resource "aws_iam_role_policy_attachment" "execution_extras" { for_each = toset(var.fleet_config.extra_execution_iam_policies) policy_arn = each.value role = aws_iam_role.execution.name } resource "aws_iam_policy" "execution" { name = var.fleet_config.iam.execution.policy_name description = "IAM policy that Fleet application uses to define access to AWS resources" policy = data.aws_iam_policy_document.fleet-execution.json } resource "aws_iam_role_policy_attachment" "execution" { policy_arn = aws_iam_policy.execution.arn role = aws_iam_role.execution.name } resource "aws_iam_role" "run_cloudwatch" { name_prefix = "${var.customer_prefix}-cloudwatch-run" assume_role_policy = data.aws_iam_policy_document.assume_events.json } resource "aws_iam_policy" "run_cloudwatch" { name_prefix = "${var.customer_prefix}-cloudwatch-run" policy = data.aws_iam_policy_document.cloudwatch_task.json } resource "aws_iam_role_policy_attachment" "run_cloudwatch" { role = aws_iam_role.run_cloudwatch.name policy_arn = aws_iam_policy.run_cloudwatch.arn } resource "aws_iam_role_policy_attachment" "ecs_role_attachment" { role = aws_iam_role.execution.name policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole" } resource "aws_iam_role_policy_attachment" "ecs_task" { policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceEventsRole" role = aws_iam_role.execution.name } resource "aws_iam_role" "execution" { name = var.fleet_config.iam.execution.name description = "The execution role for Fleet in ECS" assume_role_policy = data.aws_iam_policy_document.assume_events.json }