-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.tf
176 lines (149 loc) · 4 KB
/
ec2.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
data "hcp_packer_image" "nomad-multi-region" {
#bucket_name = "nomad-multi-region-focal"
bucket_name = "nomad-multi-region"
channel = "latest"
cloud_provider = "aws"
region = var.region
#region = "us-east-2"
}
locals {
ami_id = var.use_hcp_packer == true ? "${data.hcp_packer_image.nomad-multi-region.cloud_image_id}" : "${data.aws_ami.nomad-mr.image_id}"
#ami_id = "${data.aws_ami.nomad-mr.image_id}"
}
data "aws_ami" "nomad-mr" {
#executable_users = ["self"]
most_recent = true
#name_regex = "^hashistack-\\d{3}"
owners = ["self","099720109477"]
filter {
name = "name"
values = ["nomad-mr-*"]
}
}
resource "random_id" "server" {
byte_length = 4
keepers = {
ami_id = local.ami_id
#"ami_id" = "${data.aws_ami.nomad-mr.image_id}"
}
}
resource "random_id" "random" {
byte_length = 4
keepers = {
"ami_id" = "${data.aws_ami.nomad-mr.image_id}"
}
}
resource "aws_instance" "worker" {
count = var.server_count
#ami = "${data.aws_ami.nomad-mr.image_id}"
ami = random_id.server.keepers.ami_id
instance_type = var.server_instance_type
key_name = var.key_name
subnet_id = var.subnet_id
#subnet_id = aws_subnet.private[count.index].id
vpc_security_group_ids = [aws_security_group.boundary_sg.id]
#TODO
associate_public_ip_address = true
tags = merge(
{
"Name" = "${var.prefix}-server-${count.index}"
},
{
"boundary" = "ssh"
}
)
root_block_device {
volume_type = "gp2"
volume_size = var.root_block_device_size
delete_on_termination = "true"
tags = {}
}
user_data = templatefile("${path.module}/modules/shared/data-scripts/user-data.sh", {
server_count = var.server_count
region = var.region
cloud_env = "aws"
worker_activation_token = boundary_worker.worker_1.controller_generated_activation_token
initial_upstreams = var.initial_upstreams
NAME = "boundary"
TYPE = "worker"
})
iam_instance_profile = aws_iam_instance_profile.instance_profile.name
metadata_options {
http_endpoint = "enabled"
instance_metadata_tags = "enabled"
}
depends_on = [
#aws_kms_key.vault
boundary_worker.worker_1
]
}
# resource "aws_kms_key" "vault" {
# description = "Vault unseal key"
# deletion_window_in_days = 10
# tags = {
# Name = "vault-kms-unseal-${var.prefix}"
# }
# }
resource "aws_iam_instance_profile" "instance_profile" {
name_prefix = "${var.prefix}-profile"
role = aws_iam_role.instance_role.name
}
resource "aws_iam_role" "instance_role" {
name_prefix = "${var.prefix}-role"
assume_role_policy = data.aws_iam_policy_document.instance_role.json
}
data "aws_iam_policy_document" "instance_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "auto_discover_cluster" {
name = "${var.prefix}-auto-discover-cluster-pol"
role = aws_iam_role.instance_role.id
policy = data.aws_iam_policy_document.auto_discover_cluster.json
}
data "aws_iam_policy_document" "auto_discover_cluster" {
statement {
effect = "Allow"
actions = [
"ec2:DescribeInstances",
"ec2:DescribeTags",
"autoscaling:DescribeAutoScalingGroups",
"kms:DescribeKey",
"kms:Encrypt",
"kms:Decrypt"
]
resources = ["*"]
}
}
resource "aws_security_group" "boundary_sg" {
name_prefix = var.prefix
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"
]
}
ingress {
from_port = 9202
to_port = 9202
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"
]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}