-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logs): add support for cloudwatch logs resource policy (#17015)
CloudFormation now supports [Cloudwatch logs Resource policies](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-resourcepolicy.html) This PR adds L2 support for it. And now its possible to grant access to service principals as follows. Previously this was throwing an error - see #5343 ```ts const eventsTargetLogs = new logs.LogGroup(this, 'EventsTargetLogGroup'); eventsTargetLogs.grantWrite(new iam.ServicePrincipal('events.amazonaws.com')).assertSuccess(); ``` In future, following custom resource implementation of `LogGroupResourcePolicy` could be replaced. https://github.com/aws/aws-cdk/blob/83b8df8c390a27e10bf362f49babfb24ee425506/packages/@aws-cdk/aws-elasticsearch/lib/log-group-resource-policy.ts#L25 https://github.com/aws/aws-cdk/blob/a872e672f8990fc3879413e5d797533d3916e1fd/packages/@aws-cdk/aws-events-targets/lib/log-group-resource-policy.ts#L26 https://github.com/aws/aws-cdk/blob/a872e672f8990fc3879413e5d797533d3916e1fd/packages/@aws-cdk/aws-events-targets/lib/log-group-resource-policy.ts#L26 closes #5343 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
4 changed files
with
157 additions
and
6 deletions.
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
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,47 @@ | ||
import { PolicyDocument, PolicyStatement } from '@aws-cdk/aws-iam'; | ||
import { Resource, Lazy, Names } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CfnResourcePolicy } from './logs.generated'; | ||
|
||
/** | ||
* Properties to define Cloudwatch log group resource policy | ||
*/ | ||
export interface ResourcePolicyProps { | ||
/** | ||
* Name of the log group resource policy | ||
* @default - Uses a unique id based on the construct path | ||
*/ | ||
readonly policyName?: string; | ||
|
||
/** | ||
* Initial statements to add to the resource policy | ||
* | ||
* @default - No statements | ||
*/ | ||
readonly policyStatements?: PolicyStatement[]; | ||
} | ||
|
||
/** | ||
* Creates Cloudwatch log group resource policies | ||
*/ | ||
export class ResourcePolicy extends Resource { | ||
/** | ||
* The IAM policy document for this resource policy. | ||
*/ | ||
public readonly document = new PolicyDocument(); | ||
|
||
constructor(scope: Construct, id: string, props?: ResourcePolicyProps) { | ||
super(scope, id); | ||
new CfnResourcePolicy(this, 'Resource', { | ||
policyName: Lazy.string({ | ||
produce: () => props?.policyName ?? Names.uniqueId(this), | ||
}), | ||
policyDocument: Lazy.string({ | ||
produce: () => JSON.stringify(this.document), | ||
}), | ||
}); | ||
if (props?.policyStatements) { | ||
this.document.addStatements(...props.policyStatements); | ||
} | ||
} | ||
} |
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