Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 1220840

Browse files
authored
feat: run a separate task to initialize the cluster (#7)
1 parent a91c346 commit 1220840

File tree

3 files changed

+141
-8
lines changed

3 files changed

+141
-8
lines changed

files/vault_init.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
{
3+
"name": "vault-${env}",
4+
"image": "${image}",
5+
"essential": true,
6+
"memoryReservation": 10,
7+
"privileged": true,
8+
"portMappings": [
9+
{
10+
"containerPort": 8200,
11+
"hostPort": 8200,
12+
"protocol": "tcp"
13+
}
14+
],
15+
"environment": [
16+
{
17+
"name": "VAULT_LOCAL_CONFIG",
18+
"value": "{ \"backend\": {\"consul\": {\"address\": \"127.0.0.1:8500\", \"path\": \"vault\"}}, \"default_lease_ttl\": \"168h\", \"max_lease_ttl\": \"720h\", \"listener\": [{ \"tcp\": { \"address\": \"0.0.0.0:8200\", \"tls_disable\": true }}] }"
19+
},
20+
{
21+
"name": "VAULT_UI",
22+
"value": "${vault_ui}"
23+
}
24+
],
25+
"command": [
26+
"server"
27+
],
28+
"cpu": 0,
29+
"volumesFrom": [
30+
31+
],
32+
"mountPoints": [
33+
34+
],
35+
"logConfiguration": {
36+
"logDriver": "awslogs",
37+
"options": {
38+
"awslogs-group": "${awslogs_group}",
39+
"awslogs-region": "${awslogs_region}",
40+
"awslogs-stream-prefix": "${awslogs_stream_prefix}"
41+
}
42+
}
43+
},
44+
{
45+
"name": "vault-init",
46+
"image": "${image}",
47+
"memoryReservation": 10,
48+
"privileged": true,
49+
"essential": false,
50+
"environment": [
51+
{
52+
"name": "VAULT_ADDR",
53+
"value": "http://127.0.0.1:8200"
54+
}
55+
],
56+
"command": [
57+
"sh", "-c", "sleep 10; vault operator init -key-shares=3 -key-threshold=3"
58+
],
59+
"cpu": 0,
60+
"volumesFrom": [
61+
62+
],
63+
"mountPoints": [
64+
65+
],
66+
"portMappings": [
67+
68+
],
69+
"logConfiguration": {
70+
"logDriver": "awslogs",
71+
"options": {
72+
"awslogs-group": "${awslogs_group}",
73+
"awslogs-region": "${awslogs_region}",
74+
"awslogs-stream-prefix": "${awslogs_stream_prefix}"
75+
}
76+
}
77+
}
78+
]

main.tf

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,32 @@ data "template_file" "vault" {
3434
}
3535
}
3636

37+
data "template_file" "vault_init" {
38+
template = "${file("${path.module}/files/vault_init.json")}"
39+
40+
vars {
41+
datacenter = "${local.vpc_name}"
42+
env = "${var.env}"
43+
image = "${var.vault_image}"
44+
awslogs_group = "vault-${var.env}"
45+
awslogs_stream_prefix = "vault-${var.env}"
46+
awslogs_region = "${data.aws_region.current.name}"
47+
vault_ui = "${var.enable_vault_ui ? "true" : "false"}"
48+
}
49+
}
50+
3751
# End Data block
3852
# local variables
3953
locals {
40-
cluster_count = "${length(var.ecs_cluster_ids)}"
41-
vpc_name = "${data.aws_vpc.vpc.tags["Name"]}"
42-
sg_name = "tf-${local.vpc_name}-vault-uiSecurityGroup"
43-
sg_tags = "${merge(var.tags, map("Name", local.sg_name, "Environment", var.env))}"
44-
log_tags = "${merge(var.tags, map("VPC", local.vpc_name, "Application", aws_ecs_task_definition.vault.family))}"
54+
initialize = "${var.initialize ? true : false}"
55+
cluster_count = "${length(var.ecs_cluster_ids)}"
56+
vault_standalone_count = "${local.initialize ? 0 : local.cluster_count == 1 ? 1 : 0}"
57+
vault_clustered_count = "${local.initialize ? 0 : local.cluster_count > 1 ? 1 : 0}"
58+
vault_init_count = "${local.initialize ? 1 : 0}"
59+
vpc_name = "${data.aws_vpc.vpc.tags["Name"]}"
60+
sg_name = "tf-${local.vpc_name}-vault-uiSecurityGroup"
61+
sg_tags = "${merge(var.tags, map("Name", local.sg_name, "Environment", var.env))}"
62+
log_tags = "${merge(var.tags, map("VPC", local.vpc_name, "Application", aws_ecs_task_definition.vault.family))}"
4563
}
4664

