-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathsecrets-sidecar.tf
184 lines (168 loc) · 4.84 KB
/
secrets-sidecar.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* This module adds a task definition configuration for deploying your app along with
* a sidecar container that writes your secrets manager secret to an ephemeral file
* that gets bind mounted into your app container. Note that this module is
* dependent upon opting in to the secretsmanager.tf module.
*
* You can deploy this configuration using Fargate CLI:
* fargate service deploy -r x
* where "x" is the revision number that this module outputs (see terraform output)
*
* Note that if you deploy this configuration on top of your existing app,
* you will need to re-deploy your app (image and envvars) afterwards.
* If using fargate-create CLI you can run ./deploy.sh
*
*/
variable "secret_dir" {
type = string
default = "/var/secret"
description = "directory where secret is written"
}
variable "secret_sidecar_image" {
type = string
default = "quay.io/turner/secretsmanager-sidecar"
description = "sidecar container that writes the secret to a file accessible by app container"
}
locals {
secret_file = "${var.secret_dir}/${aws_secretsmanager_secret.sm_secret.name}"
logs_group = "/fargate/service/${var.app}-${var.environment}"
}
resource "aws_ecs_task_definition" "secrets_sidecar" {
family = "${var.app}-${var.environment}"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn
task_role_arn = aws_iam_role.app_role.arn
volume {
name = "secret"
}
container_definitions = <<DEFINITION
[
{
"name": "${var.container_name}",
"image": "${var.default_backend_image}",
"essential": true,
"dependsOn": [
{
"containerName": "secrets",
"condition": "SUCCESS"
}
],
"portMappings": [
{
"protocol": "tcp",
"containerPort": ${var.container_port},
"hostPort": ${var.container_port}
}
],
"environment": [
{
"name": "PORT",
"value": "${var.container_port}"
},
{
"name": "HEALTHCHECK",
"value": "${var.health_check}"
},
{
"name": "ENABLE_LOGGING",
"value": "false"
},
{
"name": "PRODUCT",
"value": "${var.app}"
},
{
"name": "ENVIRONMENT",
"value": "${var.environment}"
},
{
"name": "SECRET",
"value": "${local.secret_file}"
}
],
"mountPoints": [
{
"readOnly": true,
"containerPath": "${var.secret_dir}",
"sourceVolume": "secret"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${local.logs_group}",
"awslogs-region": "${var.region}",
"awslogs-stream-prefix": "ecs"
}
}
},
{
"name": "secrets",
"image": "${var.secret_sidecar_image}",
"essential": false,
"environment": [
{
"name": "SECRET_ID",
"value": "${aws_secretsmanager_secret.sm_secret.arn}"
},
{
"name": "SECRET_FILE",
"value": "${local.secret_file}"
}
],
"mountPoints": [
{
"readOnly": false,
"containerPath": "${var.secret_dir}",
"sourceVolume": "secret"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${local.logs_group}",
"awslogs-region": "${var.region}",
"awslogs-stream-prefix": "ecs"
}
}
}
]
DEFINITION
tags = var.tags
# avoid race condition:
#"Too many concurrent attempts to create a new revision of the specified family"
depends_on = [aws_ecs_task_definition.app]
}
data "template_file" "secrets_sidecar_deploy" {
template = <<EOF
#!/bin/bash
set -e
AWS_PROFILE=$${aws_profile}
AWS_DEFAULT_REGION=$${region}
echo "backing up running container configuration"
fargate task describe -t $${current_taskdefinition} > backup.yml
echo "deploying new sidecar configuration"
fargate service deploy -r $${sidecar_revision}
echo "re-deploying app configuration"
fargate service deploy -f backup.yml
fargate service env set -e SECRET=$${secret}
EOF
vars = {
aws_profile = var.aws_profile
region = var.region
current_taskdefinition = aws_ecs_service.app.task_definition
sidecar_revision = split(":", aws_ecs_task_definition.secrets_sidecar.arn)[6]
secret = local.secret_file
}
}
resource "local_file" "secrets_sidecar" {
filename = "secrets-sidecar-deploy.sh"
content = data.template_file.secrets_sidecar_deploy.rendered
}
# command to deploy the secrets sidecar configuration
output "deploy_secrets_sidecar" {
value = "fargate service deploy --revision ${split(":", aws_ecs_task_definition.secrets_sidecar.arn)[6]}"
}