-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
158 lines (136 loc) · 3.67 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.0.1"
}
}
}
#creating a vpc
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "vpc_ws"
}
}
#creating an Internet Gateway
resource "aws_internet_gateway" "igw" {
depends_on = [aws_vpc.vpc, ]
vpc_id = aws_vpc.vpc.id
tags = {
Name = "igw_ws"
}
}
#creating a public subnet
resource "aws_subnet" "publicsubnet" {
depends_on = [aws_vpc.vpc, ]
vpc_id = aws_vpc.vpc.id
cidr_block = var.public_subnet_cidr
availability_zone = var.avail_zone
tags = {
Name = "publicsubnet_ws"
}
}
#creating a public route table
resource "aws_route_table" "publicrt" {
depends_on = [aws_vpc.vpc, aws_internet_gateway.igw, ]
vpc_id = aws_vpc.vpc.id
tags = {
Name = "publicrt_ws"
}
}
#creating a public route
resource "aws_route" "publicroute" {
depends_on = [aws_internet_gateway.igw, aws_subnet.publicsubnet]
route_table_id = aws_route_table.publicrt.id
destination_cidr_block = var.allowallcidr
gateway_id = aws_internet_gateway.igw.id
}
#associating public routetable with public subnet
resource "aws_route_table_association" "publicrtsassociation" {
depends_on = [aws_subnet.publicsubnet, aws_route_table.publicrt, ]
subnet_id = aws_subnet.publicsubnet.id
route_table_id = aws_route_table.publicrt.id
}
#creating a private subnet
resource "aws_subnet" "privatesubnet" {
depends_on = [aws_vpc.vpc, ]
vpc_id = aws_vpc.vpc.id
cidr_block = var.private_subnet_cidr
availability_zone = var.avail_zone
tags = {
Name = "privatesubnet_ws"
}
}
#creating a private route table
resource "aws_route_table" "privatert" {
depends_on = [aws_vpc.vpc, ]
vpc_id = aws_vpc.vpc.id
tags = {
Name = "privatert_ws"
}
}
#private route is not necessary
#associating private route table with private subnet
resource "aws_route_table_association" "privatertsassociation" {
depends_on = [aws_subnet.privatesubnet, aws_route_table.privatert, ]
subnet_id = aws_subnet.privatesubnet.id
route_table_id = aws_route_table.privatert.id
}
#creating a web server
resource "aws_instance" "webserver" {
depends_on = [aws_vpc.vpc, aws_security_group.ws_sg, ]
ami = var.linux_ami_id
instance_type = var.instance_type
key_name = var.keypair
subnet_id = aws_subnet.publicsubnet.id
vpc_security_group_ids = [aws_security_group.ws_sg.id]
source_dest_check = false
associate_public_ip_address = true
tags = {
Name = "webserver_jenkins"
}
user_data = file("jenkinsinit.sh")
}
#creating a security group for web server
resource "aws_security_group" "ws_sg" {
vpc_id = aws_vpc.vpc.id
description = "webserver-securitygroup"
ingress {
description = "SSH-Ingress CIDR"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.myip, var.officeip]
}
ingress {
description = "HTTP-Ingress CIDR"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [var.myip, var.officeip]
}
ingress {
description = "HTTPS-Ingress CIDR"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [var.myip, var.officeip]
}
ingress {
description = "Jenkins-Ingress CIDR"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = [var.myip, var.officeip]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "ws_sg"
}
}