4765
resource "aws_ecs_task_definition" "vault" {
@@ -57,9 +75,25 @@ resource "aws_cloudwatch_log_group" "vault" {
5775
tags = "${local.log_tags}"
5876
}
5977

78+
resource "aws_ecs_task_definition" "vault_init" {
79+
count = "${local.vault_init_count}"
80+
family = "vault-init-${var.env}"
81+
container_definitions = "${data.template_file.vault_init.rendered}"
82+
network_mode = "host"
83+
task_role_arn = "${aws_iam_role.vault_task.arn}"
84+
}
85+
86+
resource "aws_cloudwatch_log_group" "vault_init" {
87+
count = "${local.vault_init_count}"
88+
name = "${aws_ecs_task_definition.vault_init.family}"
89+
retention_in_days = "1"
90+
tags = "${local.log_tags}"
91+
}
92+
6093
# ECS Service
6194
resource "aws_ecs_service" "vault" {
62-
count = "${local.cluster_count == 1 ? 1 : 0}"
95+
/* count = "${local.cluster_count == 1 ? 1 : 0}" */
96+
count = "${local.vault_standalone_count}"
6397
name = "vault-${var.env}"
6498
cluster = "${var.ecs_cluster_ids[0]}"
6599
task_definition = "${aws_ecs_task_definition.vault.arn}"
@@ -85,7 +119,8 @@ resource "aws_ecs_service" "vault" {
85119
}
86120

87121
resource "aws_ecs_service" "vault_primary" {
88-
count = "${local.cluster_count > 1 ? 1 : 0}"
122+
/* count = "${local.cluster_count > 1 ? 1 : 0}" */
123+
count = "${local.vault_clustered_count}"
89124
name = "vault-${var.env}-primary"
90125
cluster = "${var.ecs_cluster_ids[0]}"
91126
task_definition = "${aws_ecs_task_definition.vault.arn}"
@@ -111,7 +146,8 @@ resource "aws_ecs_service" "vault_primary" {
111146
}
112147

113148
resource "aws_ecs_service" "vault_secondary" {
114-
count = "${local.cluster_count > 1 ? 1 : 0}"
149+
/* count = "${local.cluster_count > 1 ? 1 : 0}" */
150+
count = "${local.vault_clustered_count}"
115151
name = "vault-${var.env}-secondary"
116152
cluster = "${var.ecs_cluster_ids[1]}"
117153
task_definition = "${aws_ecs_task_definition.vault.arn}"
@@ -136,6 +172,19 @@ resource "aws_ecs_service" "vault_secondary" {
136172
]
137173
}
138174

175+
resource "aws_ecs_service" "vault_init" {
176+
count = "${local.vault_init_count}"
177+
name = "vault-init-${var.env}"
178+
cluster = "${var.ecs_cluster_ids[0]}"
179+
task_definition = "${aws_ecs_task_definition.vault_init.arn}"
180+
desired_count = "1"
181+
deployment_minimum_healthy_percent = "0"
182+
183+
placement_constraints {
184+
type = "distinctInstance"
185+
}
186+
}
187+
139188
# End Service
140189
# Security Groups
141190
resource "aws_security_group" "lb-vault-sg" {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,9 @@ variable "enable_vault_ui" {
7171
}
7272

7373
variable "vpc_id" {}
74+
75+
variable "initialize" {
76+
type = "string"
77+
default = "false"
78+
description = "Runs a `vault operator init` command to initialize the Vault backend. Run this once and then extract the unseal keys from the ECS task's logs."
79+
}

0 commit comments

Comments
 (0)