-
Notifications
You must be signed in to change notification settings - Fork 4
/
jenkins-server.yml
96 lines (92 loc) · 3.35 KB
/
jenkins-server.yml
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
AWSTemplateFormatVersion: 2010-09-09
Description: >
This Cloudformation Template creates a Nginx Server and Jenkins on EC2 Instance.
Nginx Server and Jenkins will run on Amazon Linux 2 EC2 Instance with
custom security group allowing HTTP(80, 8080) and SSH (22) connections from anywhere.
Parameters:
KeyPairName:
Description: Enter the name of your Key Pair for SSH connections.
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Must one of the existing EC2 KeyPair
Resources:
DeployServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH and HTTP for Jenkins Server
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
DeployServer:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0a8b4cd432b1c3063 # Amazon Linux 2 AMI (HVM) - Kernel 5.10, SSD Volume Type - ami-0a8b4cd432b1c3063 (64-bit x86) / ami-0b6705f88b1f688c1 (64-bit Arm)
InstanceType: t2.micro
KeyName: !Ref KeyPairName
SecurityGroupIds:
- !GetAtt DeployServerSecurityGroup.GroupId
Tags:
- Key: Name
Value: !Sub Deploy Server of ${AWS::StackName}
- Key: env
Value: prod
UserData:
Fn::Base64: |
#! /bin/bash
# update os
yum update -y
# set deploy server hostname as deploy-server
hostnamectl set-hostname deploy-server
# install git
yum install git -y
amazon-linux-extras install java-openjdk11 -y
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
amazon-linux-extras install epel -y
# install jenkins
yum install jenkins -y
systemctl start jenkins
# enable jenkins to start on boot
systemctl enable jenkins
systemctl status jenkins
# install docker
amazon-linux-extras install docker -y
# start docker
systemctl start docker
# enable docker to start on boot
systemctl enable docker
# add ec2-user to docker group
usermod -a -G docker ec2-user
usermod -a -G docker jenkins
systemctl daemon-reload
systemctl restart docker
# install nginx server
amazon-linux-extras install nginx1.12
# start nginx
systemctl start nginx
# enable reverse proxy in nginx.conf
sed -i '48i proxy_pass http://localhost:3456/;' etc/nginx/nginx.conf
# create docker image and run it
docker pull devenes/weather-app:20
docker run -d -p 3456:3456 --name weather-app devenes/weather-app:20
systemctl restart nginx.service
Outputs:
DeployServerDNS:
Description: Deploy Server DNS Name
Value: !Sub
- ${PublicAddress}
- PublicAddress: !GetAtt DeployServer.PublicDnsName
DeployURL:
Description: Deploy Server URL
Value: !Sub
- http://${PublicAddress}:8080
- PublicAddress: !GetAtt DeployServer.PublicDnsName