Skip to content

Commit

Permalink
Merge pull request #7 from terra-mod/ecs-iam-role-changes
Browse files Browse the repository at this point in the history
Separates execution and task role
  • Loading branch information
k-k authored Jun 30, 2020
2 parents 598fb9e + 7097c7d commit e6acfbd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 21 deletions.
83 changes: 69 additions & 14 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ resource aws_security_group_rule lb_ingress {
*/
resource aws_ecs_task_definition task {
family = var.name
task_role_arn = aws_iam_role.ecs_execution_role.arn
execution_role_arn = aws_iam_role.ecs_execution_role.arn
task_role_arn = var.use_task_role ? aws_iam_role.ecs_task_role[0].arn : null
execution_role_arn = var.use_execution_role ? aws_iam_role.ecs_execution_role[0].arn : null

cpu = var.task_cpu
memory = var.task_memory
Expand Down Expand Up @@ -245,6 +245,8 @@ resource aws_appautoscaling_policy auto_scaling {
* Generates the Role for the ECS Container
*/
resource aws_iam_role ecs_execution_role {
count = var.use_execution_role ? 1 : 0

name = "${var.cluster_name}-${var.name}-execution-role"

assume_role_policy = <<EOF
Expand All @@ -265,11 +267,13 @@ EOF
}

/**
* Generates the Policy for the ECS Container
* Generates a default Policy for the ECS Container
*/
resource aws_iam_role_policy ecs_execution_role_policy {
name = "${var.cluster_name}-${var.name}-execution-policy"
role = aws_iam_role.ecs_execution_role.id
resource aws_iam_role_policy ecs_execution_default_policy {
count = var.use_execution_role ? 1 : 0

name = "${var.cluster_name}-${var.name}-default-policy"
role = aws_iam_role.ecs_execution_role[0].id

policy = <<EOF
{
Expand All @@ -285,27 +289,78 @@ resource aws_iam_role_policy ecs_execution_role_policy {
],
"Resource": "*"
}
%{if var.cloudwatch_log_group_arn != null}
,{
]
}
EOF
}

/**
* Generates a policy for the execution role to provide access to the given Cloudwatch Log stream.
*/
resource aws_iam_role_policy ecs_execution_log_policy {
count = var.use_execution_role && var.cloudwatch_log_group_arn != null ? 1 : 0

name = "${var.cluster_name}-${var.name}-cloudwatch-log-policy"
role = aws_iam_role.ecs_execution_role[0].id

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "${var.cloudwatch_log_group_arn}"

This comment has been minimized.

Copy link
@Prophecy67

Prophecy67 Aug 18, 2020

Contributor

@kmfk This needs to be "${var.cloudwatch_log_group_arn}:*"

The action PutLogEvents and CreateLogStream work on theLogGroup, not on the LogStream. Otherwise the containers crash on creating with being unable to create them. So you need that wildcard there.

This inherently broke a set-up now as all the log-groups are unable to be written to without an additional policy in execution roles.


User: REDACTED:assumed-role/REDACTED-execution-role/CONTAINERID is not authorized to perform: logs:CreateLogStream on resource: arn:aws:logs:eu-central-1:REDACTED

}
%{endif}
]
}
EOF
}

/**
* Attach a policy to the given role to allow access to Secrets in Secrets Manager.
* Generates a Task Role for the ECS Task.
*/
resource aws_iam_role ecs_task_role {
count = var.use_task_role ? 1 : 0

name = "${var.cluster_name}-${var.name}-task-role"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

/**
* Attach a additional policies to the ECS Task Role.
*/
resource aws_iam_role_policy_attachment task_policy_attachments {
count = var.use_task_role ? length(var.task_role_policy_arns) : 0

role = aws_iam_role.ecs_task_role[0].id
policy_arn = element(var.task_role_policy_arns, count.index)
}

/**
* Attach a additional policies to the ECS Execution Role.
*/
resource aws_iam_role_policy_attachment secrets_policy_attachment {
count = length(var.secrets_policy_arns)
resource aws_iam_role_policy_attachment execution_policy_attachments {
count = var.use_execution_role ? length(var.execution_role_policy_arns) : 0

role = aws_iam_role.ecs_execution_role.id
policy_arn = element(var.secrets_policy_arns, count.index)
role = aws_iam_role.ecs_execution_role[0].id
policy_arn = element(var.execution_role_policy_arns, count.index)
}
11 changes: 8 additions & 3 deletions output.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
output ecs_iam_role_id {
description = "The ID for the IAM Role attached to the Service."
value = aws_iam_role.ecs_execution_role.id
output ecs_execution_role_id {
description = "The ID for the Task Execution Role attached to the Service."
value = var.use_execution_role ? aws_iam_role.ecs_execution_role[0].id : null
}

output ecs_task_role_id {
description = "The ID for the Task Execution Role attached to the Service."
value = var.use_task_role ? aws_iam_role.ecs_task_role[0].id : null
}

output security_group_id {
Expand Down
28 changes: 24 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ variable task_cpu {

variable task_volumes {
description = "A set of volume blocks that containers in your task may use."
type = set(object({ name: string, host_path: string }))
type = set(object({ name : string, host_path : string }))
default = []
}

Expand Down Expand Up @@ -229,13 +229,33 @@ EOT
default = 120
}

# Secrets
variable secrets_policy_arns {
description = "The ARNs of IAM Policies that grants the Service access to one or more SecretsManager Secrets."
# Policies
variable use_task_role {
description = "Whether or not a default Task Role should be created for this task. Policy ARNS can be supplied to attach policies to the generated role."
type = bool
default = true
}

variable task_role_policy_arns {
description = "The ARNs of any additional IAM Policies that should be attached to the ECS Task Role."
type = list(string)
default = []
}

variable use_execution_role {
description = "Whether or not a default Task Execution Role should be created for this task. Policy ARNS can be supplied to attach policies to the generated role."
type = bool
default = true
}

variable execution_role_policy_arns {
description = "The ARNs of any additional IAM Policies that should be attached to the ECS Execution Role."
type = list(string)
default = []
}



# ECS Auto Scaling
variable enable_auto_scaling {
description = "Whether or not to include a Target Tracking Scaling Policy. Treat as a tinyint - use `1` for true, `0` for false."
Expand Down

0 comments on commit e6acfbd

Please sign in to comment.