forked from awslabs/aiops-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-click-launch.yaml
309 lines (294 loc) · 12.4 KB
/
one-click-launch.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
AWSTemplateFormatVersion: 2010-09-09
Description: Creates AIOps setup for Developers
Resources:
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
ManagedPolicyArns:
- !Sub arn:${AWS::Partition}:iam::aws:policy/AWSCodeBuildAdminAccess
Policies:
- PolicyName: create
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: Logs
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*:*
- Sid: CFN
Effect: Allow
Action:
- cloudformation:DescribeStacks
- cloudformation:CreateChangeSet
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
- cloudformation:GetTemplate
- cloudformation:DescribeStackEvents
- cloudformation:DeleteStack
- cloudformation:DeleteChangeSet
Resource:
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/CDKToolkit
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/CDKToolkit/*
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/seedfarmer-aiops-*
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/seedfarmer-aiops-*/*
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/aws-codeseeder-aiops
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/aws-codeseeder-aiops/*
- Sid: IAM
Effect: Allow
Action:
- iam:GetRole
- iam:CreateRole
- iam:AttachRolePolicy
- iam:DetachRolePolicy
- iam:DeleteRole
- iam:PutRolePolicy
- iam:DeleteRolePolicy
- iam:GetRolePolicy
- iam:TagRole
- iam:UpdateAssumeRolePolicy
Resource:
- !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/seedfarmer-aiops-toolchain-role
- !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/seedfarmer-aiops-deployment-role
CreateUpdateCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Description: Builds AIOps Solution Using Seedfarmer
ServiceRole: !GetAtt CodeBuildRole.Arn
Artifacts:
Type: NO_ARTIFACTS
TimeoutInMinutes: 480
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_LARGE
Image: aws/codebuild/standard:7.0
EnvironmentVariables:
- Name: AWS_ACCOUNT_ID
Value: !Ref AWS::AccountId
- Name: AWS_REGION
Value: !Ref AWS::Region
- Name: AWS_DEFAULT_REGION
Value: !Ref AWS::Region
- Name: PRIMARY_ACCOUNT
Value: !Ref AWS::AccountId
- Name: PRIMARY_REGION
Value: !Ref AWS::Region
- Name: ROLE_ARN
Value: !GetAtt CodeBuildRole.Arn
- Name: url_path
Value: placeholder
- Name: url_query
Value: placeholder
- Name: cfn_signal_url
Value: placeholder
- Name: cfn_stack_id
Value: placeholder
- Name: cfn_logical_resource_id
Value: placeholder
- Name: cfn_request_id
Value: placeholder
Source:
Type: NO_SOURCE
BuildSpec: |
version: 0.2
env:
shell: bash
phases:
install:
runtime-versions:
python: 3.11
nodejs: 18
pre_build:
commands:
- pip install seed-farmer
- apt-get install jq git
- npm install -g aws-cdk
build:
commands:
- echo Build started on `date`
- echo 'Cloning aiops repo...'
- git clone https://github.com/awslabs/aiops-modules.git
- echo 'Cloning complete'
- cd aiops-modules/
- echo 'Installing dependencies...'
- pip install -r requirements.txt
- echo 'Python environment setup complete'
- echo 'Bootstrapping CDK...'
- cdk bootstrap aws://${AWS_ACCOUNT_ID}/${AWS_REGION}
- echo 'CDK bootstrap complete'
- echo 'Bootstrapping seedfarmer...'
- seedfarmer bootstrap toolchain --project aiops --trusted-principal ${ROLE_ARN} --as-target
- python scripts/role_assume_update.py ${ROLE_ARN}
- echo 'Sleeping 120 seconds after role update'
- sleep 120
- echo 'Preparing Manifest files'
- sed -i "s/us-east-1/${AWS_REGION}/g" manifests/*.yaml
- echo 'Deploying AIOPS manifest'
- seedfarmer apply manifests/uber-deployment.yaml --region ${AWS_REGION} --enable-session-timeout
- echo 'Deployment of aiops-modules manifest complete'
post_build:
commands:
- echo Build completed on `date`
- echo Signal back if we have gotten this far
- echo url_path - $url_path
- echo url_query - $url_query
- export UUID=1233244324
- |
STATUS='SUCCESS'
if [ $CODEBUILD_BUILD_SUCCEEDING -ne 1 ] # Test if the build is failing
then
STATUS='FAILURE'
fi
cat <<EOF > /tmp/payload.json
{
"UniqueId": "$UUID",
"Status": "$STATUS",
"Reason": "$STATUS",
"Data": "Deployment of solution has finished or stopped. See status."
}
EOF
cat /tmp/payload.json
echo "Calling Callback URL: ${cfn_signal_url}"
curl -T /tmp/payload.json "$cfn_signal_url"
CodeBuildRun:
Type: AWS::CloudFormation::CustomResource
Properties:
ServiceToken: !GetAtt CodeBuildLambda.Arn
BuildProjectName: !Ref CreateUpdateCodeBuildProject
CallbackUrl: !Ref CodeBuildRunWaitConditionHandler01
CodeBuildRunWaitConditionHandler01:
Type: AWS::CloudFormation::WaitConditionHandle
CodeBuildRunWaitCondition:
Type: AWS::CloudFormation::WaitCondition
DependsOn: CodeBuildRun
Properties:
Count: 1
Handle: !Ref CodeBuildRunWaitConditionHandler01
Timeout: 18000
CodeBuildLambda:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
import http.client
import urllib.parse
import json
import boto3
import traceback
def lambda_handler(event, context):
account_id = context.invoked_function_arn.split(":")[4]
try:
print(("Received event: " + json.dumps(event, indent=2)))
response = get_response_dict(event)
if event['RequestType'] in ("Create", "Update"):
try:
print("Kicking off Build")
execute_build(event, event["ResourceProperties"]["BuildProjectName"])
send_response(event, get_response_dict(event), "SUCCESS")
except Exception as build_exce:
print("ERROR: Build threw exception")
print((repr(build_exce)))
return send_response(event, get_response_dict(event),
"FAILED", repr(build_exce))
else:
print("Build Kicked off ok CodeBuild should signal back")
return
elif event['RequestType'] == "Delete":
try:
send_response(event, get_response_dict(event), "SUCCESS")
except Exception as response_exce:
print("ERROR: Send Response threw exception")
print((repr(response_exce)))
return send_response(event, get_response_dict(event),
"FAILED", repr(response_exce))
else:
return
else:
print("ERROR: Invalid request type send error signal to cfn")
print("ERROR: Expected - Create, Update, Delete")
except Exception as unhandled:
response = get_response_dict(event)
return send_response(event, response, "FAILED",
"Unhandled exception, failing gracefully: " + str(unhandled))
def execute_build(event, project_name):
"""Kickoff CodeBuild Project."""
build = boto3.client('codebuild')
signal_url = event["ResourceProperties"]["CallbackUrl"]
stack_id = event["StackId"]
request_id = event["RequestId"]
logical_resource_id = event["LogicalResourceId"]
url = urllib.parse.urlparse(event['ResponseURL'])
response = build.start_build(
projectName=project_name, environmentVariablesOverride=[
{'name': 'url_path', 'value': url.path},
{'name': 'url_query', 'value': url.query},
{'name': 'cfn_signal_url', 'value': signal_url},
{'name': 'cfn_stack_id', 'value': stack_id},
{'name': 'cfn_request_id', 'value': request_id},
{'name': 'cfn_logical_resource_id', 'value': logical_resource_id}
])
return response
def get_response_dict(event):
response = {
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Status': 'SUCCESS',
'PhysicalResourceId': 'SeedFarmerDeployment'
}
return response
def send_response(event, response, status=None, reason=None):
if status is not None:
response['Status'] = status
if reason is not None:
response['Reason'] = reason
if 'ResponseURL' in event and event['ResponseURL']:
url = urllib.parse.urlparse(event['ResponseURL'])
body = json.dumps(response)
https = http.client.HTTPSConnection(url.hostname)
https.request('PUT', url.path+'?'+url.query, body)
print("Sent CFN Response")
return response
Handler: index.lambda_handler
Runtime: python3.12
Timeout: 300
Role: !GetAtt CodeBuildLambdaExecutionRole.Arn
CodeBuildLambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- codebuild:StartBuild
Resource:
- !GetAtt CreateUpdateCodeBuildProject.Arn
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !Sub arn:${AWS::Partition}:logs:*:*:*