-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
99 lines (83 loc) · 2.77 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.45.0"
}
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE A KEY PAIR USED FOR SSH
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_key_pair" "bastion" {
key_name = var.key_pair_name
public_key = var.key_pair_publickey
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE THE BASTION HOST
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_instance" "bastion" {
count = var.enabled ? 1 : 0
ami = var.instance_ami
instance_type = var.instance_type
subnet_id = var.public_subnet
key_name = aws_key_pair.bastion.key_name
vpc_security_group_ids = [aws_security_group.bastion.id]
associate_public_ip_address = true
tags = {
Name = var.instance_name
Environment = var.environment
Terraform = "true"
}
lifecycle {
create_before_destroy = true
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE THE SECURITY GROUP
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_security_group" "bastion" {
name = "${var.instance_name}-sg"
vpc_id = var.vpc_id
description = "Bastion security group"
ingress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = [var.vpc_cidr]
}
ingress {
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = ["0.0.0.0/0"]
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.instance_name}-sg"
Environment = var.environment
Terraform = "true"
}
lifecycle {
create_before_destroy = true
}
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE THE ROUTE 53 RECORD
# ---------------------------------------------------------------------------------------------------------------------
data "aws_route53_zone" "bastion" {
name = var.hosted_zone_name
}
resource "aws_route53_record" "bastion" {
count = var.enabled ? 1 : 0
zone_id = data.aws_route53_zone.bastion.id
name = var.dns_record
type = "A"
ttl = "60"
records = [aws_instance.bastion.0.public_ip]
}