-
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(aws-logs-destinations): add Firehose Log Subscription Destination #14918
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a294c8
feat(aws-logs-destinations): add Firehose Log Subscription Destination
yerzhan7 95e9ca7
Merge remote-tracking branch 'upstream/master'
yerzhan7 cbd9a16
Fix single role scope issue
yerzhan7 f34f5bf
Migrate to L2 DeliveryStream
yerzhan7 9b3d542
Revert whitespace changes in rules.ts
yerzhan7 6d52cb2
Fix integration test and address some comments
yerzhan7 0f33993
Change role scope and remove unit test case
yerzhan7 e59b712
Remove `tryFindChild` and always create role
yerzhan7 6baf06f
Merge remote-tracking branch 'upstream/master'
yerzhan7 7aa9ac4
Merge remote-tracking branch 'upstream/master'
yerzhan7 d706e74
Merge remote-tracking branch 'upstream/master'
yerzhan7 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,3 @@ | ||
export * from './lambda'; | ||
export * from './kinesis'; | ||
export * from './kinesisfirehose'; |
44 changes: 44 additions & 0 deletions
44
packages/@aws-cdk/aws-logs-destinations/lib/kinesisfirehose.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,44 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as firehose from '@aws-cdk/aws-kinesisfirehose'; | ||
import * as logs from '@aws-cdk/aws-logs'; | ||
import { Stack } from '@aws-cdk/core'; | ||
|
||
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from '@aws-cdk/core'; | ||
|
||
/** | ||
* Use a Kinesis Firehose as the destination for a log subscription | ||
*/ | ||
export class KinesisFirehoseDestination implements logs.ILogSubscriptionDestination { | ||
constructor(private readonly deliveryStream: firehose.CfnDeliveryStream) { | ||
} | ||
|
||
public bind(scope: Construct, _sourceLogGroup: logs.ILogGroup): logs.LogSubscriptionDestinationConfig { | ||
const { region } = Stack.of(scope); | ||
|
||
// Following example from https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html#FirehoseExample | ||
// Create a role to be assumed by CWL that can write to this Firehose. | ||
const roleId = 'CloudWatchLogsCanPutRecordsIntoKinesisFirehose'; | ||
const role = scope.node.tryFindChild(roleId) as iam.IRole ?? new iam.Role(scope, roleId, { | ||
assumedBy: new iam.ServicePrincipal(`logs.${region}.amazonaws.com`), | ||
}); | ||
|
||
iam.Grant.addToPrincipal({ | ||
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. Shouldn't be necessary when the L2 is created, but I'd prefer
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. Will wait for L2 |
||
scope, | ||
grantee: role, | ||
actions: [ | ||
'firehose:PutRecord', | ||
'firehose:PutRecordBatch', | ||
], | ||
resourceArns: [ | ||
this.deliveryStream.attrArn, | ||
], | ||
}); | ||
|
||
return { | ||
arn: this.deliveryStream.attrArn, | ||
role, | ||
}; | ||
} | ||
} |
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
174 changes: 174 additions & 0 deletions
174
packages/@aws-cdk/aws-logs-destinations/test/integ.kinesisfirehose-to-s3.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,174 @@ | ||
{ | ||
"Resources": { | ||
"Bucket83908E77": { | ||
"Type": "AWS::S3::Bucket", | ||
"UpdateReplacePolicy": "Delete", | ||
"DeletionPolicy": "Delete" | ||
}, | ||
"FirehoseRoleAA67C190": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "firehose.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"Policies": [ | ||
{ | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"s3:AbortMultipartUpload", | ||
"s3:GetBucketLocation", | ||
"s3:GetObject", | ||
"s3:ListBucket", | ||
"s3:ListBucketMultipartUploads", | ||
"s3:PutObject" | ||
], | ||
"Effect": "Allow", | ||
"Resource": [ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
"/*" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "allowToDeliverToS3Bucket" | ||
} | ||
] | ||
} | ||
}, | ||
"MyFirehose": { | ||
"Type": "AWS::KinesisFirehose::DeliveryStream", | ||
"Properties": { | ||
"ExtendedS3DestinationConfiguration": { | ||
"BucketARN": { | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
"RoleARN": { | ||
"Fn::GetAtt": [ | ||
"FirehoseRoleAA67C190", | ||
"Arn" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"LogGroupF5B46931": { | ||
"Type": "AWS::Logs::LogGroup", | ||
"Properties": { | ||
"RetentionInDays": 731 | ||
}, | ||
"UpdateReplacePolicy": "Delete", | ||
"DeletionPolicy": "Delete" | ||
}, | ||
"SubscriptionCloudWatchLogsCanPutRecordsIntoKinesisFirehoseC45D3D89": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"logs.", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
".amazonaws.com" | ||
] | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"SubscriptionCloudWatchLogsCanPutRecordsIntoKinesisFirehoseDefaultPolicy8571150D": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"firehose:PutRecord", | ||
"firehose:PutRecordBatch" | ||
], | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"MyFirehose", | ||
"Arn" | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "SubscriptionCloudWatchLogsCanPutRecordsIntoKinesisFirehoseDefaultPolicy8571150D", | ||
"Roles": [ | ||
{ | ||
"Ref": "SubscriptionCloudWatchLogsCanPutRecordsIntoKinesisFirehoseC45D3D89" | ||
} | ||
] | ||
} | ||
}, | ||
"Subscription391C9821": { | ||
"Type": "AWS::Logs::SubscriptionFilter", | ||
"Properties": { | ||
"DestinationArn": { | ||
"Fn::GetAtt": [ | ||
"MyFirehose", | ||
"Arn" | ||
] | ||
}, | ||
"FilterPattern": "", | ||
"LogGroupName": { | ||
"Ref": "LogGroupF5B46931" | ||
}, | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"SubscriptionCloudWatchLogsCanPutRecordsIntoKinesisFirehoseC45D3D89", | ||
"Arn" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/@aws-cdk/aws-logs-destinations/test/integ.kinesisfirehose-to-s3.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,70 @@ | ||
#!/usr/bin/env node | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as firehose from '@aws-cdk/aws-kinesisfirehose'; | ||
import * as logs from '@aws-cdk/aws-logs'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import * as dests from '../lib'; | ||
|
||
BenChaimberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* | ||
* Stack verification steps: | ||
* 1. `aws logs create-log-stream --log-group-name <log-group-name> --log-stream-name MyIntegTestLogStream` | ||
* 2. `aws logs put-log-events --log-group-name <log-group-name> --log-stream-name MyIntegTestLogStream --log-events timestamp=$(date +%s000),message=hello` | ||
* 3. Wait a few minutes for Kinesis to deliver log to S3 | ||
* 4. `aws s3 ls s3://<bucket-name> --recursive` should list one object | ||
*/ | ||
|
||
class TestStack extends cdk.Stack { | ||
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
// Create Firehose Stream that will deliver to S3 bucket | ||
const bucket = new s3.Bucket(this, 'Bucket', { | ||
removalPolicy: cdk.RemovalPolicy.DESTROY, | ||
}); | ||
|
||
// Following example from https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html#FirehoseExample | ||
const firehoseRole = new iam.Role(this, 'FirehoseRole', { | ||
assumedBy: new iam.ServicePrincipal('firehose.amazonaws.com'), | ||
inlinePolicies: { | ||
allowToDeliverToS3Bucket: new iam.PolicyDocument({ | ||
statements: [new iam.PolicyStatement({ | ||
actions: [ | ||
's3:AbortMultipartUpload', | ||
's3:GetBucketLocation', | ||
's3:GetObject', | ||
's3:ListBucket', | ||
's3:ListBucketMultipartUploads', | ||
's3:PutObject', | ||
], | ||
resources: [ | ||
bucket.bucketArn, | ||
`${bucket.bucketArn}/*`, | ||
], | ||
})], | ||
}), | ||
}, | ||
}); | ||
|
||
const deliveryStream = new firehose.CfnDeliveryStream(this, 'MyFirehose', { | ||
extendedS3DestinationConfiguration: { | ||
bucketArn: bucket.bucketArn, | ||
roleArn: firehoseRole.roleArn, | ||
}, | ||
}); | ||
const logGroup = new logs.LogGroup(this, 'LogGroup', { | ||
removalPolicy: cdk.RemovalPolicy.DESTROY, | ||
}); | ||
|
||
new logs.SubscriptionFilter(this, 'Subscription', { | ||
logGroup, | ||
destination: new dests.KinesisFirehoseDestination(deliveryStream), | ||
filterPattern: logs.FilterPattern.allEvents(), | ||
}); | ||
} | ||
} | ||
|
||
const app = new cdk.App(); | ||
new TestStack(app, 'cdk-logs-destinations-firehose-to-s3'); | ||
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.
If you're feeling up for it, it would be great to have these snippets compile in their first iteration. I'm thinking an initial snippet showing how to set up a log group, then each individual destination uses a fixture seeded with a log group to create the destination. Though ellipses are supported, giving a full specification of the destination resource would be ideal. See: CONTRIBUTING#documentation
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.
Will wait for L2