Skip to content

Commit b6b0ab5

Browse files
committed
Add ability to get the whole secret as a key-value pair
1 parent 6bfff9b commit b6b0ab5

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ This module uses the recommended way of passing sensitive data from SecretManage
66

77
## Usage
88

9+
### Passing specific keys to ECS task definition
910
```hcl
1011
module "secrets" {
1112
source = "exlabs/ecs-secrets-manager/aws"
1213
# We recommend pinning every module to a specific version
13-
version = "1.0.0"
14+
version = "1.1.0"
1415
name = "data-pipeline-secrets"
1516
1617
ecs_task_execution_roles = [
@@ -37,6 +38,39 @@ resource "aws_ecs_task_definition" "data_pipeline" {
3738
}
3839
```
3940

41+
### Passing the whole AWS Secret Manager secret to the ECS task as a single variable
42+
```hcl
43+
module "secrets" {
44+
source = "exlabs/ecs-secrets-manager/aws"
45+
# We recommend pinning every module to a specific version
46+
version = "1.1.0"
47+
name = "data-pipeline-secrets"
48+
49+
enable_secret_assigned_to_single_key = true
50+
51+
ecs_task_execution_roles = [
52+
"ecs-task-execution-role1",
53+
"ecs-task-execution-role2"
54+
]
55+
56+
# You can define your own key or leave it default then the key name is built based on the secret name
57+
key_names = [
58+
"YOUR_OWN_KEY"
59+
]
60+
}
61+
62+
resource "aws_ecs_task_definition" "data_pipeline" {
63+
#...
64+
65+
container_definitions = jsonencode([
66+
{
67+
secrets = module.secrets.ecs_secrets,
68+
#...
69+
}
70+
])
71+
}
72+
```
73+
4074
After `terraform apply` you have to go to the AWS Console SecretsManager dashboard, select created secret and set values by creating a key-value pair for each defined key name.
4175

4276

@@ -77,10 +111,12 @@ No modules.
77111
| <a name="input_key_names"></a> [key\_names](#input\_key\_names) | Secret names that will be injected as env variables | `list(string)` | `[]` | yes |
78112
| <a name="input_name"></a> [name](#input\_name) | AWS SecretsManager secret name | `string` | n/a | yes |
79113
| <a name="input_description"></a> [description](#input\_description) | AWS SecretsManager secret description | `string` | n/a | no |
114+
| <a name="input_enable_secret_assigned_to_single_key"></a> [enable\_secret\_assigned\_to\_single\_key](#input\_enable\_secret\_assigned\_to\_single\_key) | Enables returning the whole secret as a single key-value pair | `string` | `false` | no |
80115

81116
## Outputs
82117

83118
| Name | Description |
84119
|------|-------------|
85120
| <a name="output_ecs_secrets"></a> [ecs\_secrets](#output\_ecs\_secrets) | Secrets description to be injected in the ECS Container definition. |
121+
| <a name="output_secretsmanager_secret_arn"></a> [secretsmanager\_secret\_arn](#output\_secretsmanager\_secret\_arn) | AWS SecretsManager secret ARN |
86122
<!-- END_TF_DOCS -->

main.tf

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ resource "aws_iam_role_policy_attachment" "this" {
3434
}
3535

3636
locals {
37-
ecs_secrets = [
37+
ecs_secrets = var.enable_secret_assigned_to_single_key ? [
38+
{
39+
name = coalesce(one(var.key_names), upper(replace(replace(var.name,"/[^a-zA-Z\\d\\-_:]/","*"),"-","_")))
40+
valueFrom = aws_secretsmanager_secret.this.arn
41+
}
42+
] : [
3843
for key_name in var.key_names :{
39-
name = key_name
44+
name = key_name
4045
valueFrom = "${aws_secretsmanager_secret.this.arn}:${key_name}::"
4146
}
4247
]

outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ output "ecs_secrets" {
22
value = local.ecs_secrets
33
description = "Secrets description to be injected in the ECS Container definition."
44
}
5+
6+
output "secretsmanager_secret_arn" {
7+
value = aws_secretsmanager_secret.this.arn
8+
description = "AWS SecretsManager secret ARN"
9+
}

variables.tf

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ variable "key_names" {
2323
nullable = false
2424
default = []
2525
}
26+
27+
variable "enable_secret_assigned_to_single_key" {
28+
description = "Enables returning the whole secret as a single key-value pair"
29+
type = bool
30+
nullable = false
31+
default = false
32+
}

0 commit comments

Comments
 (0)