-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathawsCommands.ts
363 lines (328 loc) · 11.1 KB
/
awsCommands.ts
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import {
CloudWatchLogsClient,
CreateLogGroupCommand,
CreateLogGroupCommandOutput,
DeleteSubscriptionFilterCommand,
DeleteSubscriptionFilterCommandOutput,
DescribeSubscriptionFiltersCommand,
DescribeSubscriptionFiltersCommandOutput,
PutSubscriptionFilterCommand,
PutSubscriptionFilterCommandOutput,
} from '@aws-sdk/client-cloudwatch-logs'
import {
AttachRolePolicyCommand,
AttachRolePolicyCommandOutput,
CreatePolicyCommand,
CreatePolicyCommandOutput,
IAMClient,
} from '@aws-sdk/client-iam'
import {
DescribeStateMachineCommand,
DescribeStateMachineCommandOutput,
ListTagsForResourceCommand,
ListTagsForResourceCommandOutput,
LogLevel,
Tag,
TagResourceCommand,
TagResourceCommandOutput,
UntagResourceCommand,
UntagResourceCommandOutput,
UpdateStateMachineCommand,
UpdateStateMachineCommandOutput,
} from '@aws-sdk/client-sfn'
import {SFNClient} from '@aws-sdk/client-sfn/dist-types/SFNClient'
import {BaseContext} from 'clipanion'
import {buildLogAccessPolicyName, displayChanges, StateMachineDefinitionType} from './helpers'
export const describeStateMachine = async (
stepFunctionsClient: SFNClient,
stepFunctionArn: string
): Promise<DescribeStateMachineCommandOutput> => {
const input = {stateMachineArn: stepFunctionArn}
const command = new DescribeStateMachineCommand(input)
const data = await stepFunctionsClient.send(command)
return data
}
export const listTagsForResource = async (
stepFunctionsClient: SFNClient,
stepFunctionArn: string
): Promise<ListTagsForResourceCommandOutput> => {
const input = {resourceArn: stepFunctionArn}
const command = new ListTagsForResourceCommand(input)
const data = await stepFunctionsClient.send(command)
return data
}
export const putSubscriptionFilter = async (
cloudWatchLogsClient: CloudWatchLogsClient,
forwarderArn: string,
filterName: string,
logGroupName: string,
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<PutSubscriptionFilterCommandOutput | undefined> => {
// Running this function multiple times would not create duplicate filters (old filter with the same name would be overwritten).
// However, two filters with the same destination forwarder can exist when the filter names are different.
const input = {
destinationArn: forwarderArn,
filterName,
filterPattern: '',
logGroupName,
}
const command = new PutSubscriptionFilterCommand(input)
const commandName = 'PutSubscriptionFilter'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
if (!dryRun) {
const data = await cloudWatchLogsClient.send(command)
// Even if the same filter name is created before, the response is still 200.
// there are no way to tell
context.stdout.write(
`Subscription filter ${filterName} is created or the original filter ${filterName} is overwritten.\n\n`
)
return data
}
}
export const tagResource = async (
stepFunctionsClient: SFNClient,
stepFunctionArn: string,
tags: Tag[],
context: BaseContext,
dryRun: boolean
): Promise<TagResourceCommandOutput | undefined> => {
const input = {
resourceArn: stepFunctionArn,
tags,
}
const command = new TagResourceCommand(input)
const commandName = 'TagResource'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
if (!dryRun) {
const data = await stepFunctionsClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
}
export const createLogGroup = async (
cloudWatchLogsClient: CloudWatchLogsClient,
logGroupName: string,
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<CreateLogGroupCommandOutput | undefined> => {
const input = {
logGroupName,
}
const command = new CreateLogGroupCommand(input)
const commandName = 'CreateLogGroup'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
try {
if (!dryRun) {
const data = await cloudWatchLogsClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
} catch (err) {
// if a resource already exists it's a warning since we can use that resource instead of creating it
if (err instanceof Error && err.name === 'ResourceAlreadyExistsException') {
context.stdout.write(
` -> [Info] ${err.message}. Skipping resource creation and continuing with instrumentation.\n`
)
}
}
}
export const createLogsAccessPolicy = async (
iamClient: IAMClient,
describeStateMachineCommandOutput: DescribeStateMachineCommandOutput,
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<CreatePolicyCommandOutput | undefined> => {
// according to https://docs.aws.amazon.com/step-functions/latest/dg/cw-logs.html#cloudwatch-iam-policy
const logsAccessPolicy = {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: [
'logs:CreateLogDelivery',
'logs:CreateLogStream',
'logs:GetLogDelivery',
'logs:UpdateLogDelivery',
'logs:DeleteLogDelivery',
'logs:ListLogDeliveries',
'logs:PutLogEvents',
'logs:PutResourcePolicy',
'logs:DescribeResourcePolicies',
'logs:DescribeLogGroups',
],
Resource: '*',
},
],
}
const input = {
PolicyDocument: JSON.stringify(logsAccessPolicy),
PolicyName: buildLogAccessPolicyName(describeStateMachineCommandOutput),
}
const command = new CreatePolicyCommand(input)
const commandName = 'CreatePolicy'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
try {
if (!dryRun) {
const data = await iamClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
} catch (err) {
// if a resource already exists it's a warning since we can use that resource instead of creating it
if (err instanceof Error && err.name === 'ResourceAlreadyExistsException') {
context.stdout.write(
` -> [Info] ${err.message}. Skipping resource creation and continuing with instrumentation.\n`
)
}
}
}
export const attachPolicyToStateMachineIamRole = async (
iamClient: IAMClient,
describeStateMachineCommandOutput: DescribeStateMachineCommandOutput,
accountId: string,
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<AttachRolePolicyCommandOutput | undefined> => {
const splitRoleArnList = describeStateMachineCommandOutput?.roleArn?.split('/')
if (splitRoleArnList === undefined) {
throw Error(
`Unexpected roleArn ${describeStateMachineCommandOutput?.roleArn} for the describeStateMachineCommandOutput ${describeStateMachineCommandOutput}`
)
}
// `arn:aws:iam::<accountId>:role/<name>` or `arn:aws:iam::<accountId>:role/service-role/<name>`
const roleName = splitRoleArnList[splitRoleArnList.length - 1]
const policyArn = `arn:aws:iam::${accountId}:policy/${buildLogAccessPolicyName(describeStateMachineCommandOutput)}`
const input = {
PolicyArn: policyArn,
RoleName: roleName,
}
const command = new AttachRolePolicyCommand(input)
const commandName = 'AttachRolePolicy'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
if (!dryRun) {
const data = await iamClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
}
export const enableStepFunctionLogs = async (
stepFunctionsClient: SFNClient,
stepFunction: DescribeStateMachineCommandOutput,
logGroupArn: string,
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<UpdateStateMachineCommandOutput | undefined> => {
const input = {
stateMachineArn: stepFunction.stateMachineArn,
loggingConfiguration: {
destinations: [{cloudWatchLogsLogGroup: {logGroupArn}}],
level: LogLevel.ALL,
includeExecutionData: true,
},
}
const previousParams = {
stateMachineArn: stepFunction.stateMachineArn,
loggingConfiguration: stepFunction.loggingConfiguration,
}
const command = new UpdateStateMachineCommand(input)
const commandName = 'UpdateStateMachine'
displayChanges(stepFunctionArn, context, commandName, dryRun, input, previousParams)
if (!dryRun) {
const data = await stepFunctionsClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
}
export const updateStateMachineDefinition = async (
stepFunctionsClient: SFNClient,
stepFunction: DescribeStateMachineCommandOutput,
definitionObj: StateMachineDefinitionType,
context: BaseContext,
dryRun: boolean
): Promise<UpdateStateMachineCommandOutput | undefined> => {
if (stepFunction === undefined) {
return
}
const input = {
stateMachineArn: stepFunction.stateMachineArn,
definition: JSON.stringify(definitionObj),
}
const command = new UpdateStateMachineCommand(input)
context.stdout.write(
`Going to inject Step Function context into lambda payload in steps of ${stepFunction.stateMachineArn}.\n\n`
)
if (!dryRun) {
try {
const data = await stepFunctionsClient.send(command)
context.stdout.write(
`Step Function context is injected into lambda payload in steps of ${stepFunction.stateMachineArn}\n\n`
)
return data
} catch (err) {
if (err instanceof Error) {
context.stdout.write(
`\n[Error] ${err.message}. Failed to inject context into lambda functions' payload of ${stepFunction.stateMachineArn} \n`
)
}
}
}
}
export const deleteSubscriptionFilter = async (
cloudWatchLogsClient: CloudWatchLogsClient,
filterName: string,
logGroupName: string,
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<DeleteSubscriptionFilterCommandOutput | undefined> => {
const input = {
filterName,
logGroupName,
}
const command = new DeleteSubscriptionFilterCommand(input)
const commandName = 'DeleteSubscriptionFilter'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
if (!dryRun) {
const data = await cloudWatchLogsClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
}
export const describeSubscriptionFilters = async (
cloudWatchLogsClient: CloudWatchLogsClient,
logGroupName: string
): Promise<DescribeSubscriptionFiltersCommandOutput> => {
const input = {logGroupName}
const command = new DescribeSubscriptionFiltersCommand(input)
const data = await cloudWatchLogsClient.send(command)
return data
}
const printSuccessfulMessage = (commandName: string, context: BaseContext): void => {
context.stdout.write(`${commandName} finished successfully!\n\n`)
}
export const untagResource = async (
stepFunctionsClient: SFNClient,
tagKeys: string[],
stepFunctionArn: string,
context: BaseContext,
dryRun: boolean
): Promise<UntagResourceCommandOutput | undefined> => {
const input = {
resourceArn: stepFunctionArn,
tagKeys,
}
const command = new UntagResourceCommand(input)
const commandName = 'UntagResource'
displayChanges(stepFunctionArn, context, commandName, dryRun, input)
if (!dryRun) {
const data = await stepFunctionsClient.send(command)
printSuccessfulMessage(commandName, context)
return data
}
}