-
Notifications
You must be signed in to change notification settings - Fork 26
/
CF_spring_cloud_aws_sample.yml
230 lines (204 loc) · 6.86 KB
/
CF_spring_cloud_aws_sample.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
AWSTemplateFormatVersion: 2010-09-09
Description: AWS Cloud Formation Spring Cloud Aws Example
Parameters:
DatabasePassword:
Description: Database Password
Type: String
NoEcho: true
Default: your_password
KeyName:
Description: "Name of an existing EC2 KeyPair to enable SSH access to the instance."
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: "must be the name of an existing EC2 KeyPair."
Mappings:
RegionMap:
eu-west-1:
AMI: ami-02df9ea15c1778c9c
Resources:
SpringCloudAwsRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: SpringCloudAwsRolePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'rds:DescribeDBInstances'
- 's3:PutObject'
- 's3:GetObject'
- 's3:DeleteObject'
- 'secretsmanager:DescribeSecret'
- 'secretsmanager:GetSecretValue'
- 'secretsmanager:ListSecrets'
- 'secretsmanager:ListSecretVersionIds'
Resource: '*'
RoleName: SpringCloudAwsSampleRole
SpringCloudAwsInstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
DependsOn: SpringCloudAwsRole
Properties:
Path: /
Roles:
- SpringCloudAwsSampleRole
SpringCloudAwsRDS:
Type: AWS::RDS::DBInstance
DependsOn:
- SpringCloudAwsRole
Properties:
AllocatedStorage: '5'
DBInstanceIdentifier: springaws
DBInstanceClass: db.t2.micro
Engine: MySQL
DBName: springaws
MasterUsername: springaws
MasterUserPassword: !Ref DatabasePassword
MultiAZ: false
PubliclyAccessible: true
DBSecurityGroups:
- !Ref SpringCloudAwsRDSSecurityGroup
DeletionPolicy: Delete
SpringCloudAwsRDSSecurityGroup:
Type: AWS::RDS::DBSecurityGroup
Properties:
GroupDescription : Security Group for RDS public Access
DBSecurityGroupIngress:
- CIDRIP: 0.0.0.0/0
SpringCloudAwsServerSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Security Group for Spring server
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
S3bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: spring-cloud-aws-sample-s3
DeletionPolicy: Delete
BucketPolicy:
Type: 'AWS::S3::BucketPolicy'
DependsOn: S3bucket
Properties:
Bucket: 'spring-cloud-aws-sample-s3'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: 'AddPerm'
Principal: '*'
Action: 's3:GetObject'
Effect: 'Allow'
Resource:
- 'arn:aws:s3:::spring-cloud-aws-sample-s3/*'
SpringCloudAwsEC2Instance:
Type: AWS::EC2::Instance
DependsOn: SpringCloudAwsRDS
Metadata:
Comment: Spring Using AWS services
AWS::CloudFormation::Init:
config:
files:
"/etc/cfn/cfn-hup.conf":
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
mode: "000400"
owner: "root"
group: "root"
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.SpringCloudAwsEC2Instance.Metadata.AWS::CloudFormation::Init
action=/usr/local/bin/cfn-init -v --stack ${AWS::StackName} --resource SpringCloudAwsEC2Instance --region ${AWS::Region}
mode: "000400"
owner: "root"
group: "root"
"/usr/local/bin/installSoftware.sh":
content: |
#!/bin/bash -xe
set -eu -o pipefail
# installing necessary software
apt-get update
apt-get install -y awscli python-pip
pip install --upgrade awscli
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
apt-get update
apt-get install -y docker-ce
mode: "000755"
owner: "root"
group: "root"
"/usr/local/bin/check_app_ready.sh":
content: |
#!/bin/bash -xe
set -eu -o pipefail
sleep 1m
while true
do
HTTP_STATUS=$(curl -Ik http://localhost/ | head -n1 | awk '{print $2}')
if [ $HTTP_STATUS == 200 ]; then
break
fi
sleep 1
done
mode: "000755"
owner: "root"
group: "root"
Properties:
ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
InstanceType: t2.micro
KeyName: !Ref KeyName
IamInstanceProfile: !Ref SpringCloudAwsInstanceProfile
SecurityGroupIds:
- !GetAtt 'SpringCloudAwsServerSecurityGroup.GroupId'
Tags:
- Key: Name
Value: 'spring-aws-cloud-sample'
UserData:
"Fn::Base64":
!Sub |
#!/bin/bash -xe
set -eu -o pipefail
apt-get update
apt-get install -y python-pip
pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
cfn-init --region ${AWS::Region} --stack ${AWS::StackId} --resource SpringCloudAwsEC2Instance
# Install needed software
/usr/local/bin/installSoftware.sh || { echo "Error installing software"; exit 1; }
# Run App
docker run -d -e PROG_OPTS='--spring.profiles.active=prod' -p 80:8080 codeurjc/spring-cloud-aws-sample:latest
/usr/local/bin/check_app_ready.sh || { echo "Error installing software"; exit 1; }
# sending the finish call
/usr/local/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource WaitCondition --region ${AWS::Region}
WaitCondition:
Type: AWS::CloudFormation::WaitCondition
CreationPolicy:
ResourceSignal:
Timeout: PT40M
Count: 1
Outputs:
Application:
Description: "Deployed Spring Application"
Value: !Join
- ''
- - 'http://'
- !GetAtt SpringCloudAwsEC2Instance.PublicDnsName