generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1k
added cdk-sfn-dmap-df pattern #2994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marcojahn
wants to merge
3
commits into
aws-samples:main
Choose a base branch
from
marcojahn:cdk-sfn-dmap-df
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,149 @@ | ||
| # AWS Step Functions Distributed Map with Lambda durable functions | ||
|
|
||
| This pattern demonstrates how to use AWS Step Functions Distributed Map with an Amazon S3 JSON input to fan out across 50 product catalog items, invoking a Lambda durable function for each item. The key technique is using the AWS Step Functions AWS SDK service integration (`CallAwsService` targeting `lambda:invoke`) instead of the optimized Lambda integration. This is currently necessary because only the raw SDK integration exposes the `DurableExecutionName` parameter, which enables per-item idempotency, derived from each product's `itemId`, showcased in this example. | ||
|
|
||
| Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/cdk-sfn-dmap-df](https://serverlessland.com/patterns/cdk-sfn-dmap-df) | ||
|
|
||
| Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||
|
|
||
| ## Requirements | ||
|
|
||
| * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||
| * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
| * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
| * [Node.js and npm](https://nodejs.org/) installed (Node.js 22+) | ||
| * [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html) installed | ||
| * CDK bootstrapped in your target account/region | ||
|
|
||
| ## Deployment Instructions | ||
|
|
||
| 1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: | ||
|
|
||
| ```bash | ||
| git clone https://github.com/aws-samples/serverless-patterns | ||
| ``` | ||
|
|
||
| 2. Change directory to the pattern directory: | ||
|
|
||
| ```bash | ||
| cd cdk-sfn-dmap-df | ||
| ``` | ||
|
|
||
| 3. Install dependencies: | ||
|
|
||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| 4. Run tests: | ||
|
|
||
| ```bash | ||
| npm test | ||
| ``` | ||
|
|
||
| This runs the CDK stack assertions and the local durable function tests using `LocalDurableTestRunner`. The durable function tests verify the three-operation workflow (validate → wait → update) and price tier assignment without deploying to AWS. | ||
|
|
||
| 5. Deploy the CDK stack to your default AWS account and region: | ||
|
|
||
| ```bash | ||
| cdk deploy | ||
| ``` | ||
|
|
||
| 6. Note the outputs from the CDK deployment process. These contain the resource names and ARNs used for testing. | ||
|
|
||
| ## How it works | ||
|
|
||
|  | ||
|
|
||
| Architecture flow: | ||
| 1. AWS Step Functions Distributed Map reads 50 product items from an Amazon S3 JSON file | ||
| 2. For each item, the map invokes a Lambda durable function via the AWS SDK service integration (`lambda:invoke`) | ||
| 3. The `DurableExecutionName` is derived from each item's `itemId` using `States.Format`, providing per-item idempotency | ||
| 4. Each durable function executes a three-operation workflow: | ||
| - **`validate-item`** (step) — Validates required fields, checks price > 0, computes a pricing tier (budget / standard / premium) | ||
| - **`rate-limit-delay`** (wait) — Pauses 5 seconds to simulate downstream rate limiting. No compute charges during this wait | ||
| - **`update-catalog`** (step) — Writes the enriched catalog entry with processing timestamps and a `completed` status | ||
| 5. Results are written back to Amazon S3 under the `results/` prefix | ||
|
|
||
| ### Why the AWS SDK Service Integration? | ||
|
|
||
| AWS Step Functions offers two ways to invoke AWS Lambda: | ||
|
|
||
| | Integration | ARN Pattern | `DurableExecutionName` Support | | ||
| |---|---|---| | ||
| | Optimized Lambda | `arn:aws:states:::lambda:invoke` | No | | ||
| | AWS SDK | `arn:aws:states:::aws-sdk:lambda:invoke` | Yes | | ||
|
|
||
| The optimized integration is simpler but only exposes a subset of the `Lambda.Invoke` API parameters. The AWS SDK integration maps directly to the full `Lambda.Invoke` API, giving access to `DurableExecutionName`. In CDK, this is expressed with `CallAwsService`: | ||
|
|
||
| ```typescript | ||
| new tasks.CallAwsService(this, 'InvokeDurableFunction', { | ||
| service: 'lambda', | ||
| action: 'invoke', | ||
| parameters: { | ||
| 'FunctionName': `${itemProcessor.functionArn}:$LATEST`, | ||
| 'InvocationType': 'RequestResponse', | ||
| 'DurableExecutionName.$': "States.Format('dmap-item-{}', $.itemId)", | ||
| 'Payload.$': '$', | ||
| }, | ||
| iamResources: [ | ||
| `${itemProcessor.functionArn}:$LATEST`, | ||
| itemProcessor.functionArn, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| The `DurableExecutionName` is derived from each item's `itemId` using `States.Format`, so re-running the state machine with the same input file produces the same execution names. This means the durable functions return their previously checkpointed results instead of re-executing, providing end-to-end idempotency from AWS Step Functions through to durable function state. | ||
|
|
||
| ## Testing | ||
|
|
||
| ### Start the State Machine | ||
|
|
||
| ```bash | ||
| aws stepfunctions start-execution \ | ||
| --state-machine-arn <StateMachineArn from stack output> \ | ||
| --name "catalog-update-$(date +%s)" | ||
| ``` | ||
|
|
||
| ### Monitor Execution | ||
|
|
||
| Monitor the execution in the AWS Step Functions console. The Distributed Map view shows all 50 child executions and their status. Each child invokes the durable function synchronously with a unique `DurableExecutionName`. Results are written back to the Amazon S3 bucket under the `results/` prefix. | ||
|
|
||
| ### Verify Deployment | ||
|
|
||
| ```bash | ||
| # Confirm items.json was deployed to S3 | ||
| aws s3 ls s3://<DataBucketName from stack output>/items.json | ||
|
|
||
| # Confirm the state machine exists | ||
| aws stepfunctions describe-state-machine \ | ||
| --state-machine-arn <StateMachineArn from stack output> | ||
| ``` | ||
|
|
||
| ### Re-running for Idempotency | ||
|
|
||
| To demonstrate idempotency, start another execution with the same input: | ||
|
|
||
| ```bash | ||
| aws stepfunctions start-execution \ | ||
| --state-machine-arn <StateMachineArn from stack output> \ | ||
| --name "catalog-update-rerun-$(date +%s)" | ||
| ``` | ||
|
|
||
| Since the `DurableExecutionName` values are derived from the `itemId` (which does not change between runs), the durable functions detect that executions with those names already completed and return the previously checkpointed results without re-executing any steps. | ||
|
|
||
| ## Cleanup | ||
|
|
||
| 1. Delete the stack: | ||
|
|
||
| ```bash | ||
| cdk destroy | ||
| ``` | ||
|
|
||
| 2. Confirm the deletion when prompted. This removes all resources including the Amazon S3 bucket (configured with `autoDeleteObjects` and `RemovalPolicy.DESTROY`) and Amazon CloudWatch log groups. | ||
|
|
||
| --- | ||
|
|
||
| Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: MIT-0 | ||
This file contains hidden or 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,20 @@ | ||
| #!/usr/bin/env node | ||
| import * as cdk from 'aws-cdk-lib/core'; | ||
| import { CdkSfnDmapDfStack } from '../lib/cdk-sfn-dmap-df-stack'; | ||
|
|
||
| const app = new cdk.App(); | ||
| new CdkSfnDmapDfStack(app, 'CdkSfnDmapDfStack', { | ||
| /* If you don't specify 'env', this stack will be environment-agnostic. | ||
| * Account/Region-dependent features and context lookups will not work, | ||
| * but a single synthesized template can be deployed anywhere. */ | ||
|
|
||
| /* Uncomment the next line to specialize this stack for the AWS Account | ||
| * and Region that are implied by the current CLI configuration. */ | ||
| // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, | ||
|
|
||
| /* Uncomment the next line if you know exactly what Account and Region you | ||
| * want to deploy the stack to. */ | ||
| // env: { account: '123456789012', region: 'us-east-1' }, | ||
|
|
||
| /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */ | ||
| }); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,103 @@ | ||
| { | ||
| "app": "npx ts-node --prefer-ts-exts bin/cdk-sfn-dmap-df.ts", | ||
| "watch": { | ||
| "include": [ | ||
| "**" | ||
| ], | ||
| "exclude": [ | ||
| "README.md", | ||
| "cdk*.json", | ||
| "**/*.d.ts", | ||
| "**/*.js", | ||
| "tsconfig.json", | ||
| "package*.json", | ||
| "yarn.lock", | ||
| "node_modules", | ||
| "test" | ||
| ] | ||
| }, | ||
| "context": { | ||
| "@aws-cdk/aws-signer:signingProfileNamePassedToCfn": true, | ||
| "@aws-cdk/aws-ecs-patterns:secGroupsDisablesImplicitOpenListener": true, | ||
| "@aws-cdk/aws-lambda:recognizeLayerVersion": true, | ||
| "@aws-cdk/core:checkSecretUsage": true, | ||
| "@aws-cdk/core:target-partitions": [ | ||
| "aws", | ||
| "aws-cn" | ||
| ], | ||
| "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, | ||
| "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, | ||
| "@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true, | ||
| "@aws-cdk/aws-iam:minimizePolicies": true, | ||
| "@aws-cdk/core:validateSnapshotRemovalPolicy": true, | ||
| "@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, | ||
| "@aws-cdk/aws-s3:createDefaultLoggingPolicy": true, | ||
| "@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true, | ||
| "@aws-cdk/aws-apigateway:disableCloudWatchRole": true, | ||
| "@aws-cdk/core:enablePartitionLiterals": true, | ||
| "@aws-cdk/aws-events:eventsTargetQueueSameAccount": true, | ||
| "@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true, | ||
| "@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true, | ||
| "@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true, | ||
| "@aws-cdk/aws-route53-patters:useCertificate": true, | ||
| "@aws-cdk/customresources:installLatestAwsSdkDefault": false, | ||
| "@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true, | ||
| "@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true, | ||
| "@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true, | ||
| "@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true, | ||
| "@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true, | ||
| "@aws-cdk/aws-redshift:columnId": true, | ||
| "@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true, | ||
| "@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true, | ||
| "@aws-cdk/aws-apigateway:requestValidatorUniqueId": true, | ||
| "@aws-cdk/aws-kms:aliasNameRef": true, | ||
| "@aws-cdk/aws-kms:applyImportedAliasPermissionsToPrincipal": true, | ||
| "@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true, | ||
| "@aws-cdk/core:includePrefixInUniqueNameGeneration": true, | ||
| "@aws-cdk/aws-efs:denyAnonymousAccess": true, | ||
| "@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true, | ||
| "@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true, | ||
| "@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true, | ||
| "@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true, | ||
| "@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true, | ||
| "@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true, | ||
| "@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true, | ||
| "@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true, | ||
| "@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true, | ||
| "@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true, | ||
| "@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true, | ||
| "@aws-cdk/aws-eks:nodegroupNameAttribute": true, | ||
| "@aws-cdk/aws-eks:useNativeOidcProvider": true, | ||
| "@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true, | ||
| "@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true, | ||
| "@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false, | ||
| "@aws-cdk/aws-s3:keepNotificationInImportedBucket": false, | ||
| "@aws-cdk/core:explicitStackTags": true, | ||
| "@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true, | ||
| "@aws-cdk/aws-dynamodb:resourcePolicyPerReplica": true, | ||
| "@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true, | ||
| "@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true, | ||
| "@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId": true, | ||
| "@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true, | ||
| "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true, | ||
| "@aws-cdk/aws-stepfunctions-tasks:fixRunEcsTaskPolicy": true, | ||
| "@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault": true, | ||
| "@aws-cdk/aws-route53-targets:userPoolDomainNameMethodWithoutCustomResource": true, | ||
| "@aws-cdk/aws-elasticloadbalancingV2:albDualstackWithoutPublicIpv4SecurityGroupRulesDefault": true, | ||
| "@aws-cdk/aws-iam:oidcRejectUnauthorizedConnections": true, | ||
| "@aws-cdk/core:enableAdditionalMetadataCollection": true, | ||
| "@aws-cdk/aws-lambda:createNewPoliciesWithAddToRolePolicy": false, | ||
| "@aws-cdk/aws-s3:setUniqueReplicationRoleName": true, | ||
| "@aws-cdk/aws-events:requireEventBusPolicySid": true, | ||
| "@aws-cdk/core:aspectPrioritiesMutating": true, | ||
| "@aws-cdk/aws-dynamodb:retainTableReplica": true, | ||
| "@aws-cdk/aws-stepfunctions:useDistributedMapResultWriterV2": true, | ||
| "@aws-cdk/s3-notifications:addS3TrustKeyPolicyForSnsSubscriptions": true, | ||
| "@aws-cdk/aws-ec2:requirePrivateSubnetsForEgressOnlyInternetGateway": true, | ||
| "@aws-cdk/aws-s3:publicAccessBlockedByDefault": true, | ||
| "@aws-cdk/aws-lambda:useCdkManagedLogGroup": true, | ||
| "@aws-cdk/aws-elasticloadbalancingv2:networkLoadBalancerWithSecurityGroupByDefault": true, | ||
| "@aws-cdk/aws-ecs-patterns:uniqueTargetGroupId": true, | ||
| "@aws-cdk/aws-route53-patterns:useDistribution": true | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.