-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yaml
270 lines (247 loc) · 8.92 KB
/
template.yaml
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
SAM Template for Deploying 3xEC2 Kubernetes Cluster
Parameters:
# This is a security group which permits all ports from my ip only
DevopsFireWall:
Description: The DevOps Firewall Security Group ID
Type: AWS::EC2::SecurityGroup::Id
# ID of an existing VPC to create internal firewall and deploy the machines
TheVPC:
Description: ID of An existing VPC
Type: AWS::EC2::VPC::Id
# AMI id - this is ubuntu 18.04 on ap-south-1
ImageID:
Description: AMI id preferred to use ubuntu 18.04
Type: AWS::EC2::Image::Id
# Intstance type ( defines memory and processor )
InstanceSize:
Description: Instance Size
Type: String
Default: c5.large
# SSH key pair, will need to ssh into sometimes
KeyPair:
Description: Key Pair Name - Should be existing key pair
Type: String
Resources:
# delete ssm parameter from store when stack is deleted
DeleteParameterOnStackDelete:
Type: Custom::DeleteSSMParameter
Properties:
ServiceToken: !GetAtt DeleteParameterFunction.Arn
ParameterName: !Sub "${AWS::StackName}-join-cmd"
InstanceId: !Ref KubeMaster
# Note: This absolutely requires the inline code with ZipFile
# to get the cfn-response module included
DeleteParameterFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Role: !GetAtt DeleteParameterExecutionRole.Arn
Runtime: nodejs12.x
Timeout: 30
Code:
ZipFile: |
const AWS = require('aws-sdk');
var response = require('cfn-response');
const ssm = new AWS.SSM({apiVersion: '2014-11-06'});
exports.handler = function(event, context) {
console.log("request received:\n" + JSON.stringify(event));
var physicalId = event.PhysicalResourceId;
function success(data) {
data = data || {}
console.log('SUCCESS:\n', data);
return response.send(event, context, response.SUCCESS, data, physicalId);
}
function failed(err) {
console.log('FAILED:\n', err);
return response.send(event, context, response.FAILED, err, physicalId);
}
// ignore non-delete requests
if (event.RequestType !== 'Delete') {
console.log('Non-delete request is ignored');
return success();
}
// this is the extra property on the cf custom resource
var parameterName = event.ResourceProperties.ParameterName;
if (!parameterName) {
return failed('parameterName required');
}
var params = {
Name: parameterName
};
ssm.deleteParameter(params)
.promise()
.then((data) => {
console.log('"DeleteParameters" Response:\n', JSON.stringify(data));
success();
})
.catch((err) => failed(err));
};
DeleteParameterExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: ParameterDeletePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "ssm:DeleteParameter"
Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}-join-cmd"
# create an internal firewall - security group
FireWallInternal:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Internal All Traffic Group
VpcId: !Ref TheVPC
# this rule opens all ports and protocols with source as the same security group
# when multiple ec2 is attached they can communicate internally without issues
FireWallRuleInternal:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: "-1"
SourceSecurityGroupId:
Fn::GetAtt:
- FireWallInternal
- GroupId
GroupId:
Fn::GetAtt:
- FireWallInternal
- GroupId
# the EC2 machine role, which is the recommended practice to provide access to aws services internally
MachineRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: CFN2StackAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "cloudformation:SignalResource"
- "cloudformation:DescribeStackResource"
Resource: !Sub "${AWS::StackId}"
- Effect: Allow
Action:
- "ssm:PutParameter"
- "ssm:GetParameter"
Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}-join-cmd"
# the EC2 Access Profile which utilizes the above machine role
EC2AccessProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: "/"
Roles:
-
Ref: "MachineRole"
KubeNodeOne:
Type: AWS::EC2::Instance
CreationPolicy:
ResourceSignal:
Timeout: PT30M
DependsOn: KubeMaster
Properties:
ImageId: !Ref ImageID
KeyName: !Ref KeyPair
InstanceType: !Ref InstanceSize
IamInstanceProfile: !Ref EC2AccessProfile
SecurityGroupIds:
- !Ref DevopsFireWall
- !Ref FireWallInternal
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash
wget -q https://www.jijutm.com/downloads/kube-bootstrap.sh -O /tmp/install.sh
. /tmp/install.sh
aws ssm get-parameter --region ${AWS::Region} --name ${AWS::StackName}-join-cmd | grep Value | awk -F'"' '{print $4}' | base64 -d > /tmp/kube-join.sh
. /tmp/kube-join.sh
/usr/local/bin/cfn-signal --exit-code 0 --resource KubeNodeOne --region ${AWS::Region} --stack ${AWS::StackName}
KubeNodeTwo:
Type: AWS::EC2::Instance
CreationPolicy:
ResourceSignal:
Timeout: PT30M
DependsOn: KubeMaster
Properties:
ImageId: !Ref ImageID
KeyName: !Ref KeyPair
InstanceType: !Ref InstanceSize
IamInstanceProfile: !Ref EC2AccessProfile
SecurityGroupIds:
- !Ref DevopsFireWall
- !Ref FireWallInternal
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash
wget -q https://www.jijutm.com/downloads/kube-bootstrap.sh -O /tmp/install.sh
. /tmp/install.sh
aws ssm get-parameter --region ${AWS::Region} --name ${AWS::StackName}-join-cmd | grep Value | awk -F'"' '{print $4}' | base64 -d > /tmp/kube-join.sh
. /tmp/kube-join.sh
/usr/local/bin/cfn-signal --exit-code 0 --resource KubeNodeTwo --region ${AWS::Region} --stack ${AWS::StackName}
KubeMaster:
Type: AWS::EC2::Instance
CreationPolicy:
ResourceSignal:
Timeout: PT30M
Properties:
ImageId: !Ref ImageID
KeyName: !Ref KeyPair
InstanceType: !Ref InstanceSize
IamInstanceProfile: !Ref EC2AccessProfile
SecurityGroupIds:
- !Ref DevopsFireWall
- !Ref FireWallInternal
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash
wget -q https://www.jijutm.com/downloads/kube-bootstrap.sh -O /tmp/install.sh
. /tmp/install.sh
wget -q https://www.jijutm.com/downloads/kube-init-cluster.sh -O /tmp/init.sh
. /tmp/init.sh
aws ssm put-parameter --region ${AWS::Region} --name ${AWS::StackName}-join-cmd --type String --value "`cat /tmp/join.sh | base64 -w 0`"
/usr/local/bin/cfn-signal --exit-code 0 --resource KubeMaster --region ${AWS::Region} --stack ${AWS::StackName}
# EIP (Static IP) for only master machine
KubeMasterEIP:
Type: AWS::EC2::EIP
Properties:
InstanceId: !Ref KubeMaster
Outputs:
MasterPublicDNS:
Description: "KubeMaster Public DNS Name"
Value: !GetAtt KubeMaster.PublicDnsName
NodeOnePublicDNS:
Description: "KubeNodeOne Public DNS Name"
Value: !GetAtt KubeNodeOne.PublicDnsName
NodeTwoPublicDNS:
Description: "KubeNodeTwo Public DNS"
Value: !GetAtt KubeNodeTwo.PublicDnsName