forked from int128/terraform-aws-nat-instance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
211 lines (189 loc) · 5.01 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
205
206
207
208
209
210
211
resource "aws_security_group" "this" {
name_prefix = var.name
vpc_id = var.vpc_id
description = "Security group for NAT instance ${var.name}"
tags = local.common_tags
}
resource "aws_security_group_rule" "egress" {
security_group_id = aws_security_group.this.id
type = "egress"
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 65535
protocol = "tcp"
}
resource "aws_security_group_rule" "ingress_any" {
security_group_id = aws_security_group.this.id
type = "ingress"
cidr_blocks = var.private_subnets_cidr_blocks
from_port = 0
to_port = 65535
protocol = "all"
}
resource "aws_network_interface" "this" {
security_groups = [aws_security_group.this.id]
subnet_id = var.public_subnet
source_dest_check = false
description = "ENI for NAT instance ${var.name}"
tags = local.common_tags
}
resource "aws_route" "this" {
count = length(var.private_route_table_ids)
route_table_id = var.private_route_table_ids[count.index]
destination_cidr_block = "0.0.0.0/0"
network_interface_id = aws_network_interface.this.id
}
# AMI of the latest Amazon Linux 2
data "aws_ami" "this" {
most_recent = true
owners = ["amazon"]
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}
}
resource "aws_launch_template" "this" {
name_prefix = var.name
image_id = var.image_id != "" ? var.image_id : data.aws_ami.this.id
key_name = var.key_name
iam_instance_profile {
arn = aws_iam_instance_profile.this.arn
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
}
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.this.id]
delete_on_termination = true
}
tag_specifications {
resource_type = "instance"
tags = local.common_tags
}
user_data = base64encode(join("\n", [
"#cloud-config",
yamlencode({
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html
write_files : concat([
{
path : "/opt/nat/runonce.sh",
content : templatefile("${path.module}/runonce.sh", { eni_id = aws_network_interface.this.id }),
permissions : "0755",
},
{
path : "/opt/nat/snat.sh",
content : file("${path.module}/snat.sh"),
permissions : "0755",
},
{
path : "/etc/systemd/system/snat.service",
content : file("${path.module}/snat.service"),
},
], var.user_data_write_files),
runcmd : concat([
["/opt/nat/runonce.sh"],
], var.user_data_runcmd),
})
]))
description = "Launch template for NAT instance ${var.name}"
tags = local.common_tags
}
resource "aws_autoscaling_group" "this" {
name_prefix = var.name
desired_capacity = var.enabled ? 1 : 0
min_size = var.enabled ? 1 : 0
max_size = 1
vpc_zone_identifier = [var.public_subnet]
mixed_instances_policy {
instances_distribution {
on_demand_base_capacity = var.use_spot_instance ? 0 : 1
on_demand_percentage_above_base_capacity = var.use_spot_instance ? 0 : 100
}
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.this.id
version = "$Latest"
}
dynamic "override" {
for_each = var.instance_types
content {
instance_type = override.value
}
}
}
}
dynamic "tag" {
for_each = local.common_tags
content {
key = tag.key
value = tag.value
propagate_at_launch = false
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_iam_instance_profile" "this" {
name_prefix = var.name
role = aws_iam_role.this.name
tags = local.common_tags
}
resource "aws_iam_role" "this" {
name_prefix = var.name
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
tags = local.common_tags
}
resource "aws_iam_role_policy_attachment" "ssm" {
policy_arn = var.ssm_policy_arn
role = aws_iam_role.this.name
}
resource "aws_iam_role_policy" "eni" {
role = aws_iam_role.this.name
name_prefix = var.name
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:AttachNetworkInterface"
],
"Resource": "*"
}
]
}
EOF
}