-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-vm-main.tf
95 lines (83 loc) · 2.69 KB
/
api-vm-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
###################################
## Virtual Machine Module - Main ##
###################################
# Get latest Windows Server 2019 AMI
data "aws_ami" "windows-2019" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["Windows_Server-2019-English-Full-Base*"]
}
}
# Define the security group for the API server
resource "aws_security_group" "api-sg" {
name = "${lower(var.app_name)}-${var.app_environment}-win2019-sg"
description = "Allow incoming connections"
//vpc_id = aws_vpc.vpc.id
vpc_id = "vpc-07be40fcb192140c0"
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow incoming HTTPS connections"
}
ingress {
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow incoming RDP connections"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${lower(var.app_name)}-${var.app_environment}-win2019-sg"
Environment = var.app_environment
}
}
# Bootstrapping PowerShell Script
data "template_file" "api-userdata" {
template = file("${path.module}/setup-api.ps1")
}
# Create EC2 Instance
resource "aws_instance" "api-server" {
ami = data.aws_ami.windows-2019.id
instance_type = var.api_instance_type
//subnet_id = aws_subnet.public-subnet.id
subnet_id = "subnet-0de6e64c5b78e37f1"
vpc_security_group_ids = [aws_security_group.api-sg.id]
associate_public_ip_address = var.api_associate_public_ip_address
source_dest_check = false
key_name = aws_key_pair.key_pair.key_name
user_data = data.template_file.api-userdata.rendered
# root disk
root_block_device {
volume_size = var.api_root_volume_size
volume_type = var.api_root_volume_type
delete_on_termination = true
encrypted = true
}
tags = {
Name = "${lower(var.app_name)}-${var.app_environment}-win2019-server"
Environment = var.app_environment
}
}
# Create Elastic IP for the EC2 instance
resource "aws_eip" "api-eip" {
vpc = true
tags = {
Name = "${lower(var.app_name)}-${var.app_environment}-win2019-eip"
Environment = var.app_environment
}
}
# Associate Elastic IP to API Server
resource "aws_eip_association" "api-eip-association" {
instance_id = aws_instance.api-server.id
allocation_id = aws_eip.api-eip.id
}