-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.tf
64 lines (52 loc) · 1.72 KB
/
ec2.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
data "aws_ami" "ec2_linux" {
most_recent = true
owners = [137112412989]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "owner-alias"
values = ["amazon"]
}
}
resource "aws_instance" "bastion_hosts" {
count = var.bastion_count
ami = data.aws_ami.ec2_linux.id
instance_type = var.bastion_instance_type
subnet_id = aws_subnet.utility_subnet.id
iam_instance_profile = aws_iam_instance_profile.bastion_profile.id
key_name = var.bastion_key_name
tags = {
"Patch Group" = local.bastion_patch_group_name
Name = "${var.vpc_name} Bastion Host ${count.index}"
}
vpc_security_group_ids = [aws_security_group.utility_hosts.id]
user_data = base64encode(templatefile("${path.module}/bastion.tftpl", {}))
}
locals {
enable_single_bastion_eip = var.bastion_count == 1 && var.bastion_route53 != null
}
resource "aws_eip" "single_bastion_eip" {
count = local.enable_single_bastion_eip ? 1 : 0
vpc = true
instance = aws_instance.bastion_hosts[0].id
public_ipv4_pool = "amazon"
depends_on = [aws_internet_gateway.igw]
}
data "aws_route53_zone" "root_zone" {
count = local.enable_single_bastion_eip ? 1 : 0
name = var.bastion_route53.zone.name
}
resource "aws_route53_record" "eip_a_record" {
count = local.enable_single_bastion_eip ? 1 : 0
type = "A"
name = var.bastion_route53.record.name
zone_id = data.aws_route53_zone.root_zone[0].zone_id
records = [aws_eip.single_bastion_eip[0].public_ip]
ttl = 300
}