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

Commit 5fdc919

Browse files
authored
Tfhartmann/plat 1944 deploy to second cluster (#3)
* Lots of updates, including adding second vault cluster * removing depricated error * Formating * Removed "extra_tags" param It wasn't being used at all, using the tags map instead
1 parent 2a6583e commit 5fdc919

File tree

5 files changed

+129
-54
lines changed

5 files changed

+129
-54
lines changed

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,34 @@ Module Input Variables
5757

5858
#### Optional
5959

60-
- `vault_image` - Image to use when deploying vault, defaults to the hashicorp vault image
61-
- `desired_count` - Number of vaults that ECS should run. Defaults to 2
60+
- `vault_image` - Image to use when deploying vault, (Default: hashicorp/vault)
61+
- `cloudwatch_log_retention` - Specifies the number of days you want to retain log events in the specified log group. (Default: 30)
62+
- `desired_count` - Number of vaults that ECS should run. (Default: 2)
6263
- `hostname` - DNS Hostname for the bastion host. Defaults to ${VPC NAME}.${dns_zone} if hostname is not set
63-
- `iam_path` - IAM path, this is useful when creating resources with the same name across multiple regions. Defaults to /
64-
- `region` - AWS Region, defaults to us-east-1
64+
- `iam_path` - IAM path, this is useful when creating resources with the same name across multiple regions. (Default: / )
65+
- `lb_deregistration_delay` - The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. (Default: 300)
66+
- `service_minimum_healthy_percent` - The minimum healthy percent represents a lower limit on the number of your service's tasks that must remain in the RUNNING state during a deployment
67+
- `tags` - A map of tags to add to all resources
6568

6669
Usage
6770
-----
6871

6972
```hcl
7073
module "vault" {
71-
source = "../modules/terraform-vault"
72-
#source = "github.com/FitnessKeeper/terraform-aws-vault?ref=v0.0.1"
73-
alb_log_bucket = "rk-devops-${var.region}"
74-
vault_image = "${var.vault_image}"
75-
ecs_cluster_id = "${module.ecs_consul.cluster_id}"
76-
dns_zone = "${aws_route53_zone.region.name}"
77-
env = "${var.env}"
78-
subnets = "${module.vpc.public_subnets}"
79-
#unseal_key = "${data.aws_kms_secret.unseal_key.vault}" # pass in a list "${split(",",data.aws_kms_secret.unseal_key.vault)}"
80-
unseal_keys = "${split(",",data.aws_kms_secret.unseal_key2.vault)}"
81-
vpc_id = "${module.vpc.vpc_id}"
74+
source = "github.com/FitnessKeeper/terraform-aws-vault?ref=v0.0.1"
75+
alb_log_bucket = "rk-devops-${var.region}"
76+
vault_image = "${var.vault_image}"
77+
ecs_cluster_ids = "${module.ecs_consul.cluster_id}"
78+
dns_zone = "${aws_route53_zone.region.name}"
79+
env = "${var.env}"
80+
subnets = "${module.vpc.public_subnets}"
81+
unseal_keys = "${split(",",data.aws_kms_secret.unseal_key.vault)}"
82+
vpc_id = "${module.vpc.vpc_id}"
83+
84+
tags = {
85+
"foo" = "bar"
86+
}
87+
8288
}
8389
8490
```

alb.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ resource "aws_alb" "vault" {
88

99
tags {
1010
Environment = "${var.env}"
11-
VPC = "${data.aws_vpc.vpc.tags["Name"]}"
11+
VPC = "${local.vpc_name}"
1212
}
1313

1414
access_logs {
1515
bucket = "${var.alb_log_bucket}"
16-
prefix = "logs/elb/${data.aws_vpc.vpc.tags["Name"]}/vault"
16+
prefix = "logs/elb/${local.vpc_name}/vault"
1717
}
1818
}
1919

@@ -32,9 +32,10 @@ resource "aws_route53_record" "vault" {
3232

3333
# Create a new target group
3434
resource "aws_alb_target_group" "vault_ui" {
35-
port = 8200
36-
protocol = "HTTP"
37-
vpc_id = "${data.aws_vpc.vpc.id}"
35+
port = 8200
36+
protocol = "HTTP"
37+
deregistration_delay = "${var.lb_deregistration_delay}"
38+
vpc_id = "${data.aws_vpc.vpc.id}"
3839

3940
health_check {
4041
path = "/v1/sys/health?standbyok=true"
@@ -48,7 +49,7 @@ resource "aws_alb_target_group" "vault_ui" {
4849

4950
tags {
5051
Environment = "${var.env}"
51-
VPC = "${data.aws_vpc.vpc.tags["Name"]}"
52+
VPC = "${local.vpc_name}"
5253
}
5354
}
5455

files/vault.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"cpu": 0,
2525
"volumesFrom": [
26-
26+
2727
],
2828
"mountPoints": [
2929

main.tf

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,118 @@ data "aws_acm_certificate" "cert" {
1313
domain = "${replace(var.dns_zone, "/.$/","")}" # dirty hack to strip off trailing dot
1414
}
1515

16+
data "aws_region" "current" {}
17+
1618
data "template_file" "vault" {
1719
template = "${file("${path.module}/files/vault.json")}"
1820

1921
vars {
20-
datacenter = "${data.aws_vpc.vpc.tags["Name"]}"
22+
datacenter = "${local.vpc_name}"
2123
env = "${var.env}"
2224
image = "${var.vault_image}"
2325
unseal_key0 = "${var.unseal_keys[0]}"
2426
unseal_key1 = "${var.unseal_keys[1]}"
2527
unseal_key2 = "${var.unseal_keys[2]}"
2628
awslogs_group = "vault-${var.env}"
2729
awslogs_stream_prefix = "vault-${var.env}"
28-
awslogs_region = "${var.region}"
30+
awslogs_region = "${data.aws_region.current.name}"
2931
}
3032
}
3133

3234
# End Data block
35+
# local variables
36+
locals {
37+
cluster_count = "${length(var.ecs_cluster_ids)}"
38+
vpc_name = "${data.aws_vpc.vpc.tags["Name"]}"
39+
sg_name = "tf-${local.vpc_name}-vault-uiSecurityGroup"
40+
sg_tags = "${merge(var.tags, map("Name", local.sg_name, "Environment", var.env))}"
41+
log_tags = "${merge(var.tags, map("VPC", local.vpc_name, "Application", aws_ecs_task_definition.vault.family))}"
42+
}
3343

3444
resource "aws_ecs_task_definition" "vault" {
3545
family = "vault-${var.env}"
3646
container_definitions = "${data.template_file.vault.rendered}"
3747
network_mode = "host"
3848
task_role_arn = "${aws_iam_role.vault_task.arn}"
39-
40-
#volume {
41-
# name = "docker-sock"
42-
# host_path = "/var/run/docker.sock"
43-
#}
4449
}
4550

4651
resource "aws_cloudwatch_log_group" "vault" {
47-
name = "${aws_ecs_task_definition.vault.family}"
52+
name = "${aws_ecs_task_definition.vault.family}"
53+
retention_in_days = "${var.cloudwatch_log_retention}"
54+
tags = "${local.log_tags}"
55+
}
56+
57+
# ECS Service
58+
resource "aws_ecs_service" "vault" {
59+
count = "${local.cluster_count == 1 ? 1 : 0}"
60+
name = "vault-${var.env}"
61+
cluster = "${var.ecs_cluster_ids[0]}"
62+
task_definition = "${aws_ecs_task_definition.vault.arn}"
63+
desired_count = "${var.desired_count}"
64+
deployment_minimum_healthy_percent = "${var.service_minimum_healthy_percent}"
65+
iam_role = "${aws_iam_role.ecsServiceRole.arn}"
66+
67+
placement_constraints {
68+
type = "distinctInstance"
69+
}
4870

49-
tags {
50-
VPC = "${data.aws_vpc.vpc.tags["Name"]}"
51-
Application = "${aws_ecs_task_definition.vault.family}"
71+
load_balancer {
72+
target_group_arn = "${aws_alb_target_group.vault_ui.arn}"
73+
container_name = "vault-${var.env}"
74+
container_port = 8200
5275
}
76+
77+
depends_on = ["aws_alb_target_group.vault_ui",
78+
"aws_alb_listener.vault_https",
79+
"aws_alb.vault",
80+
"aws_iam_role.ecsServiceRole",
81+
]
5382
}
5483

55-
resource "aws_ecs_service" "vault" {
56-
name = "vault-${var.env}"
57-
cluster = "${var.ecs_cluster_id}"
58-
task_definition = "${aws_ecs_task_definition.vault.arn}"
59-
desired_count = "${var.desired_count}"
84+
resource "aws_ecs_service" "vault_primary" {
85+
count = "${local.cluster_count > 1 ? 1 : 0}"
86+
name = "vault-${var.env}-primary"
87+
cluster = "${var.ecs_cluster_ids[0]}"
88+
task_definition = "${aws_ecs_task_definition.vault.arn}"
89+
desired_count = "${var.desired_count}"
90+
deployment_minimum_healthy_percent = "${var.service_minimum_healthy_percent}"
91+
iam_role = "${aws_iam_role.ecsServiceRole.arn}"
6092

6193
placement_constraints {
6294
type = "distinctInstance"
6395
}
6496

6597
load_balancer {
6698
target_group_arn = "${aws_alb_target_group.vault_ui.arn}"
99+
container_name = "vault-${var.env}"
100+
container_port = 8200
101+
}
102+
103+
depends_on = ["aws_alb_target_group.vault_ui",
104+
"aws_alb_listener.vault_https",
105+
"aws_alb.vault",
106+
"aws_iam_role.ecsServiceRole",
107+
]
108+
}
67109

68-
#elb_name = "${aws_elb.vault.name}"
69-
container_name = "vault-${var.env}"
70-
container_port = 8200
110+
resource "aws_ecs_service" "vault_secondary" {
111+
count = "${local.cluster_count > 1 ? 1 : 0}"
112+
name = "vault-${var.env}-secondary"
113+
cluster = "${var.ecs_cluster_ids[1]}"
114+
task_definition = "${aws_ecs_task_definition.vault.arn}"
115+
desired_count = "${var.desired_count}"
116+
deployment_minimum_healthy_percent = "${var.service_minimum_healthy_percent}"
117+
iam_role = "${aws_iam_role.ecsServiceRole.arn}"
118+
119+
placement_constraints {
120+
type = "distinctInstance"
71121
}
72122

73-
iam_role = "${aws_iam_role.ecsServiceRole.arn}"
123+
load_balancer {
124+
target_group_arn = "${aws_alb_target_group.vault_ui.arn}"
125+
container_name = "vault-${var.env}"
126+
container_port = 8200
127+
}
74128

75129
depends_on = ["aws_alb_target_group.vault_ui",
76130
"aws_alb_listener.vault_https",
@@ -79,11 +133,13 @@ resource "aws_ecs_service" "vault" {
79133
]
80134
}
81135

136+
# End Service
82137
# Security Groups
83138
resource "aws_security_group" "lb-vault-sg" {
84-
name = "tf-${data.aws_vpc.vpc.tags["Name"]}-vault-uiSecurityGroup"
85-
description = "Allow Web Traffic into the ${data.aws_vpc.vpc.tags["Name"]} VPC"
139+
name = "tf-${local.vpc_name}-vault-uiSecurityGroup"
140+
description = "Allow Web Traffic into the ${local.vpc_name} VPC"
86141
vpc_id = "${data.aws_vpc.vpc.id}"
142+
tags = "${local.sg_tags}"
87143

88144
ingress {
89145
from_port = 443
@@ -98,9 +154,4 @@ resource "aws_security_group" "lb-vault-sg" {
98154
protocol = "-1"
99155
cidr_blocks = ["0.0.0.0/0"]
100156
}
101-
102-
tags {
103-
Name = "tf-${data.aws_vpc.vpc.tags["Name"]}-vault-uiSecurityGroup"
104-
Environment = "${var.env}"
105-
}
106157
}

variables.tf

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ variable "vault_image" {
77
default = "vault:latest"
88
}
99

10+
variable "cloudwatch_log_retention" {
11+
default = "30"
12+
description = "Specifies the number of days you want to retain log events in the specified log group. (defaults to 30)"
13+
}
14+
1015
variable "desired_count" {
1116
description = "Number of vaults that ECS should run."
1217
default = "2"
@@ -16,8 +21,9 @@ variable "dns_zone" {
1621
description = "Zone where the Consul UI alb will be created. This should *not* be consul.example.com"
1722
}
1823

19-
variable "ecs_cluster_id" {
20-
description = "ARN of the ECS ID"
24+
variable "ecs_cluster_ids" {
25+
type = "list"
26+
description = "List of ARNs of the ECS Cluster IDs"
2127
}
2228

2329
variable "env" {}
@@ -32,14 +38,25 @@ variable "iam_path" {
3238
description = "IAM path, this is useful when creating resources with the same name across multiple regions. Defaults to /"
3339
}
3440

41+
variable "lb_deregistration_delay" {
42+
default = "300"
43+
description = "The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. (Default: 300)"
44+
}
45+
46+
variable "service_minimum_healthy_percent" {
47+
description = "The minimum healthy percent represents a lower limit on the number of your service's tasks that must remain in the RUNNING state during a deployment (default 50)"
48+
default = "50"
49+
}
50+
3551
variable "subnets" {
3652
type = "list"
3753
description = "List of subnets used to deploy the Consul alb"
3854
}
3955

40-
variable "region" {
41-
default = "us-east-1"
42-
description = "AWS Region, defaults to us-east-1"
56+
variable "tags" {
57+
type = "map"
58+
description = "A map of tags to add to all resources"
59+
default = {}
4360
}
4461

4562
variable "unseal_keys" {

0 commit comments

Comments
 (0)