-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
207 lines (177 loc) · 4.93 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
resource "aws_autoscaling_group" "this" {
desired_capacity = 1
max_size = 1
min_size = 1
name = var.stack_name
vpc_zone_identifier = [var.subnet_id]
health_check_type = "EC2"
launch_template {
id = aws_launch_template.this.id
version = "$Latest"
}
dynamic "tag" {
for_each = local.tags
content {
key = tag.key
propagate_at_launch = true
value = tag.value
}
}
}
resource "aws_launch_template" "this" {
name = var.stack_name
image_id = var.ubuntu_ami_id == "" ? data.aws_ami.ubuntu[0].id : var.ubuntu_ami_id
key_name = aws_key_pair.this.key_name
ebs_optimized = true
instance_type = var.instance_type
instance_initiated_shutdown_behavior = "terminate"
update_default_version = true
block_device_mappings {
device_name = var.ubuntu_ami_id != "" ? "/dev/sda1" : data.aws_ami.ubuntu[0].root_device_name
ebs {
volume_size = 8
volume_type = "gp2"
delete_on_termination = true
}
}
iam_instance_profile {
name = aws_iam_instance_profile.this.name
}
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.this.id]
}
user_data = base64encode(templatefile("${path.module}/resources/templates/user_data.tpl",
{
aws_region = data.aws_region.current.name,
eip_address = aws_eip.this.public_ip,
eip_allocation_id = aws_eip.this.id,
volume_path = var.volume_path,
volume_device_name = "/dev/sdf"
volume_id = aws_ebs_volume.this.id
docker_cidr = var.docker_cidr
docker_compose = local.docker_compose
create_client = local.create_client
revoke_client = local.revoke_client
routes = [ for peered_network in var.peered_networks : "${element(split("/", peered_network), 0)} ${cidrnetmask(peered_network)}" ]
}
))
tag_specifications {
resource_type = "instance"
tags = local.tags
}
tag_specifications {
resource_type = "volume"
tags = local.tags
}
tag_specifications {
resource_type = "network-interface"
tags = local.tags
}
}
resource "aws_ebs_volume" "this" {
availability_zone = data.aws_subnet.selected.availability_zone
encrypted = true
type = "gp2"
size = 30
tags = local.tags
}
resource "aws_key_pair" "this" {
key_name = var.stack_name
public_key = base64decode(aws_ssm_parameter.public_key.value)
tags = local.tags
}
resource "aws_eip" "this" {
vpc = true
tags = local.tags
}
resource "aws_security_group" "this" {
name = var.stack_name
description = "OpenVPN"
vpc_id = var.vpc_id
tags = local.tags
ingress {
description = "OpenVPN"
from_port = 1194
to_port = 1194
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
self = true
cidr_blocks = var.ssh_ingress_cidrs
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
resource "tls_private_key" "this" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_ssm_parameter" "public_key" {
name = "${local.ssm_parameters_path}public-ssh-key"
type = "SecureString"
value = base64encode(tls_private_key.this.public_key_openssh)
tags = local.tags
}
resource "aws_ssm_parameter" "private_key" {
name = "${local.ssm_parameters_path}private-ssh-key"
type = "SecureString"
tier = "Advanced"
value = base64encode(tls_private_key.this.private_key_pem)
tags = local.tags
}
resource "aws_iam_instance_profile" "this" {
name = var.stack_name
role = aws_iam_role.this.name
}
resource "aws_iam_role" "this" {
name = var.stack_name
tags = local.tags
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
resource "aws_iam_role_policy" "this" {
name = var.stack_name
role = aws_iam_role.this.id
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:AssociateAddress",
"ec2:DisassociateAddress",
"ec2:AttachVolume"
],
"Resource": "*"
}
]
}
EOF
}