-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
118 lines (95 loc) · 2.5 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
# Provider Block
provider "aws" {
profile = "default"
region = var.dev_region
}
# Resources Block
# VPC
resource "aws_vpc" "dev_vpc" {
cidr_block = var.dev_vpc_cidr_block
enable_dns_hostnames = var.dev_dns_hostnames
enable_dns_support = var.dev_dns_support
tags = {
Name = var.dev_vpc_tagname
}
}
# subnet
resource "aws_subnet" "dev_public_subnet" {
vpc_id = aws_vpc.dev_vpc.id
cidr_block = var.dev_public_subnet_cidr_block
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
tags = {
Name = var.dev_public_subnet_tagname
}
}
# internet gatway
resource "aws_internet_gateway" "dev_igw" {
vpc_id = aws_vpc.dev_vpc.id
tags = {
Name = var.dev_igw_tagname
}
}
# route table
resource "aws_route_table" "dev_route_table" {
vpc_id = aws_vpc.dev_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.dev_igw.id
}
tags = {
Name = "dev-route-table"
}
}
resource "aws_route_table_association" "dev_associate" {
route_table_id = aws_route_table.dev_route_table.id
subnet_id = aws_subnet.dev_public_subnet.id
}
# security groups
resource "aws_security_group" "dev_security_group" {
description = "security group of the dev"
vpc_id = aws_vpc.dev_vpc.id
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = var.dev_sg_ingress_ips
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = var.dev_sg_egress_ips
}
tags = {
Name = "dev-security-group"
}
}
# Ec2 key pairs
resource "aws_key_pair" "dev_key_pair" {
key_name = "dev-public-key-pair"
public_key = file(var.public_key_path_with_filename)
}
# Ec2 instance
resource "aws_instance" "dev_ec2_node" {
ami = data.aws_ami.node_os.image_id
instance_type = var.dev_ec2_instance_type
subnet_id = aws_subnet.dev_public_subnet.id
vpc_security_group_ids = [aws_security_group.dev_security_group.id]
key_name = aws_key_pair.dev_key_pair.id
user_data = file("user_data.tpl")
root_block_device {
volume_size = 10
}
provisioner "local-exec" {
command = templatefile("${var.host_os}_ssh_config.tpl", {
hostname = self.public_ip,
user = "ubuntu",
identityfile = var.private_key_path_with_filename
})
interpreter = var.host_os == "windows" ? ["powershell", "-Command"] : ["bash", "-c"]
}
tags = {
Name = var.dev_ec2_instance_tagname
}
}