Skip to content

Commit f0383d6

Browse files
authored
fix(events_targets): installing latest aws sdk fails in cn partition (#29374)
### Issue # (if applicable) Closes #29373 ### Reason for this change AWS Log Group event target by default installs the latest aws sdk for its custom resource and this would fail in `aws-cn` partition. This PR exposes the `installLatestAwsSdk` to the surface and allows users to optionally turn off `installLatestAwsSdk` for cloudwatch log events target. ### Description of changes Allow users to override the value, if unset default to true which is the same behaviour as current. ### Description of how you validated changes all tests pass. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 61ac788 commit f0383d6

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

packages/aws-cdk-lib/aws-events-targets/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ rule.addTarget(new targets.CloudWatchLogGroup(logGroup, {
120120
}));
121121
```
122122

123+
The cloudwatch log event target will create an AWS custom resource internally which will default
124+
to set `installLatestAwsSdk` to `true`. This may be problematic for CN partition deployment. To
125+
workaround this issue, set `installLatestAwsSdk` to `false`.
126+
127+
```ts
128+
import * as logs from 'aws-cdk-lib/aws-logs';
129+
declare const logGroup: logs.LogGroup;
130+
declare const rule: events.Rule;
131+
132+
rule.addTarget(new targets.CloudWatchLogGroup(logGroup, {
133+
installLatestAwsSdk: false,
134+
}));
135+
```
136+
123137
## Start a CodeBuild build
124138

125139
Use the `CodeBuildProject` target to trigger a CodeBuild project.

packages/aws-cdk-lib/aws-events-targets/lib/log-group-resource-policy.ts

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export interface LogGroupResourcePolicyProps {
1515
* The policy statements for the log group resource logs
1616
*/
1717
readonly policyStatements: [iam.PolicyStatement];
18+
/**
19+
* Whether to install latest AWS SDK for the custom resource
20+
*
21+
* @default - install latest AWS SDK
22+
*/
23+
readonly installLatestAwsSdk?: boolean;
1824
}
1925

2026
/**
@@ -39,6 +45,7 @@ export class LogGroupResourcePolicy extends cr.AwsCustomResource {
3945
},
4046
physicalResourceId: cr.PhysicalResourceId.of(id),
4147
},
48+
installLatestAwsSdk: props.installLatestAwsSdk,
4249
onDelete: {
4350
service: 'CloudWatchLogs',
4451
action: 'deleteResourcePolicy',

packages/aws-cdk-lib/aws-events-targets/lib/log-group.ts

+9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ export interface LogGroupProps extends TargetBaseProps {
7878
* @default - the entire EventBridge event
7979
*/
8080
readonly logEvent?: LogGroupTargetInput;
81+
82+
/**
83+
* Whether the custom resource created wll default to
84+
* install latest AWS SDK
85+
*
86+
* @default - install latest AWS SDK
87+
*/
88+
readonly installLatestAwsSdk?: boolean;
8189
}
8290

8391
/**
@@ -109,6 +117,7 @@ export class CloudWatchLogGroup implements events.IRuleTarget {
109117

110118
if (!this.logGroup.node.tryFindChild(resourcePolicyId)) {
111119
new LogGroupResourcePolicy(logGroupStack, resourcePolicyId, {
120+
installLatestAwsSdk: this.props.installLatestAwsSdk,
112121
policyStatements: [new iam.PolicyStatement({
113122
effect: iam.Effect.ALLOW,
114123
actions: ['logs:PutLogEvents', 'logs:CreateLogStream'],

packages/aws-cdk-lib/aws-events-targets/test/logs/log-group.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,46 @@ test('logEvent with defaults', () => {
158158
});
159159
});
160160

161+
test('can set install latest AWS SDK value to false', () => {
162+
// GIVEN
163+
const stack = new cdk.Stack();
164+
const logGroup = new logs.LogGroup(stack, 'MyLogGroup', {
165+
logGroupName: '/aws/events/MyLogGroup',
166+
});
167+
const rule1 = new events.Rule(stack, 'Rule', {
168+
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
169+
});
170+
171+
// WHEN
172+
rule1.addTarget(new targets.CloudWatchLogGroup(logGroup, {
173+
installLatestAwsSdk: false,
174+
}));
175+
176+
// THEN
177+
Template.fromStack(stack).hasResourceProperties('Custom::CloudwatchLogResourcePolicy', {
178+
InstallLatestAwsSdk: false,
179+
});
180+
});
181+
182+
test('default install latest AWS SDK is true', () => {
183+
// GIVEN
184+
const stack = new cdk.Stack();
185+
const logGroup = new logs.LogGroup(stack, 'MyLogGroup', {
186+
logGroupName: '/aws/events/MyLogGroup',
187+
});
188+
const rule1 = new events.Rule(stack, 'Rule', {
189+
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
190+
});
191+
192+
// WHEN
193+
rule1.addTarget(new targets.CloudWatchLogGroup(logGroup));
194+
195+
// THEN
196+
Template.fromStack(stack).hasResourceProperties('Custom::CloudwatchLogResourcePolicy', {
197+
InstallLatestAwsSdk: true,
198+
});
199+
});
200+
161201
test('can use logEvent', () => {
162202
// GIVEN
163203
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)