-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
191 lines (157 loc) · 4.72 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
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
180
181
182
183
184
185
186
187
188
189
190
################## Create VPC ##################
module "vpc" {
source = "shamimice03/vpc/aws"
version = "1.0.1"
vpc_name = "efs_vpc"
cidr = "192.168.0.0/16"
azs = ["ap-northeast-1a", "ap-northeast-1c"]
public_subnet_cidr = ["192.168.0.0/20", "192.168.16.0/20"]
enable_dns_hostnames = true
enable_dns_support = true
enable_single_nat_gateway = false
tags = {
"Team" = "platform-team"
"Env" = "efs-test"
}
}
################## Local Variables ##################
locals {
public_subnets = module.vpc.public_subnet_id
vpc_id = module.vpc.vpc_id
file_system_id = aws_efs_file_system.file_system_1.id
}
################## Create Security Group for Public Instances ##################
resource "aws_security_group" "instance_sg" {
name = "allow_public_access"
description = "Allow Traffic from Anywhere"
vpc_id = local.vpc_id
dynamic "ingress" {
for_each = var.public_instance_sg_ports
content {
from_port = ingress.value["port"]
to_port = ingress.value["port"]
protocol = ingress.value["protocol"]
cidr_blocks = ["0.0.0.0/0"]
}
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
"Name" = "instance_sg"
}
}
################## Create Security Group for Private Instances ##################
resource "aws_security_group" "efs_sg" {
name = "allow_from_public_instances"
description = "Allow traffice from public instance sg only"
vpc_id = local.vpc_id
dynamic "ingress" {
for_each = var.efs_sg_ports
content {
from_port = ingress.value["port"]
to_port = ingress.value["port"]
protocol = ingress.value["protocol"]
security_groups = [aws_security_group.instance_sg.id]
}
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
security_groups = [aws_security_group.instance_sg.id]
}
tags = {
"Name" = "efs_sg"
}
}
################## Create EFS File system ################
resource "aws_efs_file_system" "file_system_1" {
creation_token = "efs-test"
performance_mode = "generalPurpose"
throughput_mode = "bursting"
lifecycle_policy {
transition_to_ia = "AFTER_30_DAYS"
}
tags = {
Name = "efs-test"
}
}
################## Create EFS mount targets ################
resource "aws_efs_mount_target" "mount_targets" {
count = 2
file_system_id = aws_efs_file_system.file_system_1.id
subnet_id = local.public_subnets[count.index]
security_groups = [aws_security_group.efs_sg.id]
}
################## Generating Script for Mounting EFS ##################
resource "null_resource" "generate_efs_mount_script" {
provisioner "local-exec" {
command = templatefile("efs_mount.tpl", {
efs_mount_point = var.efs_mount_point
file_system_id = local.file_system_id
})
interpreter = [
"bash",
"-c"
]
}
}
################## SSH key generation ##################
resource "tls_private_key" "ssh" {
algorithm = "RSA"
rsa_bits = 4096
}
################## Extracting private key ##################
resource "local_file" "private_key" {
content = tls_private_key.ssh.private_key_pem
filename = var.private_key_location
file_permission = "0400"
}
################## Create AWS key pair ##################
resource "aws_key_pair" "aws_ec2_access_key" {
key_name_prefix = var.key_name
public_key = tls_private_key.ssh.public_key_openssh
}
################## Create AWS EC2 Instance on Public Subnet ################
resource "aws_instance" "public_hosts" {
count = var.instance_count
ami = data.aws_ami.amazon_linux_ami.id
instance_type = var.instance_type
key_name = aws_key_pair.aws_ec2_access_key.id
vpc_security_group_ids = [aws_security_group.instance_sg.id]
subnet_id = local.public_subnets[count.index]
tags = {
"Name" = "public-instance-${count.index + 1}"
}
depends_on = [
null_resource.generate_efs_mount_script,
aws_efs_mount_target.mount_targets
]
provisioner "file" {
source = "efs_mount.sh"
destination = "efs_mount.sh"
}
connection {
type = "ssh"
host = self.public_ip
user = "ec2-user"
private_key = file(var.private_key_location) # Location of the Private Key
timeout = "4m"
}
provisioner "remote-exec" {
inline = [
"bash efs_mount.sh",
]
}
}
################## Clean Up Existing Script ##################
resource "null_resource" "clean_up" {
provisioner "local-exec" {
when = destroy
command = "rm -rf efs_mount.sh"
}
}