-
Notifications
You must be signed in to change notification settings - Fork 4k
/
run-lambda-task.ts
131 lines (117 loc) · 4.15 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
126
127
128
129
130
131
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 { getResourceArn } from '../resource-arn-suffix';
/**
* Properties for RunLambdaTask
*
* @deprecated Use `LambdaInvoke`
*/
export interface RunLambdaTaskProps {
/**
* The JSON that you want to provide to your Lambda function as input.
*
* @default - The state input (JSON path '$')
*/
readonly payload?: sfn.TaskInput;
/**
* 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 JsonPath.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
* @deprecated Use `LambdaInvoke`
*/
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 JsonPath.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 ? 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
* @deprecated use `LambdaInvocationType`
*/
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'
}