-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
146 lines (113 loc) · 3.62 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
/**
* # AWS Bastion Instance
* This module creates Auto-Scaling Group containing a single EC2 instance with public IP.
* The instance can access all other instances in a VPC (Security Groups are preconfigured).
* User Data script is parameterizable and it's output is logged to `/var/log/user-data.log` by default.
* One can use `aws-s3-authorized-keys` module in order to be able to manage SSH keys that have access to the instance.
*/
locals {
create_count = var.create ? 1 : 0
}
resource "aws_security_group" "bastion_host" {
count = local.create_count
name = "${var.name}-bastion-host"
vpc_id = var.vpc_id
tags = {
Name = "${var.name}-bastion-host"
}
}
resource "aws_security_group_rule" "bastion_ssh" {
count = local.create_count
security_group_id = aws_security_group.bastion_host[0].id
cidr_blocks = var.allowed_cidr_blocks
from_port = 22
to_port = 22
protocol = "tcp"
type = "ingress"
}
resource "aws_security_group_rule" "bastion_all_access_egress" {
count = local.create_count
security_group_id = aws_security_group.bastion_host[0].id
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
to_port = 0
protocol = "-1"
type = "egress"
}
resource "aws_security_group_rule" "sg_all_access_ingress" {
count = var.create ? length(var.egress_security_groups) : 0
security_group_id = var.egress_security_groups[count.index]
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.bastion_host[0].id
type = "ingress"
}
data "template_file" "user_data" {
count = local.create_count
template = file("${path.module}/templates/user_data.sh")
vars = {
additional_user_data_script = var.additional_user_data
}
}
data "aws_iam_policy_document" "bastion" {
count = local.create_count
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "bastion" {
count = local.create_count
name_prefix = "${var.name}-roles-"
assume_role_policy = data.aws_iam_policy_document.bastion[0].json
}
resource "aws_iam_instance_profile" "bastion" {
count = local.create_count
name_prefix = "${var.name}-"
role = aws_iam_role.bastion[0].id
}
data "aws_ami" "amazon_linux" {
count = local.create_count
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
owners = ["amazon"]
most_recent = true
}
resource "aws_instance" "bastion" {
count = local.create_count
ami = var.ami_id != null ? var.ami_id : data.aws_ami.amazon_linux[0].id
instance_type = var.instance_type
user_data = data.template_file.user_data[0].rendered
disable_api_termination = var.disable_api_termination
monitoring = var.detailed_monitoring
subnet_id = var.subnet_id
vpc_security_group_ids = [
aws_security_group.bastion_host[0].id
]
root_block_device {
volume_size = var.volume_size
}
iam_instance_profile = aws_iam_instance_profile.bastion[0].id
key_name = var.ssh_key_name
tags = merge(var.extra_tags, {
Name = var.name
})
lifecycle {
create_before_destroy = true
}
}
resource "aws_eip" "bastion" {
count = var.eip_id != null ? 0 : local.create_count
instance = aws_instance.bastion[0].id
}
resource "aws_eip_association" "bastion" {
count = var.eip_id != null ? local.create_count : 0
instance_id = aws_instance.bastion[0].id
allocation_id = var.eip_id
}