-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.if
186 lines (124 loc) · 3.48 KB
/
main.if
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
variable "aws_region" {
description = "EC2 Region for the VPC"
default = "eu-west-1"
}
variable "amis" {
description = "AMIs by region"
default = {
eu-west-1 = "ami-00068cd7555f543d5"
}
}
variable "vpc_cidr" {
description = "CIDR for the whole VPC"
default = "10.0.0.0/16"
}
variable "public_subnet_cidr" {
description = "CIDR for the Public Subnet"
default = "10.0.0.0/24"
}
variable "public_subnet_2_cidr" {
description = "CIDR for the Public Subnet"
default = "10.0.2.0/24"
}
variable "private_subnet_cidr" {
description = "CIDR for the Private Subnet"
default = "10.0.1.0/24"
}
#################################################
#####VPC
resource "aws_vpc" "default" {
cidr_block = "${var.vpc_cidr}"
}
#####IGW
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.default.id}"
}
#########Subnets
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "${var.public_subnet_cidr}"
availability_zone = "eu-west-1a"
}
resource "aws_subnet" "public_subnet_2" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "${var.public_subnet_2_cidr}"
availability_zone = "eu-west-1b"
}
resource "aws_subnet" "private_subnet" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "${var.private_subnet_cidr}"
availability_zone = "eu-west-1a"
}
######### security group
resource "aws_security_group" "allow_http" {
name = "allow_tls"
description = "Allow TLS inbound traffic"
vpc_id = "${aws_vpc.default.id}"
ingress {
# TLS (change to whatever ports you need)
from_port = 8080
to_port = 8080
protocol = "tcp"
# Please restrict your ingress to only necessary IPs and ports.
# Opening to 0.0.0.0/0 can lead to security vulnerabilities.
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
##### load balancer
resource "aws_lb" "Jenkins" {
name = "jenkins-lb-tf"
internal = false
load_balancer_type = "application"
security_groups = ["${aws_security_group.allow_http.id}"]
subnets = ["${aws_subnet.public_subnet.id}","${aws_subnet.public_subnet_2.id}"]
}
# target group
resource "aws_alb_target_group" "group" {
name = "terraform-example-alb-target"
port = 8080
protocol = "HTTP"
vpc_id = "${aws_vpc.default.id}"
stickiness {
type = "lb_cookie"
}
# Alter the destination of the health check to be the login page.
health_check {
path = "/"
port = 8080
}
}
resource "aws_lb_target_group_attachment" "target_group_attach" {
target_group_arn = "${aws_alb_target_group.group.arn}"
target_id = "${aws_instance.jenkins_ec2.id}"
port = 8080
}
#######ec2
resource "aws_instance" "jenkins_ec2"{
ami = "ami-01f14919ba412de34"
instance_type = "t2.micro"
key_name = "tavisca-eu-west-1"
subnet_id = "${aws_subnet.private_subnet.id}"
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = "${aws_lb.Jenkins.arn}"
port = "8080"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = "${aws_alb_target_group.group.arn}"
}
}
##############
resource "aws_route_table" "public_routable" {
vpc_id = "${aws_vpc.default.id}"
route {
cidr_block = "10.0.0.0/16"
gateway_id = "${aws_internet_gateway.gw.id}"
}
}