· 7 years ago · Nov 10, 2018, 02:52 AM
1#cloud-config
2repo_update: true
3repo_upgrade: all
4
5runcmd:
6 - sudo yum install -y java-1.8.0-openjdk
7 - sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
8 - sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
9 - sudo yum install -y jenkins
10 - sudo /usr/sbin/alternatives --config java
11 - sudo service jenkins start
12
13#
14# Variables
15#
16variable "aws_access_key" {}
17variable "aws_secret_key" {}
18variable "region" {
19 default = "ap-southeast-2"
20}
21variable "subnets" {
22 type = "list"
23 default = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
24}
25
26
27#
28# Create an AWS Provider
29#
30provider "aws" {
31 access_key = "${var.aws_access_key}"
32 secret_key = "${var.aws_secret_key}"
33 region = "${var.region}"
34}
35
36
37#
38# Create a VPC
39#
40resource "aws_vpc" "main" {
41 cidr_block = "10.0.0.0/16"
42 tags {
43 Name = "Infocentric Training VPC"
44 }
45}
46
47output "vpc_id" {
48 value = "${aws_vpc.main.id}"
49}
50
51output "vpc_arn" {
52 value = "${aws_vpc.main.arn}"
53}
54
55#
56# Create some subnets
57#
58
59resource "aws_subnet" "main" {
60 count = "${length(var.subnets)}"
61 vpc_id = "${aws_vpc.main.id}"
62 cidr_block = "${element(var.subnets, count.index)}"
63 tags {
64 Name = "Subnet_${count.index+1}"
65 }
66}
67
68#
69# Create an internet gateway
70#
71
72resource "aws_internet_gateway" "inet_gw" {
73 vpc_id = "${aws_vpc.main.id}"
74}
75
76resource "aws_route" "internet_access" {
77 route_table_id = "${aws_vpc.main.main_route_table_id}"
78 destination_cidr_block = "0.0.0.0/0"
79 gateway_id = "${aws_internet_gateway.inet_gw.id}"
80}
81
82#
83# Create an EC2 instance for Jenkins
84#
85
86resource "aws_security_group" "jenkins_sg" {
87 name = "Jenkins Security Group"
88 description = "Allow inbound traffic on port 8080 only"
89 vpc_id = "${aws_vpc.main.id}"
90 ingress {
91 from_port = "8080"
92 to_port = "8080"
93 protocol = "tcp"
94 cidr_blocks = ["0.0.0.0/0"]
95 }
96 ingress {
97 from_port = 22
98 to_port = 22
99 protocol = "tcp"
100 security_groups = ["0.0.0.0/0"]
101 }
102 egress {
103 from_port = 0
104 to_port = 0
105 protocol = "-1"
106 cidr_blocks = ["0.0.0.0/0"]
107 }
108}
109
110
111resource "aws_instance" "jenkins" {
112 ami = "ami-0b8dea0e70b969adc"
113 instance_type = "t2.micro"
114 vpc_security_group_ids = ["${aws_security_group.jenkins_sg.id}"]
115 subnet_id = "${aws_subnet.main.0.id}"
116 key_name = "jenkins"
117 tags = {Name = "Jenkins"}
118 associate_public_ip_address = true
119 user_data = "${file("jenkins.conf")}"
120}