forked from cloudposse/terraform-aws-dynamic-subnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nat-instance.tf
104 lines (89 loc) · 3.27 KB
/
nat-instance.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
module "nat_instance_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.14.0"
context = module.label.context
attributes = distinct(compact(concat(module.label.attributes, ["nat", "instance"])))
}
locals {
nat_instance_count = var.nat_instance_enabled ? length(var.availability_zones) : 0
cidr_block = var.cidr_block != "" ? var.cidr_block : data.aws_vpc.default.cidr_block
}
resource "aws_security_group" "nat_instance" {
count = var.nat_instance_enabled ? 1 : 0
name = module.nat_instance_label.id
description = "Security Group for NAT Instance"
vpc_id = var.vpc_id
tags = module.nat_instance_label.tags
}
resource "aws_security_group_rule" "nat_instance_egress" {
count = var.nat_instance_enabled ? 1 : 0
description = "Allow all egress traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.nat_instance.*.id)
type = "egress"
}
resource "aws_security_group_rule" "nat_instance_ingress" {
count = var.nat_instance_enabled ? 1 : 0
description = "Allow ingress traffic from the VPC CIDR block"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [local.cidr_block]
security_group_id = join("", aws_security_group.nat_instance.*.id)
type = "ingress"
}
// aws --region us-west-2 ec2 describe-images --owners amazon --filters Name="name",Values="amzn-ami-vpc-nat*" Name="virtualization-type",Values="hvm"
data "aws_ami" "nat_instance" {
count = var.nat_instance_enabled ? 1 : 0
most_recent = true
filter {
name = "name"
values = ["amzn-ami-vpc-nat*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"]
}
// https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-comparison.html
// https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html
// https://dzone.com/articles/nat-instance-vs-nat-gateway
resource "aws_instance" "nat_instance" {
count = local.nat_instance_count
ami = join("", data.aws_ami.nat_instance.*.id)
instance_type = var.nat_instance_type
subnet_id = element(aws_subnet.public.*.id, count.index)
vpc_security_group_ids = [aws_security_group.nat_instance[0].id]
tags = merge(
module.nat_instance_label.tags,
{
"Name" = format(
"%s%s%s",
module.nat_label.id,
var.delimiter,
replace(
element(var.availability_zones, count.index),
"-",
var.delimiter
)
)
}
)
# Required by NAT
# https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html#EIP_Disable_SrcDestCheck
source_dest_check = false
associate_public_ip_address = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "nat_instance" {
count = local.nat_instance_count
route_table_id = element(aws_route_table.private.*.id, count.index)
instance_id = element(aws_instance.nat_instance.*.id, count.index)
destination_cidr_block = "0.0.0.0/0"
depends_on = [aws_route_table.private]
}