Skip to content

Commit 6fc9138

Browse files
committed
update
1 parent 0087452 commit 6fc9138

File tree

11 files changed

+171
-101
lines changed

11 files changed

+171
-101
lines changed

cdk-sfn-s3/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out

cdk-sfn-s3/README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,11 @@ Important: this application uses various AWS services and there are costs associ
2727
```
2828
npm install
2929
```
30-
4. This project uses typescript as client language for AWS CDK. Run the given command to compile typescript to javascript:
31-
```
32-
npm run build
33-
```
34-
5. Synthesize CloudFormation template from the AWS CDK app:
35-
```
36-
cdk synth
37-
```
38-
6. Deploy the stack to your default AWS account and region. The output of this command should give you the StepFunctions State Machine ARN.
30+
4. Deploy the stack to your default AWS account and region. The output of this command should give you the StepFunctions State Machine ARN.
3931
```
4032
cdk deploy
4133
```
42-
8. Note the outputs from the CDK deployment process. These contain the ARN of StepFunctions State Machine.
34+
5. Note the outputs from the CDK deployment process. These contain the ARN of StepFunctions State Machine.
4335
4436
## How it works
4537
@@ -52,11 +44,11 @@ Use the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2
5244
5345
1. Execute the Step Functions workflow:
5446
```
55-
aws stepfunctions start-execution --state-machine-arn ENTER_YOUR_STATE_MACHINE_ARN
47+
aws stepfunctions start-execution --state-machine-arn <STATE_MACHINE_ARN>
5648
```
5749
2. View S3 bucket to see files created in the previously empty bucket: You should see a file has been saved to the S3 bucket:
5850
```
59-
aws s3 ls my-sfn-bucket-destination
51+
aws s3 ls <S3_BUCKET_NAME>
6052
2022-06-06 14:53:12 17 filename.txt
6153
```
6254
@@ -68,7 +60,7 @@ Use the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2
6860
```
6961
2. Confirm the stack has been deleted
7062
```
71-
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'ENTER_STACK_NAME')].StackStatus"
63+
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'<STACK_NAME>')].StackStatus"
7264
```
7365
7466
## Useful commands

cdk-sfn-s3/bin/cdk-sfn-s3.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
#!/usr/bin/env node
2-
import 'source-map-support/register';
3-
import * as cdk from '@aws-cdk/core';
4-
import { CdkSfnS3Stack } from '../lib/cdk-sfn-s3-stack';
2+
import "source-map-support/register";
3+
import * as cdk from "aws-cdk-lib";
4+
import { SfnS3Stack } from "../lib/sfn-s3-stack";
55

66
const app = new cdk.App();
7-
new CdkSfnS3Stack(app, 'Cdk-Sfn-S3-Stack', {
7+
new SfnS3Stack(app, "CdkSfnS3Stack", {
88
/* If you don't specify 'env', this stack will be environment-agnostic.
99
* Account/Region-dependent features and context lookups will not work,
1010
* but a single synthesized template can be deployed anywhere. */
11-
1211
/* Uncomment the next line to specialize this stack for the AWS Account
1312
* and Region that are implied by the current CLI configuration. */
1413
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15-
1614
/* Uncomment the next line if you know exactly what Account and Region you
1715
* want to deploy the stack to. */
1816
// env: { account: '123456789012', region: 'us-east-1' },
19-
2017
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
2118
});

cdk-sfn-s3/cdk.json

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"app": "npx ts-node --prefer-ts-exts bin/cdk-sfn-s3.ts",
33
"watch": {
4-
"include": [
5-
"**"
6-
],
4+
"include": ["**"],
75
"exclude": [
86
"README.md",
97
"cdk*.json",
@@ -17,10 +15,48 @@
1715
]
1816
},
1917
"context": {
20-
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
21-
"@aws-cdk/core:stackRelativeExports": true,
22-
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
23-
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
24-
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
18+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
19+
"@aws-cdk/core:checkSecretUsage": true,
20+
"@aws-cdk/core:target-partitions": ["aws", "aws-cn"],
21+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
22+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
23+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
24+
"@aws-cdk/aws-iam:minimizePolicies": true,
25+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
26+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
27+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
28+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
29+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
30+
"@aws-cdk/core:enablePartitionLiterals": true,
31+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
32+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
33+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
34+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
35+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
36+
"@aws-cdk/aws-route53-patters:useCertificate": true,
37+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
38+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
39+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
40+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
41+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
42+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
43+
"@aws-cdk/aws-redshift:columnId": true,
44+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
45+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
46+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
47+
"@aws-cdk/aws-kms:aliasNameRef": true,
48+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
49+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
50+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
51+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
52+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
53+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
54+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
55+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
56+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
57+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
58+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
59+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
60+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true
2561
}
2662
}

cdk-sfn-s3/example-pattern.json

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
11
{
2-
"title": "Step Functions to S3 in CDK",
3-
"description": "Create a Step Functions workflow that puts the object to an S3 bucket.",
2+
"title": "AWS Step Functions to Amazon S3",
3+
"description": "Creates a Step Functions workflow that puts an object to an S3 bucket.",
44
"language": "TypeScript",
55
"level": "200",
66
"framework": "CDK",
77
"introBox": {
88
"headline": "How it works",
99
"text": [
10-
"This pattern creates a Step Functions workflow that publishes a file to an S3 bucket."
10+
"Step Functions receives an execution request using the `start-execution` API command and puts an object to an S3 bucket using the `PutObject` API call from the AWS SDK service integration."
1111
]
1212
},
1313
"gitHub": {
1414
"template": {
1515
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/cdk-sfn-s3",
1616
"templateURL": "serverless-patterns/cdk-sfn-s3",
1717
"projectFolder": "cdk-sfn-s3",
18-
"templateFile": "lib/cdk-sfn-s3-stack.ts"
18+
"templateFile": "src/lib/sfn-s3-stack.ts"
1919
}
2020
},
2121
"resources": {
2222
"bullets": [
2323
{
24-
"text": "Synchronous Express Workflows for AWS Step Functions",
25-
"link": "https://aws.amazon.com/blogs/compute/new-synchronous-express-workflows-for-aws-step-functions/"
24+
"text": "Getting started with the AWS CDK",
25+
"link": "https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html"
26+
},
27+
{
28+
"text": "Call other AWS services",
29+
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-services.html"
30+
},
31+
{
32+
"text": "AWS SDK service integrations",
33+
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/supported-services-awssdk.html"
2634
}
2735
]
2836
},
2937
"deploy": {
30-
"text": [
31-
"cdk deploy"
32-
]
38+
"text": ["cdk deploy"]
3339
},
3440
"testing": {
35-
"text": [
36-
"See the GitHub repo for detailed testing instructions."
37-
]
41+
"text": ["See the GitHub repo for detailed testing instructions."]
3842
},
3943
"cleanup": {
40-
"text": [
41-
"Delete the stack: <code>cdk delete</code>."
42-
]
44+
"text": ["Delete the stack: <code>cdk destroy</code>."]
4345
},
4446
"authors": [
4547
{
46-
"name": "Harshitha Shashidhar",
47-
"image": "../assets/images/resources/harshihs.jpg",
48-
"bio": "Harshitha Shashidhar is an Cloud Application architect who loves to build the serverless applications at scale."
48+
"name": "Gary Sassano",
49+
"image": "https://avatars.githubusercontent.com/u/10464497",
50+
"bio": "Cloud architect and DevOps enthusiast specializing in serverless solutions.",
51+
"linkedin": "garysassano"
4952
}
5053
]
5154
}

cdk-sfn-s3/jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};

cdk-sfn-s3/lib/cdk-sfn-s3-stack.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

cdk-sfn-s3/lib/sfn-s3-stack.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
Stack,
3+
StackProps,
4+
RemovalPolicy,
5+
Duration,
6+
CfnOutput,
7+
} from "aws-cdk-lib";
8+
import { Bucket } from "aws-cdk-lib/aws-s3";
9+
import { StateMachine, DefinitionBody } from "aws-cdk-lib/aws-stepfunctions";
10+
import { CallAwsService } from "aws-cdk-lib/aws-stepfunctions-tasks";
11+
import { Construct } from "constructs";
12+
13+
export class SfnS3Stack extends Stack {
14+
constructor(scope: Construct, id: string, props?: StackProps) {
15+
super(scope, id, props);
16+
17+
// Generate a unique ID for resource naming
18+
const uniqueId = this.node.addr.substring(0, 8);
19+
20+
// Create S3 Bucket
21+
const sfnDestinationBucket = new Bucket(this, "SfnDestinationBucket", {
22+
bucketName: `sfn-destination-bucket-${uniqueId}`,
23+
versioned: true,
24+
removalPolicy: RemovalPolicy.DESTROY,
25+
autoDeleteObjects: true,
26+
});
27+
28+
const sfnCallS3PutObject = new CallAwsService(this, "SfnCallS3PutObject", {
29+
service: "s3",
30+
action: "putObject",
31+
parameters: {
32+
Bucket: sfnDestinationBucket.bucketName,
33+
Key: "filename.txt",
34+
Body: "Hello World",
35+
},
36+
iamResources: [sfnDestinationBucket.arnForObjects("*")],
37+
});
38+
39+
// Create StateMachine
40+
const sfnStateMachine = new StateMachine(this, "SfnStateMachine", {
41+
stateMachineName: `sfn-state-machine-${uniqueId}`,
42+
definitionBody: DefinitionBody.fromChainable(sfnCallS3PutObject),
43+
timeout: Duration.seconds(30),
44+
});
45+
46+
new CfnOutput(this, "StateMachineARN", {
47+
value: sfnStateMachine.stateMachineArn,
48+
});
49+
}
50+
}

cdk-sfn-s3/package.json

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,21 @@
77
"scripts": {
88
"build": "tsc",
99
"watch": "tsc -w",
10-
"cdk": "cdk",
11-
"deploy": "cdk deploy",
12-
"destroy": "cdk destroy",
13-
"synth": "cdk synth"
10+
"test": "jest",
11+
"cdk": "cdk"
1412
},
1513
"devDependencies": {
16-
"@types/jest": "^26.0.10",
17-
"@types/node": "10.17.27",
18-
"aws-cdk": "2.80.0",
19-
"jest": "^26.4.2",
20-
"ts-jest": "^26.2.0",
21-
"ts-node": "^9.0.0",
22-
"typescript": "~4.7.3"
14+
"@types/jest": "^29.5.12",
15+
"@types/node": "20.14.8",
16+
"jest": "^29.7.0",
17+
"ts-jest": "^29.1.5",
18+
"aws-cdk": "2.147.0",
19+
"ts-node": "^10.9.2",
20+
"typescript": "~5.5.2"
2321
},
2422
"dependencies": {
25-
"@aws-cdk/aws-s3": "^1.159.0",
26-
"@aws-cdk/aws-stepfunctions": "^1.159.0",
27-
"@aws-cdk/aws-stepfunctions-tasks": "^1.145.0",
28-
"@aws-cdk/core": "^1.145.0",
29-
"source-map-support": "^0.5.16"
23+
"aws-cdk-lib": "2.147.0",
24+
"constructs": "^10.3.0",
25+
"source-map-support": "^0.5.21"
3026
}
3127
}

cdk-sfn-s3/test/cdk-sfn-s3.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// import * as cdk from 'aws-cdk-lib';
2+
// import { Template } from 'aws-cdk-lib/assertions';
3+
// import * as CdkSfnS3 from '../lib/cdk-sfn-s3--stack';
4+
5+
// example test. To run these tests, uncomment this file along with the
6+
// example resource in lib/cdk-sfn-s3--stack.ts
7+
test("SQS Queue Created", () => {
8+
// const app = new cdk.App();
9+
// // WHEN
10+
// const stack = new CdkSfnS3.CdkSfnS3Stack(app, 'MyTestStack');
11+
// // THEN
12+
// const template = Template.fromStack(stack);
13+
// template.hasResourceProperties('AWS::SQS::Queue', {
14+
// VisibilityTimeout: 300
15+
// });
16+
});

0 commit comments

Comments
 (0)