Skip to content

Commit ea024fc

Browse files
authored
feat(aws-iot-s3): new construct implementation (#469)
* aws-iot-s3 construt implementation * fix broken test * fix cfn_nag issues * update KMS Key test * fix pr comments * update test cases & address pr review comments * fix tests for cdk v2 * clean access logging buckets in integ tests * Used IBucket instead of Bucket as existing bucket prop * address pr review comments Co-authored-by: santhosh <>
1 parent b8a3d12 commit ea024fc

14 files changed

+1617
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib/*.js
2+
test/*.js
3+
*.d.ts
4+
coverage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
lib/*.js
2+
test/*.js
3+
*.js.map
4+
*.d.ts
5+
node_modules
6+
*.generated.ts
7+
dist
8+
.jsii
9+
10+
.LAST_BUILD
11+
.nyc_output
12+
coverage
13+
.nycrc
14+
.LAST_PACKAGE
15+
*.snk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Exclude typescript source and config
2+
*.ts
3+
tsconfig.json
4+
coverage
5+
.nyc_output
6+
*.tgz
7+
*.snk
8+
*.tsbuildinfo
9+
10+
# Include javascript files and typescript declarations
11+
!*.js
12+
!*.d.ts
13+
14+
# Exclude jsii outdir
15+
dist
16+
17+
# Include .jsii
18+
!.jsii
19+
20+
# Include .jsii
21+
!.jsii
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# aws-iot-s3 module
2+
<!--BEGIN STABILITY BANNER-->
3+
4+
---
5+
6+
![Stability: Experimental](https://img.shields.io/badge/stability-Experimental-important.svg?style=for-the-badge)
7+
8+
> All classes are under active development and subject to non-backward compatible changes or removal in any
9+
> future version. These are not subject to the [Semantic Versioning](https://semver.org/) model.
10+
> This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
11+
12+
---
13+
<!--END STABILITY BANNER-->
14+
15+
| **Reference Documentation**:| <span style="font-weight: normal">https://docs.aws.amazon.com/solutions/latest/constructs/</span>|
16+
|:-------------|:-------------|
17+
<div style="height:8px"></div>
18+
19+
| **Language** | **Package** |
20+
|:-------------|-----------------|
21+
|![Python Logo](https://docs.aws.amazon.com/cdk/api/latest/img/python32.png) Python|`aws_solutions_constructs.aws_iot_s3`|
22+
|![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) Typescript|`@aws-solutions-constructs/aws-iot-s3`|
23+
|![Java Logo](https://docs.aws.amazon.com/cdk/api/latest/img/java32.png) Java|`software.amazon.awsconstructs.services.iots3`|
24+
25+
This AWS Solutions Construct implements an AWS IoT MQTT topic rule and an Amazon S3 Bucket pattern.
26+
27+
Here is a minimal deployable pattern definition in Typescript:
28+
29+
``` typescript
30+
const { IotToS3Props, IotToS3 } from '@aws-solutions-constructs/aws-iot-s3';
31+
32+
const props: IotToS3Props = {
33+
iotTopicRuleProps: {
34+
topicRulePayload: {
35+
ruleDisabled: false,
36+
description: "Testing the IotToS3 Pattern",
37+
sql: "SELECT * FROM 'solutions/constructs'",
38+
actions: []
39+
}
40+
}
41+
};
42+
43+
new IotToS3(this, 'test-iot-s3-integration', props);
44+
```
45+
46+
## Initializer
47+
48+
``` text
49+
new IotToS3(scope: Construct, id: string, props: IotToS3Props);
50+
```
51+
52+
_Parameters_
53+
54+
* scope [`Construct`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html)
55+
* id `string`
56+
* props [`IotToS3Props`](#pattern-construct-props)
57+
58+
## Pattern Construct Props
59+
60+
| **Name** | **Type** | **Description** |
61+
|:-------------|:----------------|-----------------|
62+
|existingBucketInterface?|[`s3.IBucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.IBucket.html)|Existing S3 Bucket interface. Providing this property and `bucketProps` results in an error.|
63+
|bucketProps?|[`s3.BucketProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.BucketProps.html)|Optional user provided props to override the default props for the S3 Bucket. Providing this and `existingBucketObj` reults in an error.|
64+
|loggingBucketProps?|[`s3.BucketProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.BucketProps.html)|Optional user provided props to override the default props for the S3 Logging Bucket.|
65+
|iotTopicRuleProps?|[`iot.CfnTopicRuleProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iot.CfnTopicRuleProps.html)|User provided CfnTopicRuleProps to override the defaults.|
66+
|s3Key|`string`|User provided s3Key to override the default (`${topic()}/${timestamp()}`) object key. Used to store messages matched by the IoT Rule.|
67+
|logS3AccessLogs?|`boolean`|Whether to turn on Access Logging for the S3 bucket. Creates an S3 bucket with associated storage costs for the logs. Enabling Access Logging is a best practice. default - true|
68+
69+
## Pattern Properties
70+
71+
| **Name** | **Type** | **Description** |
72+
|:-------------|:----------------|-----------------|
73+
|s3Bucket?|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)|Returns an instance of the S3 bucket created by the pattern. If an existingBucketInterface is provided in IotToS3Props, then this value will be undefined|
74+
|s3BucketInterface?|[`s3.IBucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.IBucket.html)|Returns S3 Bucket interface created or used by the pattern. If an existingBucketInterface is provided in IotToS3Props, then only this value will be set and s3Bucket will be undefined. If the construct creates the bucket, then both properties will be set.|
75+
|s3LoggingBucket?|[`s3.Bucket`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html)|Returns an instance of `s3.Bucket` created by the construct as the logging bucket for the primary bucket.|
76+
|iotActionsRole|[`iam.Role`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iam.Role.html)|Returns an instance of `iam.Role` created by the construct, which allows IoT to publish messages to the S3 bucket.|
77+
|iotTopicRule|[`iot.CfnTopicRule`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-iot.CfnTopicRule.html)|Returns an instance of `iot.CfnTopicRule` created by the construct|
78+
79+
## Default settings
80+
81+
Out of the box implementation of the Construct without any override will set the following defaults:
82+
83+
### Amazon IoT Rule
84+
85+
* Configure an IoT Rule to send messages to the S3 Bucket
86+
87+
### Amazon IAM Role
88+
89+
* Configure least privilege access IAM role for Amazon IoT to be able to publish messages to the S3 Bucket
90+
91+
### Amazon S3 Bucket
92+
93+
* Configure Access logging for S3 Bucket
94+
* Enable server-side encryption for S3 Bucket using AWS managed KMS Key
95+
* Enforce encryption of data in transit
96+
* Turn on the versioning for S3 Bucket
97+
* Don't allow public access for S3 Bucket
98+
* Retain the S3 Bucket when deleting the CloudFormation stack
99+
* Applies Lifecycle rule to move noncurrent object versions to Glacier storage after 90 days
100+
101+
## Architecture
102+
103+
![Architecture Diagram](architecture.png)
104+
105+
---
106+
&copy; Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
14+
import * as s3 from '@aws-cdk/aws-s3';
15+
import * as iot from '@aws-cdk/aws-iot';
16+
import * as iam from '@aws-cdk/aws-iam';
17+
import * as defaults from '@aws-solutions-constructs/core';
18+
// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate
19+
import { Construct } from '@aws-cdk/core';
20+
21+
/**
22+
* @summary The properties for the IotToS3 class.
23+
*/
24+
export interface IotToS3Props {
25+
/**
26+
* Existing S3 Bucket interface, providing both this and `bucketProps` will cause an error.
27+
*
28+
* @default - None
29+
*/
30+
readonly existingBucketInterface?: s3.IBucket;
31+
/**
32+
* User provided props to override the default props for the S3 Bucket.
33+
*
34+
* @default - Default props are used.
35+
*/
36+
readonly bucketProps?: s3.BucketProps;
37+
/**
38+
* User provided CfnTopicRuleProps to override the defaults
39+
*
40+
* @default - Default props are used. S3ActionProperty with S3 Key '${topic()}/${timestamp()}' is used.
41+
*/
42+
readonly iotTopicRuleProps: iot.CfnTopicRuleProps;
43+
/**
44+
* Optional user provided props to override the default props for the S3 Logging Bucket.
45+
*
46+
* @default - Default props are used
47+
*/
48+
readonly loggingBucketProps?: s3.BucketProps
49+
/**
50+
* Optional user provided value to override the default S3Key for IoTRule S3 Action.
51+
*
52+
* @default - Default value '${topic()}/${timestamp()}' is used
53+
*/
54+
readonly s3Key?: string;
55+
/**
56+
* Whether to turn on Access Logs for the S3 bucket with the associated storage costs.
57+
* Enabling Access Logging is a best practice.
58+
*
59+
* @default - true
60+
*/
61+
readonly logS3AccessLogs?: boolean;
62+
}
63+
64+
export class IotToS3 extends Construct {
65+
public readonly s3Bucket?: s3.Bucket;
66+
public readonly s3BucketInterface: s3.IBucket;
67+
public readonly s3LoggingBucket?: s3.Bucket;
68+
public readonly iotActionsRole: iam.Role;
69+
public readonly iotTopicRule: iot.CfnTopicRule;
70+
71+
/**
72+
* @summary Constructs a new instance of the IotToSqs class.
73+
* @param {cdk.App} scope - represents the scope for all the resources.
74+
* @param {string} id - this is a a scope-unique id.
75+
* @param {IotToS3Props} props - user provided props for the construct
76+
* @access public
77+
*/
78+
constructor(scope: Construct, id: string, props: IotToS3Props) {
79+
super(scope, id);
80+
defaults.CheckProps(props);
81+
82+
// Setup S3 Bucket
83+
if (!props.existingBucketInterface) {
84+
[this.s3Bucket, this.s3LoggingBucket] = defaults.buildS3Bucket(this, {
85+
bucketProps: props.bucketProps,
86+
loggingBucketProps: props.loggingBucketProps,
87+
logS3AccessLogs: props.logS3AccessLogs
88+
});
89+
this.s3BucketInterface = this.s3Bucket;
90+
} else {
91+
this.s3BucketInterface = props.existingBucketInterface;
92+
}
93+
94+
// Role to allow IoT to send messages to the S3 Bucket
95+
this.iotActionsRole = new iam.Role(this, 'iot-actions-role', {
96+
assumedBy: new iam.ServicePrincipal('iot.amazonaws.com')
97+
});
98+
99+
// Setup the IAM policy for IoT Actions
100+
this.s3BucketInterface.grantWrite(this.iotActionsRole);
101+
102+
const defaultIotTopicProps = defaults.DefaultCfnTopicRuleProps([{
103+
s3: {
104+
key: props.s3Key || '${topic()}/${timestamp()}',
105+
bucketName: this.s3BucketInterface.bucketName,
106+
roleArn: this.iotActionsRole.roleArn
107+
}
108+
}]);
109+
const iotTopicProps = defaults.overrideProps(defaultIotTopicProps, props.iotTopicRuleProps, true);
110+
111+
// Create the IoT topic rule
112+
this.iotTopicRule = new iot.CfnTopicRule(this, 'IotTopicRule', iotTopicProps);
113+
114+
// If existing bucket has a KMS CMK, explicitly provide IoTActionsRole necessary access to write to the bucket
115+
if (this.s3Bucket && this.s3Bucket.encryptionKey) {
116+
this.s3Bucket.encryptionKey.grantEncrypt(this.iotActionsRole);
117+
}
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"name": "@aws-solutions-constructs/aws-iot-s3",
3+
"version": "0.0.0",
4+
"description": "CDK Constructs for AWS IoT to AWS S3 integration",
5+
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/awslabs/aws-solutions-constructs.git",
10+
"directory": "source/patterns/@aws-solutions-constructs/aws-iot-s3"
11+
},
12+
"author": {
13+
"name": "Amazon Web Services",
14+
"url": "https://aws.amazon.com",
15+
"organization": true
16+
},
17+
"license": "Apache-2.0",
18+
"scripts": {
19+
"build": "tsc -b .",
20+
"lint": "eslint -c ../eslintrc.yml --ext=.js,.ts . && tslint --project .",
21+
"lint-fix": "eslint -c ../eslintrc.yml --ext=.js,.ts --fix .",
22+
"test": "jest --coverage",
23+
"clean": "tsc -b --clean",
24+
"watch": "tsc -b -w",
25+
"integ": "cdk-integ",
26+
"integ-no-clean": "cdk-integ --no-clean",
27+
"integ-assert": "cdk-integ-assert",
28+
"jsii": "jsii",
29+
"jsii-pacmak": "jsii-pacmak",
30+
"build+lint+test": "npm run jsii && npm run lint && npm test && npm run integ-assert",
31+
"snapshot-update": "npm run jsii && npm test -- -u && npm run integ-assert"
32+
},
33+
"jsii": {
34+
"outdir": "dist",
35+
"targets": {
36+
"java": {
37+
"package": "software.amazon.awsconstructs.services.iots3",
38+
"maven": {
39+
"groupId": "software.amazon.awsconstructs",
40+
"artifactId": "iots3"
41+
}
42+
},
43+
"dotnet": {
44+
"namespace": "Amazon.Constructs.AWS.IotS3",
45+
"packageId": "Amazon.Constructs.AWS.IotS3",
46+
"signAssembly": true,
47+
"iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png"
48+
},
49+
"python": {
50+
"distName": "aws-solutions-constructs.aws-iot-s3",
51+
"module": "aws_solutions_constructs.aws_iot_s3"
52+
}
53+
}
54+
},
55+
"dependencies": {
56+
"@aws-cdk/aws-iot": "0.0.0",
57+
"@aws-cdk/aws-s3": "0.0.0",
58+
"@aws-cdk/aws-iam": "0.0.0",
59+
"@aws-cdk/aws-kms": "0.0.0",
60+
"@aws-cdk/core": "0.0.0",
61+
"@aws-solutions-constructs/core": "0.0.0",
62+
"constructs": "^3.2.0"
63+
},
64+
"devDependencies": {
65+
"@aws-cdk/assert": "0.0.0",
66+
"@types/jest": "^26.0.22",
67+
"@types/node": "^10.3.0"
68+
},
69+
"jest": {
70+
"moduleFileExtensions": [
71+
"js"
72+
],
73+
"coverageReporters": [
74+
"text",
75+
[
76+
"lcov",
77+
{
78+
"projectRoot": "../../../../"
79+
}
80+
]
81+
]
82+
},
83+
"peerDependencies": {
84+
"@aws-cdk/aws-iot": "0.0.0",
85+
"@aws-cdk/aws-s3": "0.0.0",
86+
"@aws-cdk/aws-iam": "0.0.0",
87+
"@aws-cdk/aws-kms": "0.0.0",
88+
"@aws-cdk/core": "0.0.0",
89+
"@aws-solutions-constructs/core": "0.0.0",
90+
"constructs": "^3.2.0"
91+
}
92+
}

0 commit comments

Comments
 (0)