|
| 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 | +} |
0 commit comments