-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbastion.tf
134 lines (110 loc) · 3.3 KB
/
bastion.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
data "aws_key_pair" "terminal" {
count = length(var.ssh_keys)
key_name = element(var.ssh_keys, count.index)
include_public_key = true
}
data "cloudinit_config" "bastion" {
gzip = true
base64_encode = true
# Main cloud-config configuration file.
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = templatefile("${path.module}/templates/cloud-init.yml.tpl", {
ssh_keys = concat(data.aws_key_pair.terminal[*].public_key, [tls_private_key.terraform_cloud.public_key_openssh])
})
}
}
resource "aws_iam_instance_profile" "bastion" {
name = "${var.identifier}-bastion-profile"
role = aws_iam_role.bastion.name
tags = {
Blueprint = var.blueprint
}
}
resource "terraform_data" "bastion_cloudinit" {
input = data.cloudinit_config.bastion.rendered
}
resource "aws_instance" "bastion" {
ami = data.aws_ami.ubuntu.id
instance_type = var.bastion_size
subnet_id = var.public_subnet_ids[0]
vpc_security_group_ids = [aws_security_group.bastion_firewall.id]
user_data_base64 = data.cloudinit_config.bastion.rendered
iam_instance_profile = aws_iam_instance_profile.bastion.name
metadata_options {
http_tokens = "required"
}
root_block_device {
encrypted = true
}
connection {
type = "ssh"
user = local.user
host = self.public_ip
private_key = tls_private_key.terraform_cloud.private_key_openssh
}
provisioner "file" {
content = tls_private_key.bastion_key.private_key_openssh
destination = "/home/ubuntu/.ssh/id_ed25519"
}
provisioner "remote-exec" {
inline = [
"chmod 600 /home/ubuntu/.ssh/id_ed25519"
]
}
tags = {
Name = "${var.identifier}-bastion"
Blueprint = var.blueprint
}
lifecycle {
ignore_changes = [
ami
]
replace_triggered_by = [
terraform_data.bastion_cloudinit
]
}
}
resource "aws_security_group" "bastion_firewall" {
name = "${var.identifier}-instellar-bastion"
description = "Instellar Bastion Configuration"
vpc_id = var.vpc_id
tags = {
Name = "${var.identifier}-bastion-firewall"
Blueprint = var.blueprint
}
}
resource "aws_vpc_security_group_egress_rule" "allow_bastion_outgoing_v4" {
security_group_id = aws_security_group.bastion_firewall.id
description = "Enable all outgoing traffic"
ip_protocol = "-1"
cidr_ipv4 = "0.0.0.0/0"
tags = {
Name = "${var.identifier}-bastion-outgoing-v4"
Blueprint = var.blueprint
}
}
resource "aws_vpc_security_group_egress_rule" "allow_bastion_outgoing_v6" {
security_group_id = aws_security_group.bastion_firewall.id
description = "Enable all outgoing traffic"
ip_protocol = "-1"
cidr_ipv6 = "::/0"
tags = {
Name = "${var.identifier}-bastion-outgoing-v6"
Blueprint = var.blueprint
}
}
resource "aws_vpc_security_group_ingress_rule" "allow_ssh" {
count = var.bastion_ssh ? 1 : 0
security_group_id = aws_security_group.bastion_firewall.id
description = "Enable ssh traffic"
from_port = 22
to_port = 22
ip_protocol = "tcp"
cidr_ipv4 = "0.0.0.0/0"
tags = {
Name = "${var.identifier}-public-ssh"
Blueprint = var.blueprint
}
}