-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdeploy.template.yaml
195 lines (192 loc) · 6.72 KB
/
deploy.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
AWSTemplateFormatVersion: "2010-09-09"
Description: "deploy the AWSQS::EKS::Cluster resource into CloudFormation registry"
Parameters:
CreateClusterAccessRole:
Type: String
Default: 'true'
AllowedValues:
- 'true'
- 'false'
PermissionsBoundary:
Type: String
Default: ''
Conditions:
CreateRole: !Equals [!Ref CreateClusterAccessRole, 'true']
PermissionsBoundary: !Not [!Equals [!Ref PermissionsBoundary, '']]
Resources:
ExecutionRole:
Type: AWS::IAM::Role
Properties:
PermissionsBoundary:
Fn::If:
- PermissionsBoundary
- Ref: PermissionsBoundary
- Ref: AWS::NoValue
MaxSessionDuration: 8400
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: resources.cloudformation.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: ResourceTypePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "ec2:*"
- "eks:CreateCluster"
- "eks:DeleteCluster"
- "eks:DescribeCluster"
- "eks:ListClusters"
- "eks:UpdateClusterConfig"
- "eks:UpdateClusterVersion"
- "iam:*"
- "kms:*"
- "lambda:*"
- "sts:AssumeRole"
Resource: "*"
KubernetesVpcRole:
Condition: CreateRole
Type: AWS::IAM::Role
Properties:
PermissionsBoundary:
Fn::If:
- PermissionsBoundary
- Ref: PermissionsBoundary
- Ref: AWS::NoValue
RoleName: "CloudFormation-Kubernetes-VPC"
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/AWSLambdaENIManagementAccess'
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
LogDeliveryRole:
Type: AWS::IAM::Role
Properties:
PermissionsBoundary:
Fn::If:
- PermissionsBoundary
- Ref: PermissionsBoundary
- Ref: AWS::NoValue
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- cloudformation.amazonaws.com
- resources.cloudformation.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: ResourceTypePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:DescribeLogGroups"
- "logs:DescribeLogStreams"
- "logs:PutLogEvents"
- "cloudwatch:ListMetrics"
- "cloudwatch:PutMetricData"
Resource: "*"
RegisterTypeRole:
Type: AWS::IAM::Role
Properties:
PermissionsBoundary:
Fn::If:
- PermissionsBoundary
- Ref: PermissionsBoundary
- Ref: AWS::NoValue
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: ResourceTypePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "cloudformation:*"
- "iam:PassRole"
Resource: "*"
RegisterTypeFunction:
Type: "AWS::Lambda::Function"
Properties:
Timeout: 900
Runtime: python3.7
Handler: index.handler
Role: !GetAtt RegisterTypeRole.Arn
Code:
ZipFile: !Sub |
import cfnresponse
import logging
import boto3
from time import sleep
def stabilize(token, cfn):
p = cfn.describe_type_registration(RegistrationToken=token)
while p['ProgressStatus'] == "IN_PROGRESS":
sleep(5)
p = cfn.describe_type_registration(RegistrationToken=token)
if p['ProgressStatus'] == 'FAILED':
logging.error(p)
return cfnresponse.FAILED, p['TypeVersionArn']
return cfnresponse.SUCCESS, p['TypeVersionArn']
def register(cfn):
response = cfn.register_type(
Type='RESOURCE',
TypeName='AWSQS::EKS::Cluster',
SchemaHandlerPackage="s3://aws-quickstart/quickstart-amazon-eks-cluster-resource-provider/awsqs-eks-cluster.zip",
LoggingConfig={"LogRoleArn": "${LogDeliveryRole.Arn}", "LogGroupName": "awsqs-eks-cluster-logs"},
ExecutionRoleArn="${ExecutionRole.Arn}"
)
status, version_arn = stabilize(response['RegistrationToken'], cfn)
cfn.set_type_default_version(Arn=version_arn)
return status, version_arn
def handler(event, context):
print(event)
status = cfnresponse.SUCCESS
physical_id = event.get('PhysicalResourceId')
try:
cfn = boto3.client('cloudformation')
if event['RequestType'] == 'Create':
status, physical_id = register(cfn)
if event['RequestType'] == 'Update':
status, physical_id = register(cfn)
if event['RequestType'] == 'Delete':
versions = cfn.list_type_versions(Type='RESOURCE', TypeName='AWSQS::EKS::Cluster')['TypeVersionSummaries']
if len(versions) > 1:
cfn.deregister_type(Arn=physical_id)
else:
cfn.deregister_type(Type='RESOURCE', TypeName='AWSQS::EKS::Cluster')
except Exception:
logging.error('Unhandled exception', exc_info=True)
status = cfnresponse.FAILED
finally:
cfnresponse.send(event, context, status, {}, physicalResourceId=physical_id)
RegisterType:
Type: "AWS::CloudFormation::CustomResource"
Properties:
ServiceToken: !GetAtt RegisterTypeFunction.Arn