-
Notifications
You must be signed in to change notification settings - Fork 0
/
bastion.tf
71 lines (57 loc) · 1.76 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
data "aws_ami" "bastion" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
owners = [var.ami_owner]
}
resource "aws_security_group" "bastion_ssh" {
name = "bastion-host-security-group"
vpc_id = var.vpc_id
ingress {
description = var.bastion_ssh_sec_group_description
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [format("%s/32", var.ip_address)]
}
// We do want to e.g. install stuffs in the bastion server
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = local.default_tags
}
resource "aws_network_interface" "bastion" {
subnet_id = var.subnet_id
security_groups = [aws_security_group.bastion_ssh.id]
tags = local.default_tags
}
resource "aws_key_pair" "bastion" {
key_name = "bastion-ssh-key"
public_key = file(var.ssh_key_file)
tags = local.default_tags
}
resource "aws_instance" "bastion" {
ami = data.aws_ami.bastion.id
instance_type = "t2.micro"
key_name = aws_key_pair.bastion.key_name
network_interface {
network_interface_id = aws_network_interface.bastion.id
device_index = 0
}
tags = local.default_tags
}
// Update the target security group to allow access from the bastion server
resource "aws_security_group_rule" "target" {
type = "ingress"
from_port = var.target_ingress_port
to_port = var.target_ingress_port
protocol = var.target_protocol
source_security_group_id = aws_security_group.bastion_ssh.id
security_group_id = var.target_security_group_id
description = var.target_security_rule_description
}