Skip to content

Commit 67c7298

Browse files
authored
JavaScript (v3): Kinesis - Replace web example with single action (#6872)
The previous example was not configured with server-side encryption, which is the recommended best practice. It was also a web example that only showcase one SDK call, which we have decided doesn't meet the bar of a good example. This replaces the web example with a a single-action nodejs example and provides updated CDK/CFN scripts.
1 parent 34d1bfc commit 67c7298

File tree

21 files changed

+541
-356
lines changed

21 files changed

+541
-356
lines changed

.doc_gen/metadata/kinesis_metadata.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ kinesis_PutRecord:
231231
- kns.abapv1.put_record
232232
services:
233233
kinesis: {PutRecord}
234+
kinesis_PutRecords:
235+
languages:
236+
JavaScript:
237+
versions:
238+
- sdk_version: 3
239+
github: javascriptv3/example_code/kinesis
240+
sdkguide:
241+
excerpts:
242+
- description:
243+
snippet_files:
244+
- javascriptv3/example_code/kinesis/actions/put-records.js
245+
services:
246+
kinesis: {PutRecords}
234247
kinesis_GetRecords:
235248
languages:
236249
Java:

javascriptv3/.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/example_code/reactnative/
2-
/example_code/medical-imaging/scenarios/health-image-sets/pixel-data-verification
2+
/example_code/medical-imaging/scenarios/health-image-sets/pixel-data-verification
3+
/example_code/kinesis/kinesis-cdk

javascriptv3/example_code/kinesis/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javas
2727

2828

2929
<!--custom.prerequisites.start-->
30+
This service folder contains AWS Cloud Development Kit (CDK) code that will set up the necessary resources required to run the
31+
examples. It also contains a `stack.yaml` with the AWS CloudFormation (CFN) template generated from the CDK.
32+
33+
Create resources with the CDK using [`cdk deploy`](https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-deploy.html) or
34+
with CFN using [`aws cloudformation create-stack`](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/create-stack.html).
3035
<!--custom.prerequisites.end-->
3136

37+
### Single actions
38+
39+
Code excerpts that show you how to call individual service functions.
40+
41+
- [PutRecords](actions/put-records.js)
42+
43+
3244
<!--custom.examples.start-->
3345
<!--custom.examples.end-->
3446

@@ -46,11 +58,22 @@ node ./actions/<fileName>
4658
```
4759

4860
**Run a scenario**
61+
4962
Most scenarios can be run with the following command:
5063
```bash
5164
node ./scenarios/<fileName>
5265
```
5366

67+
**Run with options**
68+
69+
Some actions and scenarios can be run with options from the command line:
70+
```bash
71+
node ./scenarios/<fileName> --option1 --option2
72+
```
73+
[util.parseArgs](https://nodejs.org/api/util.html#utilparseargsconfig) is used to configure
74+
these options. For the specific options available to each script, see the `parseArgs` usage
75+
for that file.
76+
5477
<!--custom.instructions.start-->
5578
<!--custom.instructions.end-->
5679

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { PutRecordsCommand, KinesisClient } from "@aws-sdk/client-kinesis";
5+
6+
/**
7+
* Put multiple records into a Kinesis stream.
8+
* @param {{ streamArn: string }} config
9+
*/
10+
export const main = async ({ streamArn }) => {
11+
const client = new KinesisClient({});
12+
try {
13+
await client.send(
14+
new PutRecordsCommand({
15+
StreamARN: streamArn,
16+
Records: [
17+
{
18+
Data: new Uint8Array(),
19+
/**
20+
* Determines which shard in the stream the data record is assigned to.
21+
* Partition keys are Unicode strings with a maximum length limit of 256
22+
* characters for each key. Amazon Kinesis Data Streams uses the partition
23+
* key as input to a hash function that maps the partition key and
24+
* associated data to a specific shard.
25+
*/
26+
PartitionKey: "TEST_KEY",
27+
},
28+
{
29+
Data: new Uint8Array(),
30+
PartitionKey: "TEST_KEY",
31+
},
32+
],
33+
}),
34+
);
35+
} catch (caught) {
36+
if (caught instanceof Error) {
37+
//
38+
} else {
39+
throw caught;
40+
}
41+
}
42+
};
43+
44+
// Call function if run directly.
45+
import { fileURLToPath } from "url";
46+
import { parseArgs } from "util";
47+
48+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
49+
const options = {
50+
streamArn: {
51+
type: "string",
52+
description: "The ARN of the stream.",
53+
},
54+
};
55+
56+
const { values } = parseArgs({ options });
57+
main(values);
58+
}
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
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Kinesis example resources
2+
3+
The CDK resources necessary to run the Kinesis example code in this service directory.
4+
5+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
6+
7+
## Useful commands
8+
9+
* `npm run build` compile typescript to js
10+
* `npm run watch` watch for changes and compile
11+
* `npx cdk deploy` deploy this stack to your default AWS account/region
12+
* `npx cdk diff` compare deployed stack with current state
13+
* `npx cdk synth` emits the synthesized CloudFormation template
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
import "source-map-support/register";
5+
import * as cdk from "aws-cdk-lib";
6+
import { KinesisCdkStack } from "../lib/kinesis-cdk-stack";
7+
8+
const app = new cdk.App();
9+
new KinesisCdkStack(app, "KinesisCdkStack", {
10+
/* If you don't specify 'env', this stack will be environment-agnostic.
11+
* Account/Region-dependent features and context lookups will not work,
12+
* but a single synthesized template can be deployed anywhere. */
13+
/* Uncomment the next line to specialize this stack for the AWS Account
14+
* and Region that are implied by the current CLI configuration. */
15+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
20+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/kinesis-cdk.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules"
16+
]
17+
},
18+
"context": {
19+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
20+
"@aws-cdk/core:checkSecretUsage": true,
21+
"@aws-cdk/core:target-partitions": [
22+
"aws",
23+
"aws-cn"
24+
],
25+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
26+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
27+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
28+
"@aws-cdk/aws-iam:minimizePolicies": true,
29+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
30+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
31+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
32+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
33+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
34+
"@aws-cdk/core:enablePartitionLiterals": true,
35+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
36+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
37+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
38+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
39+
"@aws-cdk/aws-route53-patters:useCertificate": true,
40+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
41+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
42+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
43+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
44+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
45+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
46+
"@aws-cdk/aws-redshift:columnId": true,
47+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
48+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
49+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
50+
"@aws-cdk/aws-kms:aliasNameRef": true,
51+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
52+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
53+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
54+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
55+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
56+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
57+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
58+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
59+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
60+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
61+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
62+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
63+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
64+
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
65+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
66+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
67+
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
68+
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false,
69+
"@aws-cdk/aws-s3:keepNotificationInImportedBucket": false
70+
}
71+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import * as cdk from "aws-cdk-lib";
5+
import { Construct } from "constructs";
6+
import * as kinesis from "aws-cdk-lib/aws-kinesis";
7+
8+
export class KinesisCdkStack extends cdk.Stack {
9+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
10+
super(scope, id, props);
11+
12+
const stream = new kinesis.Stream(this, "ExampleStream", {
13+
streamName: "example-stream",
14+
encryption: kinesis.StreamEncryption.KMS,
15+
});
16+
17+
new cdk.CfnOutput(this, "ExampleStreamName", {
18+
key: "ExampleStreamName",
19+
value: stream.streamName,
20+
});
21+
new cdk.CfnOutput(this, "ExampleStreamArn", {
22+
key: "ExampleStreamArn",
23+
value: stream.streamArn,
24+
});
25+
}
26+
}

0 commit comments

Comments
 (0)