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: Attach ssm permissions to ecs fargate job module #407

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions aws-ecs-job-fargate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ No modules.
| [aws_ecs_service.job](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
| [aws_ecs_service.unmanaged-job](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
| [aws_ecs_task_definition.job](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
| [aws_iam_policy.task_execution_role_ssm_permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.task_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy.task_execution_role_secretsmanager](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
| [aws_iam_role_policy_attachment.task_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.task_execution_role_ssm_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_policy_document.execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.registry_secretsmanager](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.ssm_permission](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

Expand All @@ -69,6 +72,7 @@ No modules.
| <a name="input_registry_secretsmanager_arn"></a> [registry\_secretsmanager\_arn](#input\_registry\_secretsmanager\_arn) | ARN for AWS Secrets Manager secret for credentials to private registry | `string` | `null` | no |
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | Security group to use for the Fargate task. | `list(string)` | `[]` | no |
| <a name="input_service"></a> [service](#input\_service) | Service for tagging and naming. See [doc](../README.md#consistent-tagging). | `string` | n/a | yes |
| <a name="input_ssm_arn"></a> [ssm\_arn](#input\_ssm\_arn) | Parameter Store ARN | `string` | `null` | no |
| <a name="input_tag_service"></a> [tag\_service](#input\_tag\_service) | Apply cost tags to the ECS service. Only specify false for backwards compatibility with old ECS services. | `bool` | `true` | no |
| <a name="input_task_definition"></a> [task\_definition](#input\_task\_definition) | JSON to describe task. If omitted, defaults to a stub task that is expected to be managed outside of Terraform. | `string` | `null` | no |
| <a name="input_task_role_arn"></a> [task\_role\_arn](#input\_task\_role\_arn) | n/a | `string` | n/a | yes |
Expand Down
23 changes: 23 additions & 0 deletions aws-ecs-job-fargate/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,26 @@ resource "aws_iam_role_policy" "task_execution_role_secretsmanager" {
role = aws_iam_role.task_execution_role.name
policy = data.aws_iam_policy_document.registry_secretsmanager[0].json
}

data "aws_iam_policy_document" "ssm_permission" {
count = var.ssm_arn != null ? 1 : 0

statement {
actions = [
"ssm:GetParameters",
]

resources = [var.ssm_arn]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resources = [var.ssm_arn]
resources = var.ssm_parameter_store_arns

I'd make this a list so that we're granting access to specific keys in Parameter Store. Even though you can somewhat achieve the same behavior by providing an ARN prefix, I think this is much more flexible.

}
}

resource "aws_iam_policy" "task_execution_role_ssm_permission" {
count = var.ssm_arn != null ? 1 : 0
policy = data.aws_iam_policy_document.ssm_permission[0].json
}

resource "aws_iam_role_policy_attachment" "task_execution_role_ssm_attachment" {
count = var.ssm_arn != null ? 1 : 0
role = aws_iam_role.task_execution_role.name
policy_arn = aws_iam_policy.task_execution_role_ssm_permission[0].arn
}
6 changes: 6 additions & 0 deletions aws-ecs-job-fargate/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,9 @@ variable "ordered_placement_strategy" {
default = []
description = "Placement strategy for the task instances."
}

variable "ssm_arn" {
type = string
default = null
description = "Parameter Store ARN"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Parameter Store ARN"
description = "SSM Parameter Store ARN. If present, allows ECS task to make ssm:GetParameters call."

}