Skip to content

Commit 6bfff9b

Browse files
committed
Ability to assign more than one ECS task
1 parent ebaae24 commit 6bfff9b

File tree

3 files changed

+58
-69
lines changed

3 files changed

+58
-69
lines changed

README.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ This module uses the recommended way of passing sensitive data from SecretManage
1010
module "secrets" {
1111
source = "exlabs/ecs-secrets-manager/aws"
1212
# We recommend pinning every module to a specific version
13-
# version = "x.x.x"
14-
name = "data-pipeline-secrets"
15-
ecs_task_execution_role = "ecs-task-execution-role"
13+
version = "1.0.0"
14+
name = "data-pipeline-secrets"
15+
16+
ecs_task_execution_roles = [
17+
"ecs-task-execution-role1",
18+
"ecs-task-execution-role2"
19+
]
1620
1721
key_names = [
1822
"STRIPE_PUBLIC_KEY",
@@ -24,14 +28,12 @@ module "secrets" {
2428
resource "aws_ecs_task_definition" "data_pipeline" {
2529
#...
2630
27-
container_definitions = <<TASK_DEFINITION
28-
[
31+
container_definitions = jsonencode([
2932
{
30-
"secrets": ${jsonencode(module.secrets.ecs_secrets)},
33+
secrets = module.secrets.ecs_secrets,
3134
#...
3235
}
33-
]
34-
TASK_DEFINITION
36+
])
3537
}
3638
```
3739

@@ -62,20 +64,19 @@ No modules.
6264

6365
| Name | Type |
6466
|------|------|
65-
| [aws_iam_policy.secrets_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
66-
| [random_id.secrets_access_policy_suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
67-
| [aws_iam_role_policy_attachment.secret_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
68-
| [aws_secretsmanager_secret.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
69-
| [aws_iam_policy_document.secrets_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
70-
| [aws_iam_role.ecs_task_execution_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_role) | data source |
67+
| [aws_iam_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
68+
| [random_id.policy_suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
69+
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
70+
| [aws_secretsmanager_secret.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
7171

7272
## Inputs
7373

7474
| Name | Description | Type | Default | Required |
7575
|------|-------------|------|---------|:--------:|
76-
| <a name="input_ecs_task_execution_role"></a> [ecs\_task\_execution\_role](#input\_ecs\_task\_execution\_role) | ECS task execution role name | `string` | n/a | yes |
77-
| <a name="input_key_names"></a> [key\_names](#input\_key\_names) | Secret names that will be injected as env variables | `list(string)` | n/a | yes |
76+
| <a name="input_ecs_task_execution_roles"></a> [ecs\_task\_execution\_roles](#input\_ecs\_task\_execution\_roles) | ECS task execution role names | `list(string)` | `[]` | yes |
77+
| <a name="input_key_names"></a> [key\_names](#input\_key\_names) | Secret names that will be injected as env variables | `list(string)` | `[]` | yes |
7878
| <a name="input_name"></a> [name](#input\_name) | AWS SecretsManager secret name | `string` | n/a | yes |
79+
| <a name="input_description"></a> [description](#input\_description) | AWS SecretsManager secret description | `string` | n/a | no |
7980

8081
## Outputs
8182

main.tf

+25-48
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,43 @@
1-
data "aws_iam_role" "ecs_task_execution_role" {
2-
name = var.ecs_task_execution_role
1+
resource "aws_secretsmanager_secret" "this" {
2+
name = var.name
3+
description = var.description
34
}
45

5-
data aws_iam_policy_document secrets_policy {
6-
statement {
7-
effect = "Allow"
8-
principals {
9-
identifiers = [data.aws_iam_role.ecs_task_execution_role.arn]
10-
type = "AWS"
11-
}
12-
actions = [
13-
"secretsmanager:GetSecret",
14-
"secretsmanager:GetSecretValue"
15-
]
16-
resources = ["*"]
17-
}
18-
}
19-
20-
resource "aws_secretsmanager_secret" "default" {
21-
name = var.name
22-
policy = data.aws_iam_policy_document.secrets_policy.json
23-
}
24-
25-
resource "random_id" "secrets_access_policy_suffix" {
6+
resource "random_id" "policy_suffix" {
267
byte_length = 8
278
}
289

29-
resource aws_iam_policy secrets_access {
30-
name = "secrets_access_${random_id.secrets_access_policy_suffix.hex}"
10+
resource "aws_iam_policy" "this" {
11+
name = "SecretsManagerPolicyForECSTaskExecutionRole-${random_id.policy_suffix.hex}"
3112
description = "Access rights to SecretsManager Secret created by terraform-aws-ecs-secrets-manager module"
3213

33-
policy = <<-POLICY
34-
{
35-
"Version": "2012-10-17",
36-
"Statement": [
37-
{
38-
"Effect": "Allow",
39-
"Action": [
40-
"secretsmanager:GetResourcePolicy",
41-
"secretsmanager:GetSecretValue",
42-
"secretsmanager:DescribeSecret",
43-
"secretsmanager:ListSecretVersionIds"
44-
],
45-
"Resource": [
46-
"${aws_secretsmanager_secret.default.arn}"
47-
]
48-
}
49-
]
50-
}
51-
POLICY
14+
policy = jsonencode({
15+
Version = "2012-10-17"
16+
Statement = [
17+
{
18+
Effect = "Allow"
19+
Action = [
20+
"secretsmanager:GetSecretValue"
21+
]
22+
Resource = [
23+
aws_secretsmanager_secret.this.arn
24+
]
25+
}
26+
]
27+
})
5228
}
5329

54-
resource aws_iam_role_policy_attachment secret_access {
55-
role = var.ecs_task_execution_role
56-
policy_arn = aws_iam_policy.secrets_access.arn
30+
resource "aws_iam_role_policy_attachment" "this" {
31+
for_each = toset(var.ecs_task_execution_roles)
32+
role = each.value
33+
policy_arn = aws_iam_policy.this.arn
5734
}
5835

5936
locals {
6037
ecs_secrets = [
6138
for key_name in var.key_names :{
6239
name = key_name
63-
valueFrom = "${aws_secretsmanager_secret.default.arn}:${key_name}::"
40+
valueFrom = "${aws_secretsmanager_secret.this.arn}:${key_name}::"
6441
}
6542
]
6643
}

variables.tf

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
variable "name" {
22
description = "AWS SecretsManager secret name"
3-
type = string
3+
type = string
4+
nullable = false
45
}
56

6-
variable "ecs_task_execution_role" {
7-
description = "ECS task execution role name"
8-
type = string
7+
variable "description" {
8+
description = "AWS SecretsManager secret description"
9+
type = string
10+
default = null
11+
}
12+
13+
variable "ecs_task_execution_roles" {
14+
description = "ECS task execution role names that should be allowed to read secrets"
15+
type = list(string)
16+
nullable = false
17+
default = []
918
}
1019

1120
variable "key_names" {
1221
description = "Secret names that will be injected as env variables"
13-
type = list(string)
22+
type = list(string)
23+
nullable = false
24+
default = []
1425
}

0 commit comments

Comments
 (0)