Skip to content

Commit b049fa8

Browse files
authored
chore(aws-cdk-lib): enable project references (#32970)
### Reason for this change Using project references in `aws-cdk-lib` improves the experience for other monorepo packages depending on `aws-cdk-lib`. A project reference to a composite package is an explicit instruction to only look at the build declaration files of the references project and not compile declarations from the .ts files again. This is opt-in from the _calling_ package, but must be allowed from the target for some reason. Practically this improves performance for the dependant package, but also means that the package do not have to share the same TS config anymore. The latter is particularly useful if a newer package wants to impose stricter rules. Previously all these packages were effectively bound to the same (low-ish) standards. The original opt-out was historically enabled in #8625 However the situation has drastically changes since then. Particularly `aws-cdk-lib` is now a single mega package, and thus much easier to handle. ### Description of this change Enables project references in `aws-cdk-lib`. This exposed that we are still using some deprecated APIs in some downstream packages. Previously we didn't notice because ts compiler of the downstream package would look at the uncompiled source, which still had the deprecated type. However as part of the jsii compilation these are then removed from the type declarations (and thus jsii bindings). With project references we are now looking at the declaration files and thus any usage of deprecated APIs causes a build failure. This PR is also fixing all of these instances. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes existing tests and build ### 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 3e4f377 commit b049fa8

File tree

11 files changed

+27
-33
lines changed

11 files changed

+27
-33
lines changed

packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,13 @@ export interface SubnetV2Attributes {
408408

409409
}
410410

411-
const subnetTypeMap = {
411+
type DeprecatedSubnetType = 'Deprecated_Isolated' | 'Deprecated_Private';
412+
const subnetTypeMap: { [key in SubnetType | DeprecatedSubnetType]: (vpc: IVpcV2, subnet: SubnetV2) => void } = {
412413
[SubnetType.PRIVATE_ISOLATED]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
413414
[SubnetType.PUBLIC]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.publicSubnets.push(subnet),
414415
[SubnetType.PRIVATE_WITH_EGRESS]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
415-
[SubnetType.ISOLATED]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
416-
[SubnetType.PRIVATE]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
416+
['Deprecated_Isolated']: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
417+
['Deprecated_Private']: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
417418
[SubnetType.PRIVATE_WITH_NAT]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
418419
};
419420

packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,18 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
636636
return subnets;
637637
}
638638

639-
private selectSubnetObjectsByType(subnetType: SubnetType) {
640-
const allSubnets = {
639+
private selectSubnetObjectsByType(subnetType: SubnetType): ISubnet[] {
640+
type DeprecatedSubnetType = 'Deprecated_Isolated' | 'Deprecated_Private';
641+
const allSubnets: { [key in SubnetType | DeprecatedSubnetType]?: ISubnet[] } = {
641642
[SubnetType.PRIVATE_ISOLATED]: this.isolatedSubnets,
642-
[SubnetType.ISOLATED]: this.isolatedSubnets,
643+
['Deprecated_Isolated']: this.isolatedSubnets,
643644
[SubnetType.PRIVATE_WITH_NAT]: this.privateSubnets,
644645
[SubnetType.PRIVATE_WITH_EGRESS]: this.privateSubnets,
645-
[SubnetType.PRIVATE]: this.privateSubnets,
646+
['Deprecated_Private']: this.privateSubnets,
646647
[SubnetType.PUBLIC]: this.publicSubnets,
647648
};
648649

649-
const subnets = allSubnets[subnetType];
650+
const subnets = allSubnets[subnetType]!;
650651

651652
// Force merge conflict here with https://github.com/aws/aws-cdk/pull/4089
652653
// see ImportedVpc
@@ -668,13 +669,13 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
668669
private reifySelectionDefaults(placement: SubnetSelection): SubnetSelection {
669670

670671
// TODO: throw error as new VpcV2 cannot support subnetName or subnetGroupName anymore
671-
if (placement.subnetName !== undefined) {
672+
if ('subnetName' in placement && placement.subnetName !== undefined) {
672673
if (placement.subnetGroupName !== undefined) {
673674
throw new Error('Please use only \'subnetGroupName\' (\'subnetName\' is deprecated and has the same behavior)');
674675
} else {
675676
Annotations.of(this).addWarningV2('@aws-cdk/aws-ec2:subnetNameDeprecated', 'Usage of \'subnetName\' in SubnetSelection is deprecated, use \'subnetGroupName\' instead');
676677
}
677-
placement = { ...placement, subnetGroupName: placement.subnetName };
678+
placement = { ...placement, subnetGroupName: placement.subnetName as string };
678679
}
679680

680681
const exclusiveSelections: Array<keyof SubnetSelection> = ['subnets', 'subnetType', 'subnetGroupName'];

packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ export class VpcV2 extends VpcV2Base {
329329
if (props.subnets) {
330330
for (const subnet of props.subnets) {
331331
if (subnet.subnetType === SubnetType.PRIVATE_WITH_EGRESS || subnet.subnetType === SubnetType.PRIVATE_WITH_NAT ||
332-
subnet.subnetType === SubnetType.PRIVATE) {
332+
subnet.subnetType as string === 'Deprecated_Private') {
333333
this.privateSubnets.push(SubnetV2.fromSubnetV2Attributes(scope, subnet.subnetName?? 'ImportedPrivateSubnet', subnet));
334334
} else if (subnet.subnetType === SubnetType.PUBLIC) {
335335
this.publicSubnets.push(SubnetV2.fromSubnetV2Attributes(scope, subnet.subnetName?? 'ImportedPublicSubnet', subnet));
336-
} else if (subnet.subnetType === SubnetType.ISOLATED || subnet.subnetType === SubnetType.PRIVATE_ISOLATED) {
336+
} else if (subnet.subnetType as string === 'Deprecated_Isolated' || subnet.subnetType === SubnetType.PRIVATE_ISOLATED) {
337337
this.isolatedSubnets.push(SubnetV2.fromSubnetV2Attributes(scope, subnet.subnetName?? 'ImportedIsolatedSubnet', subnet));
338338
}
339339
}

packages/@aws-cdk/aws-iot-actions-alpha/test/cloudwatch/integ.cloudwatch-set-alarm-state-action.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TestStack extends cdk.Stack {
1212
const metric = new cloudwatch.Metric({
1313
namespace: 'MyNamespace',
1414
metricName: 'MyMetric',
15-
dimensions: { MyDimension: 'MyDimensionValue' },
15+
dimensionsMap: { MyDimension: 'MyDimensionValue' },
1616
});
1717
const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
1818
metric: metric,

packages/@aws-cdk/aws-iot-alpha/lib/logging.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Resource, Stack, IResource } from 'aws-cdk-lib/core';
1+
import { Resource, Stack, IResource, ArnFormat } from 'aws-cdk-lib/core';
22
import { Construct } from 'constructs';
33
import * as iot from 'aws-cdk-lib/aws-iot';
44
import * as iam from 'aws-cdk-lib/aws-iam';
@@ -119,7 +119,7 @@ export class Logging extends Resource implements ILogging {
119119
Stack.of(this).formatArn({
120120
service: 'logs',
121121
resource: 'log-group',
122-
sep: ':',
122+
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
123123
resourceName: 'AWSIotLogsV2:*',
124124
}),
125125
],

packages/@aws-cdk/aws-lambda-go-alpha/test/bundling.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ test('Local bundling', () => {
191191

192192
expect(bundler.local).toBeDefined();
193193

194-
const tryBundle = bundler.local?.tryBundle('/outdir', { image: Runtime.GO_1_X.bundlingDockerImage });
194+
const tryBundle = bundler.local?.tryBundle('/outdir', { image: Runtime.GO_1_X.bundlingImage });
195195
expect(tryBundle).toBe(true);
196196

197197
expect(spawnSyncMock).toHaveBeenCalledWith(
@@ -217,7 +217,7 @@ test('Incorrect go version', () => {
217217
architecture: Architecture.X86_64,
218218
});
219219

220-
const tryBundle = bundler.local?.tryBundle('/outdir', { image: Runtime.GO_1_X.bundlingDockerImage });
220+
const tryBundle = bundler.local?.tryBundle('/outdir', { image: Runtime.GO_1_X.bundlingImage });
221221

222222
expect(tryBundle).toBe(false);
223223
});

packages/@aws-cdk/aws-lambda-python-alpha/test/function.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ jest.mock('../lib/bundling', () => {
2424
throw new Error('unexpected asset hash type');
2525
})();
2626

27-
return {
28-
isInline: false,
29-
bind: () => ({
27+
return new class extends lambda.Code {
28+
public readonly isInline: boolean = false;
29+
public bind = () => ({
3030
s3Location: {
3131
bucketName: 'mock-bucket-name',
3232
objectKey: mockObjectKey,
3333
},
34-
}),
35-
bindToResource: () => { return; },
36-
};
34+
});
35+
}();
3736
}),
3837
hasDependencies: jest.fn().mockReturnValue(false),
3938
},

packages/@aws-cdk/aws-sagemaker-alpha/lib/endpoint.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class EndpointInstanceProductionVariant implements IEndpointInstanceProductionVa
148148
return new cloudwatch.Metric({
149149
namespace,
150150
metricName,
151-
dimensions: {
151+
dimensionsMap: {
152152
EndpointName: this.endpoint.endpointName,
153153
VariantName: this.variantName,
154154
},

packages/@aws-cdk/aws-sagemaker-alpha/lib/model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ abstract class ModelBase extends cdk.Resource implements IModel {
103103
return;
104104
}
105105

106-
this.role.addToPolicy(statement);
106+
this.role.addToPrincipalPolicy(statement);
107107
}
108108
}
109109

packages/aws-cdk-lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"packageName": "awscdk"
9191
}
9292
},
93-
"projectReferences": false,
93+
"projectReferences": true,
9494
"metadata": {
9595
"jsii": {
9696
"rosetta": {

tools/@aws-cdk/pkglint/lib/rules.ts

-7
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,6 @@ export class JSIIProjectReferences extends ValidationRule {
645645
if (!isJSII(pkg)) {
646646
return;
647647
}
648-
649-
expectJSON(
650-
this.name,
651-
pkg,
652-
'jsii.projectReferences',
653-
pkg.json.name !== 'aws-cdk-lib',
654-
);
655648
}
656649
}
657650

0 commit comments

Comments
 (0)