forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stepfunctions): new service integration classes for Lambda, SNS,…
… and SQS (aws#7946) merge functionality currently modeled under task and state into a single object that represents a task state as an abstract class. Service integrations extend the base class and add service specific configuration, metrics, and policies. this commit introduces the "new" service integrations for Lambda, SNS, and SQS Motivation: The current service integrations that are offered in `aws-stepfunctions-tasks` all currently implement a `bind()` method and contribute a portion of the configuration that creates a `Task` state. However, it's often useful to configure state level properties such as paths, retries, errors based on the service integration and the pattern that has been requested. Implementation: * Duplicate the current `Task` class and merge the properties of a task state and a task service integration into a new abstract base class. * Concrete implementation per service integration class where all of the best practices and user intents can be encoded * After all the service integrations have been migrated, we will want to retire the `Task` class as well since we don't want it to be instantiated. Paves the way for: aws#6715 by simplifying the invocation of service integration calls. We would be able to start adding properties such as retries and errors. aws#6489 where we will be making service integrations consistent ### End Commit Message ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
8f95e8d
commit baafb83
Showing
25 changed files
with
2,491 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/@aws-cdk/aws-stepfunctions-tasks/lib/lambda/invoke.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
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 { 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. | ||
*/ | ||
readonly qualifier?: string; | ||
} | ||
|
||
/** | ||
* 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: cdk.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 Context.taskToken to set the token.'); | ||
} | ||
|
||
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.functionArn], | ||
actions: ['lambda:InvokeFunction'], | ||
}), | ||
]; | ||
} | ||
|
||
/** | ||
* Provides the Lambda Invoke service integration task configuration | ||
*/ | ||
protected renderTask(): any { | ||
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.fromDataAt('$').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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/@aws-cdk/aws-stepfunctions-tasks/lib/private/task-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
IntegrationPattern, | ||
} from '@aws-cdk/aws-stepfunctions'; | ||
import { Aws } from '@aws-cdk/core'; | ||
|
||
/** | ||
* Verifies that a validation pattern is supported for a service integration | ||
* | ||
*/ | ||
export function validatePatternSupported(integrationPattern: IntegrationPattern, supportedPatterns: IntegrationPattern[]) { | ||
if (!supportedPatterns.includes(integrationPattern)) { | ||
throw new Error(`Unsupported service integration pattern. Supported Patterns: ${supportedPatterns}. Received: ${integrationPattern}`); | ||
} | ||
} | ||
|
||
/** | ||
* Suffixes corresponding to different service integration patterns | ||
* | ||
* Key is the service integration pattern, value is the resource ARN suffix. | ||
* | ||
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html | ||
*/ | ||
const resourceArnSuffix: Record<IntegrationPattern, string> = { | ||
[IntegrationPattern.REQUEST_RESPONSE]: '', | ||
[IntegrationPattern.RUN_JOB]: '.sync', | ||
[IntegrationPattern.WAIT_FOR_TASK_TOKEN]: '.waitForTaskToken', | ||
}; | ||
|
||
export function integrationResourceArn(service: string, api: string, integrationPattern: IntegrationPattern): string { | ||
if (!service || !api) { | ||
throw new Error("Both 'service' and 'api' must be provided to build the resource ARN."); | ||
} | ||
return `arn:${Aws.PARTITION}:states:::${service}:${api}` + | ||
(integrationPattern ? resourceArnSuffix[integrationPattern] : ''); | ||
} |
Oops, something went wrong.