-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
invoke.ts
190 lines (167 loc) · 6.17 KB
/
invoke.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
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';
/**
* Properties for invoking a Lambda function with LambdaInvoke
*/
export interface LambdaInvokeProps extends sfn.TaskStateBaseProps {
/**
* Lambda function to invoke
*/
readonly lambdaFunction: lambda.IFunction;
/**
* The JSON that will be supplied as input to the Lambda function
*
* @default - The state input (JSON path '$')
*/
readonly payload?: sfn.TaskInput;
/**
* Invocation type of the Lambda function
*
* @default InvocationType.REQUEST_RESPONSE
*/
readonly invocationType?: LambdaInvocationType;
/**
* Up to 3583 bytes of base64-encoded data about the invoking client
* to pass to the function.
*
* @default - No context
*/
readonly clientContext?: string;
/**
* Version or alias to invoke a published version of the function
*
* You only need to supply this if you want the version of the Lambda Function to depend
* on data in the state machine state. If not, you can pass the appropriate Alias or Version object
* directly as the `lambdaFunction` argument.
*
* @default - Version or alias inherent to the `lambdaFunction` object.
* @deprecated pass a Version or Alias object as lambdaFunction instead
*/
readonly qualifier?: string;
/**
* Invoke the Lambda in a way that only returns the payload response without additional metadata.
*
* The `payloadResponseOnly` property cannot be used if `integrationPattern`, `invocationType`,
* `clientContext`, or `qualifier` are specified.
* It always uses the REQUEST_RESPONSE behavior.
*
* @default false
*/
readonly payloadResponseOnly?: boolean;
/**
* Whether to retry on Lambda service exceptions.
*
* This handles `Lambda.ServiceException`, `Lambda.AWSLambdaException` and
* `Lambda.SdkClientException` with an interval of 2 seconds, a back-off rate
* of 2 and 6 maximum attempts.
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/bp-lambda-serviceexception.html
*
* @default true
*/
readonly retryOnServiceExceptions?: boolean;
}
/**
* Invoke a Lambda function as a Task
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-lambda.html
*/
export class LambdaInvoke extends sfn.TaskStateBase {
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [
sfn.IntegrationPattern.REQUEST_RESPONSE,
sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN,
];
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
protected readonly taskPolicies?: iam.PolicyStatement[];
private readonly integrationPattern: sfn.IntegrationPattern;
constructor(scope: Construct, id: string, private readonly props: LambdaInvokeProps) {
super(scope, id, props);
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE;
validatePatternSupported(this.integrationPattern, LambdaInvoke.SUPPORTED_INTEGRATION_PATTERNS);
if (this.integrationPattern === sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN
&& !sfn.FieldUtils.containsTaskToken(props.payload)) {
throw new Error('Task Token is required in `payload` for callback. Use JsonPath.taskToken to set the token.');
}
if (props.payloadResponseOnly &&
(props.integrationPattern || props.invocationType || props.clientContext || props.qualifier)) {
throw new Error(
"The 'payloadResponseOnly' property cannot be used if 'integrationPattern', 'invocationType', 'clientContext', or 'qualifier' are specified.",
);
}
this.taskMetrics = {
metricPrefixSingular: 'LambdaFunction',
metricPrefixPlural: 'LambdaFunctions',
metricDimensions: {
LambdaFunctionArn: this.props.lambdaFunction.functionArn,
...(this.props.qualifier && { Qualifier: this.props.qualifier }),
},
};
this.taskPolicies = [
new iam.PolicyStatement({
resources: this.props.lambdaFunction.resourceArnsForGrantInvoke,
actions: ['lambda:InvokeFunction'],
}),
];
if (props.retryOnServiceExceptions ?? true) {
// Best practice from https://docs.aws.amazon.com/step-functions/latest/dg/bp-lambda-serviceexception.html
this.addRetry({
errors: ['Lambda.ServiceException', 'Lambda.AWSLambdaException', 'Lambda.SdkClientException'],
interval: cdk.Duration.seconds(2),
maxAttempts: 6,
backoffRate: 2,
});
}
}
/**
* Provides the Lambda Invoke service integration task configuration
*/
/**
* @internal
*/
protected _renderTask(): any {
if (this.props.payloadResponseOnly) {
return {
Resource: this.props.lambdaFunction.functionArn,
...this.props.payload && { Parameters: sfn.FieldUtils.renderObject(this.props.payload.value) },
};
} else {
return {
Resource: integrationResourceArn('lambda', 'invoke', this.integrationPattern),
Parameters: sfn.FieldUtils.renderObject({
FunctionName: this.props.lambdaFunction.functionArn,
Payload: this.props.payload ? this.props.payload.value : sfn.TaskInput.fromJsonPathAt('$').value,
InvocationType: this.props.invocationType,
ClientContext: this.props.clientContext,
Qualifier: this.props.qualifier,
}),
};
}
}
}
/**
* Invocation type of a Lambda
*/
export enum LambdaInvocationType {
/**
* Invoke the function synchronously.
*
* Keep the connection open until the function returns a response or times out.
* The API response includes the function response and additional data.
*/
REQUEST_RESPONSE = 'RequestResponse',
/**
* Invoke the function 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',
/**
* Validate parameter values and verify that the user or role has permission to invoke the function.
*/
DRY_RUN = 'DryRun'
}