Skip to content

Commit

Permalink
chore(aws-cdk-lib): enable project references (#32970)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
mrgrain authored Jan 17, 2025
1 parent 3e4f377 commit b049fa8
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 33 deletions.
7 changes: 4 additions & 3 deletions packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,13 @@ export interface SubnetV2Attributes {

}

const subnetTypeMap = {
type DeprecatedSubnetType = 'Deprecated_Isolated' | 'Deprecated_Private';
const subnetTypeMap: { [key in SubnetType | DeprecatedSubnetType]: (vpc: IVpcV2, subnet: SubnetV2) => void } = {
[SubnetType.PRIVATE_ISOLATED]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
[SubnetType.PUBLIC]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.publicSubnets.push(subnet),
[SubnetType.PRIVATE_WITH_EGRESS]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
[SubnetType.ISOLATED]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
[SubnetType.PRIVATE]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
['Deprecated_Isolated']: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
['Deprecated_Private']: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
[SubnetType.PRIVATE_WITH_NAT]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.privateSubnets.push(subnet),
};

Expand Down
15 changes: 8 additions & 7 deletions packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,17 +636,18 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
return subnets;
}

private selectSubnetObjectsByType(subnetType: SubnetType) {
const allSubnets = {
private selectSubnetObjectsByType(subnetType: SubnetType): ISubnet[] {
type DeprecatedSubnetType = 'Deprecated_Isolated' | 'Deprecated_Private';
const allSubnets: { [key in SubnetType | DeprecatedSubnetType]?: ISubnet[] } = {
[SubnetType.PRIVATE_ISOLATED]: this.isolatedSubnets,
[SubnetType.ISOLATED]: this.isolatedSubnets,
['Deprecated_Isolated']: this.isolatedSubnets,
[SubnetType.PRIVATE_WITH_NAT]: this.privateSubnets,
[SubnetType.PRIVATE_WITH_EGRESS]: this.privateSubnets,
[SubnetType.PRIVATE]: this.privateSubnets,
['Deprecated_Private']: this.privateSubnets,
[SubnetType.PUBLIC]: this.publicSubnets,
};

const subnets = allSubnets[subnetType];
const subnets = allSubnets[subnetType]!;

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

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

const exclusiveSelections: Array<keyof SubnetSelection> = ['subnets', 'subnetType', 'subnetGroupName'];
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,11 @@ export class VpcV2 extends VpcV2Base {
if (props.subnets) {
for (const subnet of props.subnets) {
if (subnet.subnetType === SubnetType.PRIVATE_WITH_EGRESS || subnet.subnetType === SubnetType.PRIVATE_WITH_NAT ||
subnet.subnetType === SubnetType.PRIVATE) {
subnet.subnetType as string === 'Deprecated_Private') {
this.privateSubnets.push(SubnetV2.fromSubnetV2Attributes(scope, subnet.subnetName?? 'ImportedPrivateSubnet', subnet));
} else if (subnet.subnetType === SubnetType.PUBLIC) {
this.publicSubnets.push(SubnetV2.fromSubnetV2Attributes(scope, subnet.subnetName?? 'ImportedPublicSubnet', subnet));
} else if (subnet.subnetType === SubnetType.ISOLATED || subnet.subnetType === SubnetType.PRIVATE_ISOLATED) {
} else if (subnet.subnetType as string === 'Deprecated_Isolated' || subnet.subnetType === SubnetType.PRIVATE_ISOLATED) {
this.isolatedSubnets.push(SubnetV2.fromSubnetV2Attributes(scope, subnet.subnetName?? 'ImportedIsolatedSubnet', subnet));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestStack extends cdk.Stack {
const metric = new cloudwatch.Metric({
namespace: 'MyNamespace',
metricName: 'MyMetric',
dimensions: { MyDimension: 'MyDimensionValue' },
dimensionsMap: { MyDimension: 'MyDimensionValue' },
});
const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
metric: metric,
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-iot-alpha/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Resource, Stack, IResource } from 'aws-cdk-lib/core';
import { Resource, Stack, IResource, ArnFormat } from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import * as iot from 'aws-cdk-lib/aws-iot';
import * as iam from 'aws-cdk-lib/aws-iam';
Expand Down Expand Up @@ -119,7 +119,7 @@ export class Logging extends Resource implements ILogging {
Stack.of(this).formatArn({
service: 'logs',
resource: 'log-group',
sep: ':',
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
resourceName: 'AWSIotLogsV2:*',
}),
],
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda-go-alpha/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ test('Local bundling', () => {

expect(bundler.local).toBeDefined();

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

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

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

expect(tryBundle).toBe(false);
});
Expand Down
11 changes: 5 additions & 6 deletions packages/@aws-cdk/aws-lambda-python-alpha/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ jest.mock('../lib/bundling', () => {
throw new Error('unexpected asset hash type');
})();

return {
isInline: false,
bind: () => ({
return new class extends lambda.Code {
public readonly isInline: boolean = false;
public bind = () => ({
s3Location: {
bucketName: 'mock-bucket-name',
objectKey: mockObjectKey,
},
}),
bindToResource: () => { return; },
};
});
}();
}),
hasDependencies: jest.fn().mockReturnValue(false),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-sagemaker-alpha/lib/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class EndpointInstanceProductionVariant implements IEndpointInstanceProductionVa
return new cloudwatch.Metric({
namespace,
metricName,
dimensions: {
dimensionsMap: {
EndpointName: this.endpoint.endpointName,
VariantName: this.variantName,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-sagemaker-alpha/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract class ModelBase extends cdk.Resource implements IModel {
return;
}

this.role.addToPolicy(statement);
this.role.addToPrincipalPolicy(statement);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"packageName": "awscdk"
}
},
"projectReferences": false,
"projectReferences": true,
"metadata": {
"jsii": {
"rosetta": {
Expand Down
7 changes: 0 additions & 7 deletions tools/@aws-cdk/pkglint/lib/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,6 @@ export class JSIIProjectReferences extends ValidationRule {
if (!isJSII(pkg)) {
return;
}

expectJSON(
this.name,
pkg,
'jsii.projectReferences',
pkg.json.name !== 'aws-cdk-lib',
);
}
}

Expand Down

0 comments on commit b049fa8

Please sign in to comment.