-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat.tf
74 lines (58 loc) · 1.85 KB
/
nat.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
data "aws_ami" "nat" {
filter {
name = "name"
values = ["amzn-ami-vpc-nat-hvm-*-x86_64-ebs"]
}
most_recent = true
owners = ["amazon"]
}
resource "aws_instance" "nat" {
ami = data.aws_ami.nat.id
instance_type = "t2.micro"
subnet_id = module.vpc.public_subnets[0]
source_dest_check = false
key_name = var.ec2_key_name
vpc_security_group_ids = [aws_security_group.nat.id]
tags = {
Name = format("%s%s", var.stack_identifier, "-nat")
}
}
data "external" "myipaddr" {
program = ["bash", "-c", "curl -s 'https://api.ipify.org?format=json'"]
}
resource "aws_security_group" "nat" {
name = format("%s%s", var.stack_identifier, "-sg-nat")
description = "SG for nat instance"
vpc_id = module.vpc.vpc_id
}
resource "aws_security_group_rule" "ssh_in_nat" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
// Opening to 0.0.0.0/0 can lead to security vulnerabilities.
cidr_blocks = [format("%s/%s", data.external.myipaddr.result.ip, "32")]
security_group_id = aws_security_group.nat.id
}
resource "aws_security_group_rule" "priv_in_nat" {
type = "ingress"
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = module.vpc.private_subnets_cidr_blocks
security_group_id = aws_security_group.nat.id
}
resource "aws_security_group_rule" "egress_all_nat" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.nat.id
}
resource "aws_route" "r" {
count = length(module.vpc.private_route_table_ids)
route_table_id = module.vpc.private_route_table_ids[count.index]
destination_cidr_block = "0.0.0.0/0"
instance_id = aws_instance.nat.id
}