forked from shuaibiyy/terraform-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
114 lines (100 loc) · 2.2 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
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
data "terraform_remote_state" "tfstate" {
backend = "s3"
config {
bucket = "mycompany-terraform"
key = "terraform/terraform.tfstate"
region = "us-east-1"
}
}
resource "aws_security_group" "sg_jenkins" {
name = "sg_${var.jenkins_name}"
description = "Allows all traffic"
# SSH
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
# HTTP
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
# HTTPS
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
# Jenkins JNLP port
ingress {
from_port = 50000
to_port = 50000
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
# Weave Scope port
ingress {
from_port = 4040
to_port = 4040
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
}
data "template_file" "user_data" {
template = "${file("templates/user_data.tpl")}"
}
resource "aws_instance" "jenkins" {
instance_type = "${var.instance_type}"
security_groups = ["${aws_security_group.sg_jenkins.name}"]
ami = "${lookup(var.amis, var.region)}"
key_name = "${var.key_name}"
user_data = "${data.template_file.user_data.rendered}"
tags {
"Name" = "${var.jenkins_name}"
}
# Add backup task to crontab
provisioner "file" {
connection {
user = "ec2-user"
host = "${aws_instance.jenkins.public_ip}"
timeout = "1m"
private_key = "${file("${var.key_name}.pem")}"
}
source = "templates/cron.sh"
destination = "/home/ec2-user/cron.sh"
}
provisioner "remote-exec" {
connection {
user = "ec2-user"
host = "${aws_instance.jenkins.public_ip}"
timeout = "1m"
private_key = "${file("${var.key_name}.pem")}"
}
inline = [
"chmod +x /home/ec2-user/cron.sh",
"/home/ec2-user/cron.sh ${var.access_key} ${var.secret_key} ${var.s3_bucket} ${var.jenkins_name}"
]
}
}