-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(iot): add Action to put objects in S3 Buckets #17307
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
be3be36
feat(iot-actions): Add the action to put s3 bucket objects
yamatatsu b81a310
fix(iot-actions): suppress an error in CodeBuild CI
yamatatsu 4b66370
fix(iot-actions): address comments
yamatatsu 0a37bdd
fix(iot-actions): rename files
yamatatsu 9023c01
Merge branch 'master' into aws-iot-actions-s3
yamatatsu 6fc3248
refactor(iot-actions): superinterface for action props
yamatatsu a870670
Merge branch 'aws-iot-actions-s3' of https://github.com/yamatatsu/aws…
yamatatsu f0a3480
fix(iot-actions): fix jsii lint error
yamatatsu f4c292c
address comments
yamatatsu d524a80
Merge branch 'master' into aws-iot-actions-s3
mergify[bot] 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 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
This file contains 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 |
---|---|---|
@@ -1,2 +1,32 @@ | ||
AWS Cloud Development Kit (AWS CDK) | ||
Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
------------------------------------------------------------------------------- | ||
|
||
The AWS CDK includes the following third-party software/licensing: | ||
|
||
** case - https://www.npmjs.com/package/case | ||
Copyright (c) 2013 Nathan Bubna | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
---------------- |
This file contains 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
9 changes: 2 additions & 7 deletions
9
packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-logs-action.ts
This file contains 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
13 changes: 13 additions & 0 deletions
13
packages/@aws-cdk/aws-iot-actions/lib/common-action-props.ts
This file contains 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,13 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
|
||
/** | ||
* Common properties shared by Actions it access to AWS service. | ||
*/ | ||
export interface CommonActionProps { | ||
/** | ||
* The IAM role that allows access to AWS service. | ||
* | ||
* @default a new role will be created | ||
*/ | ||
readonly role?: iam.IRole; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './cloudwatch-logs-action'; | ||
export * from './common-action-props'; | ||
export * from './lambda-function-action'; | ||
export * from './s3-put-object-action'; |
67 changes: 67 additions & 0 deletions
67
packages/@aws-cdk/aws-iot-actions/lib/s3-put-object-action.ts
This file contains 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,67 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as iot from '@aws-cdk/aws-iot'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
import { kebab as toKebabCase } from 'case'; | ||
import { CommonActionProps } from './common-action-props'; | ||
import { singletonActionRole } from './private/role'; | ||
|
||
/** | ||
* Configuration properties of an action for s3. | ||
*/ | ||
export interface S3PutObjectActionProps extends CommonActionProps { | ||
/** | ||
* The Amazon S3 canned ACL that controls access to the object identified by the object key. | ||
* @see https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl | ||
* | ||
* @default None | ||
*/ | ||
readonly accessControl?: s3.BucketAccessControl; | ||
|
||
/** | ||
* The path to the file where the data is written. | ||
* | ||
* Supports substitution templates. | ||
* @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html | ||
* | ||
* @default '${topic()}/${timestamp()}' | ||
*/ | ||
readonly key?: string; | ||
} | ||
|
||
/** | ||
* The action to write the data from an MQTT message to an Amazon S3 bucket. | ||
*/ | ||
export class S3PutObjectAction implements iot.IAction { | ||
private readonly accessControl?: string; | ||
private readonly key?: string; | ||
private readonly role?: iam.IRole; | ||
|
||
/** | ||
* @param bucket The Amazon S3 bucket to which to write data. | ||
* @param props Optional properties to not use default | ||
*/ | ||
constructor(private readonly bucket: s3.IBucket, props: S3PutObjectActionProps = {}) { | ||
this.accessControl = props.accessControl; | ||
this.key = props.key; | ||
this.role = props.role; | ||
} | ||
|
||
bind(rule: iot.ITopicRule): iot.ActionConfig { | ||
const role = this.role ?? singletonActionRole(rule); | ||
role.addToPrincipalPolicy(new iam.PolicyStatement({ | ||
actions: ['s3:PutObject'], | ||
resources: [this.bucket.arnForObjects('*')], | ||
})); | ||
|
||
return { | ||
configuration: { | ||
s3: { | ||
bucketName: this.bucket.bucketName, | ||
cannedAcl: this.accessControl && toKebabCase(this.accessControl.toString()), | ||
key: this.key ?? '${topic()}/${timestamp()}', | ||
roleArn: role.roleArn, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
This file contains 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
86 changes: 86 additions & 0 deletions
86
...ages/@aws-cdk/aws-iot-actions/test/s3-put-object/integ.s3-put-object-action.expected.json
This file contains 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,86 @@ | ||
{ | ||
"Resources": { | ||
"TopicRule40A4EA44": { | ||
"Type": "AWS::IoT::TopicRule", | ||
"Properties": { | ||
"TopicRulePayload": { | ||
"Actions": [ | ||
{ | ||
"S3": { | ||
"BucketName": { | ||
"Ref": "MyBucketF68F3FF0" | ||
}, | ||
"CannedAcl": "bucket-owner-full-control", | ||
"Key": "${year}/${month}/${day}/${topic(2)}", | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"TopicRuleTopicRuleActionRole246C4F77", | ||
"Arn" | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"AwsIotSqlVersion": "2016-03-23", | ||
"Sql": "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'" | ||
} | ||
} | ||
}, | ||
"TopicRuleTopicRuleActionRole246C4F77": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "iot.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"TopicRuleTopicRuleActionRoleDefaultPolicy99ADD687": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "s3:PutObject", | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"MyBucketF68F3FF0", | ||
"Arn" | ||
] | ||
}, | ||
"/*" | ||
] | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "TopicRuleTopicRuleActionRoleDefaultPolicy99ADD687", | ||
"Roles": [ | ||
{ | ||
"Ref": "TopicRuleTopicRuleActionRole246C4F77" | ||
} | ||
] | ||
} | ||
}, | ||
"MyBucketF68F3FF0": { | ||
"Type": "AWS::S3::Bucket", | ||
"UpdateReplacePolicy": "Delete", | ||
"DeletionPolicy": "Delete" | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/@aws-cdk/aws-iot-actions/test/s3-put-object/integ.s3-put-object-action.ts
This file contains 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,32 @@ | ||
/// !cdk-integ pragma:ignore-assets | ||
import * as iot from '@aws-cdk/aws-iot'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as actions from '../../lib'; | ||
|
||
const app = new cdk.App(); | ||
|
||
class TestStack extends cdk.Stack { | ||
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
const topicRule = new iot.TopicRule(this, 'TopicRule', { | ||
sql: iot.IotSql.fromStringAsVer20160323( | ||
"SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'", | ||
), | ||
}); | ||
|
||
const bucket = new s3.Bucket(this, 'MyBucket', { | ||
removalPolicy: cdk.RemovalPolicy.DESTROY, | ||
}); | ||
topicRule.addAction( | ||
new actions.S3PutObjectAction(bucket, { | ||
key: '${year}/${month}/${day}/${topic(2)}', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
accessControl: s3.BucketAccessControl.BUCKET_OWNER_FULL_CONTROL, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
new TestStack(app, 'test-stack'); | ||
app.synth(); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show
cannedAcl
too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, where is it? 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I see below. In the future, I would merge these examples, because otherwise the ReadMe becomes way too long.