-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathrun-lambda-task.ts
125 lines (111 loc) · 3.92 KB
/
run-lambda-task.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
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
import sfn = require('@aws-cdk/aws-stepfunctions');
import { getResourceArn } from './resource-arn-suffix';
/**
* Properties for RunLambdaTask
*/
export interface RunLambdaTaskProps {
/**
* The JSON that you want to provide to your Lambda function as input.
*/
readonly payload?: { [key: string]: any };
/**
* The service integration pattern indicates different ways to invoke Lambda function.
*
* The valid value for Lambda is either FIRE_AND_FORGET or WAIT_FOR_TASK_TOKEN,
* it determines whether to pause the workflow until a task token is returned.
*
* If this is set to WAIT_FOR_TASK_TOKEN, the Context.taskToken value must be included
* somewhere in the payload and the Lambda must call
* `SendTaskSuccess/SendTaskFailure` using that token.
*
* @default FIRE_AND_FORGET
*/
readonly integrationPattern?: sfn.ServiceIntegrationPattern;
/**
* Invocation type of the Lambda function
*
* @default RequestResponse
*/
readonly invocationType?: InvocationType;
/**
* Client context to pass to the function
*
* @default - No context
*/
readonly clientContext?: string;
/**
* Version or alias of the function to be invoked
*
* @default - No qualifier
*/
readonly qualifier?: string;
}
/**
* Invoke a Lambda function as a Task
*
* OUTPUT: the output of this task is either the return value of Lambda's
* Invoke call, or whatever the Lambda Function posted back using
* `SendTaskSuccess/SendTaskFailure` in `waitForTaskToken` mode.
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-lambda.html
*/
export class RunLambdaTask implements sfn.IStepFunctionsTask {
private readonly integrationPattern: sfn.ServiceIntegrationPattern;
constructor(private readonly lambdaFunction: lambda.IFunction, private readonly props: RunLambdaTaskProps = {}) {
this.integrationPattern = props.integrationPattern || sfn.ServiceIntegrationPattern.FIRE_AND_FORGET;
const supportedPatterns = [
sfn.ServiceIntegrationPattern.FIRE_AND_FORGET,
sfn.ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN
];
if (!supportedPatterns.includes(this.integrationPattern)) {
throw new Error(`Invalid Service Integration Pattern: ${this.integrationPattern} is not supported to call Lambda.`);
}
if (this.integrationPattern === sfn.ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN
&& !sfn.FieldUtils.containsTaskToken(props.payload)) {
throw new Error('Task Token is missing in payload (pass Context.taskToken somewhere in payload)');
}
}
public bind(_task: sfn.Task): sfn.StepFunctionsTaskConfig {
return {
resourceArn: getResourceArn("lambda", "invoke", this.integrationPattern),
policyStatements: [new iam.PolicyStatement({
resources: [this.lambdaFunction.functionArn],
actions: ["lambda:InvokeFunction"],
})],
metricPrefixSingular: 'LambdaFunction',
metricPrefixPlural: 'LambdaFunctions',
metricDimensions: { LambdaFunctionArn: this.lambdaFunction.functionArn },
parameters: {
FunctionName: this.lambdaFunction.functionName,
Payload: this.props.payload,
InvocationType: this.props.invocationType,
ClientContext: this.props.clientContext,
Qualifier: this.props.qualifier
}
};
}
}
/**
* Invocation type of a Lambda
*/
export enum InvocationType {
/**
* Invoke synchronously
*
* The API response includes the function response and additional data.
*/
REQUEST_RESPONSE = 'RequestResponse',
/**
* Invoke asynchronously
*
* Send events that fail multiple times to the function's dead-letter queue (if it's configured).
* The API response only includes a status code.
*/
EVENT = 'Event',
/**
* TValidate parameter values and verify that the user or role has permission to invoke the function.
*/
DRY_RUN = 'DryRun'
}