-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtual Machine.tf
executable file
·179 lines (151 loc) · 4.44 KB
/
Virtual Machine.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
## Data
# AMI
data "aws_ami" "ubuntu2004" {
most_recent = true
filter {
name = "name"
values = ["*ubuntu*20.04*"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "state"
values = ["available"]
}
owners = ["099720109477"] # Canonical
}
data "aws_subnet" "Subnet" {
id = var.Subnet_ID
}
data "null_data_source" "resource" {
inputs = {
name = "${var.Project_Prefix}${var.Hostname_Suffix}"
}
}
## Variables
# Naming
variable "Hostname_Suffix" {
default = "vm01"
type = string
description = "String is 'Project_Prefix' followed by the suffix, e.g. `demovm01` where `Project_Prefix` is demo."
}
# Access and Licenses
variable "Key_Name" {
default = null
description = "The name of the SSH Key to be provided from the AWS APIs. Left blank creates the virtual machine with no SSH key authentication."
}
# Network location and addressing
variable "Subnet_ID" {
description = "The ID of the subnet into which the VM should be built"
validation {
condition = can(regex("^subnet-[0-9a-f]+", var.Subnet_ID))
error_message = "Subnet IDs must match a particular convention."
}
}
variable "Public" {
default = false
description = "Assign a public IP address or not"
validation {
condition = var.Public == true || var.Public == false
error_message = "Value must be a boolean."
}
}
## Resources
resource "aws_instance" "appliance" {
tags = {
Name = data.null_data_source.resource.outputs["name"]
}
ami = data.aws_ami.ubuntu2004.id
instance_type = "t2.nano"
key_name = var.Key_Name
user_data = templatefile(
"${path.module}/user_data.txt.tmpl",
{
hostname = data.null_data_source.resource.outputs["name"]
}
)
network_interface {
network_interface_id = aws_network_interface.eth0.id
device_index = 0
}
}
resource "aws_network_interface" "eth0" {
description = "${data.null_data_source.resource.outputs["name"]}-eth0"
subnet_id = data.aws_subnet.Subnet.id
}
resource "aws_network_interface_sg_attachment" "eth0" {
depends_on = [aws_network_interface.eth0]
security_group_id = aws_security_group.allow_in.id
network_interface_id = aws_network_interface.eth0.id
}
resource "aws_eip" "eth0" {
count = var.Public ? 1 : 0
vpc = true
}
resource "aws_eip_association" "eth0" {
count = var.Public ? 1 : 0
network_interface_id = aws_network_interface.eth0.id
allocation_id = aws_eip.eth0[0].id
}
resource "aws_security_group" "allow_in" {
name = "${data.null_data_source.resource.outputs["name"]}_allow_in"
description = "Allow Ingress and Egress traffic"
vpc_id = data.aws_subnet.Subnet.vpc_id
tags = {
Name = "${data.null_data_source.resource.outputs["name"]}_allow_in"
}
}
resource "aws_security_group_rule" "ingress_ping" {
type = "ingress"
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.allow_in.id
description = "Ping from Anywhere"
}
resource "aws_security_group_rule" "ingress_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.allow_in.id
description = "SSH from Anywhere"
}
resource "aws_security_group_rule" "ingress_http" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.allow_in.id
description = "HTTP from Anywhere"
}
resource "aws_security_group_rule" "ingress_https" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.allow_in.id
description = "HTTPS from Anywhere"
}
resource "aws_security_group_rule" "egress_any" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.allow_in.id
description = "Anything to Anywhere"
}
## Outputs
output "ip" {
value = var.Public == true ? aws_eip.eth0[0].public_ip : aws_network_interface.eth0.private_ip
}
output "name" {
value = data.null_data_source.resource.outputs["name"]
}