Skip to content

Commit

Permalink
Merge branch 'main' into bump-cfnspec-main/v126.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller authored Jun 15, 2023
2 parents 07286b9 + d8f5e2d commit d7c97b0
Show file tree
Hide file tree
Showing 14 changed files with 826 additions and 949 deletions.
3 changes: 1 addition & 2 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"npmClient": "yarn",
"useWorkspaces": true,
"packages": [
"packages/aws-cdk-lib",
"packages/cdk-assets",
Expand All @@ -22,7 +21,7 @@
"tools/@aws-cdk/yarn-cling",
"scripts/@aws-cdk/script-tests"
],
"rejectCycles": "true",
"rejectCycles": true,
"version": "0.0.0",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
54 changes: 34 additions & 20 deletions nx.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
{
"extends": "@nrwl/workspace/presets/npm.json",
"workspaceLayout": { },
"tasksRunnerOptions": {
"default": {
"runner": "@nrwl/workspace/tasks-runners/default",
"options": {
"cacheableOperations": [
"build",
"test",
"lint",
"package",
"prepare"
]
}
}
},
"targetDefaults": {
"build": {
"implicitDependencies": ["aws-cdk-lib"],
"dependsOn": ["^build"],
"dependsOn": [
"^build"
],
"inputs": [
"{projectRoot}/**/lib/!(*.d|*.generated).ts",
"{projectRoot}/**/test/!(*.d).ts",
Expand All @@ -28,7 +13,7 @@
"!{workspaceRoot}/**/tsconfig.tsbuildinfo"
],
"outputs": [
"!{projectRoot}/**/*.integ.*.js.snapshot/*",
"{projectRoot}/**/*.integ.*.js.snapshot/*",
"{projectRoot}/tsconfig.json",
"{projectRoot}/**/lib/aws-custom-resource/sdk-api-metadata.json",
"{projectRoot}/**/build-info.json",
Expand All @@ -46,7 +31,25 @@
]
},
"test": {
"dependsOn": ["build"]
"dependsOn": [
"build"
]
}
},
"extends": "@nrwl/workspace/presets/npm.json",
"workspaceLayout": {},
"tasksRunnerOptions": {
"default": {
"runner": "@nx/workspace/tasks-runners/default",
"options": {
"cacheableOperations": [
"build",
"test",
"lint",
"package",
"prepare"
]
}
}
},
"affected": {
Expand All @@ -56,5 +59,16 @@
"@nrwl/js": {
"analyzeSourceFiles": false
}
},
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"namedInputs": {
"default": [
"{projectRoot}/**/*",
"sharedGlobals"
],
"sharedGlobals": [],
"production": [
"default"
]
}
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"build-all": "tsc -b"
},
"devDependencies": {
"@nrwl/cli": "^15.9.4",
"@nrwl/workspace": "^15.9.4",
"@nx/workspace": "^16.3.2",
"@types/node": "18.11.19",
"@types/prettier": "2.6.0",
"@yarnpkg/lockfile": "^1.1.0",
Expand All @@ -29,8 +28,8 @@
"jsii-pacmak": "1.82.0",
"jsii-reflect": "1.82.0",
"jsii-rosetta": "~5.0.8",
"lerna": "^6.6.1",
"nx": "^15.9.4",
"lerna": "^7.0.1",
"nx": "^16.3.2",
"patch-package": "^6.5.1",
"semver": "^6.3.0",
"standard-version": "^9.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/integ-runner/THIRD_PARTY_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH RE

----------------

** aws-sdk@2.1396.0 - https://www.npmjs.com/package/aws-sdk/v/2.1396.0 | Apache-2.0
** aws-sdk@2.1397.0 - https://www.npmjs.com/package/aws-sdk/v/2.1397.0 | Apache-2.0
AWS SDK for JavaScript
Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,10 @@ export interface ClusterAttributes {

/**
* The security groups associated with the container instances registered to the cluster.
*
* @default - no security groups
*/
readonly securityGroups: ec2.ISecurityGroup[];
readonly securityGroups?: ec2.ISecurityGroup[];

/**
* Specifies whether the cluster has EC2 instance capacity.
Expand Down
14 changes: 14 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,20 @@ describe('cluster', () => {

});

test('Security groups are optonal for imported clusters', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');

const cluster = ecs.Cluster.fromClusterAttributes(stack, 'Cluster', {
clusterName: 'cluster-name',
vpc,
});

// THEN
expect(cluster.connections.securityGroups).toEqual([]);
});

test('Metric', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
14 changes: 14 additions & 0 deletions packages/aws-cdk-lib/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,20 @@ It's possible to synthesize the project with more Resources than the allowed (or

Set the context key `@aws-cdk/core:stackResourceLimit` with the proper value, being 0 for disable the limit of resources.

### Template Indentation

The AWS CloudFormation templates generated by CDK include indentation by default.
Indentation makes the templates more readable, but also increases their size,
and CloudFormation templates cannot exceed 1MB.

It's possible to reduce the size of your templates by suppressing indentation.

To do this for all templates, set the context key `@aws-cdk/core:suppressTemplateIndentation` to `true`.

To do this for a specific stack, add a `suppressTemplateIndentation: true` property to the
stack's `StackProps` parameter. You can also set this property to `false` to override
the context key setting.

## App Context

[Context values](https://docs.aws.amazon.com/cdk/v2/guide/context.html) are key-value pairs that can be associated with an app, stack, or construct.
Expand Down
45 changes: 44 additions & 1 deletion packages/aws-cdk-lib/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const MY_STACK_CACHE = Symbol.for('@aws-cdk/core.Stack.myStack');

export const STACK_RESOURCE_LIMIT_CONTEXT = '@aws-cdk/core:stackResourceLimit';

const SUPPRESS_TEMPLATE_INDENTATION_CONTEXT = '@aws-cdk/core:suppressTemplateIndentation';
const TEMPLATE_BODY_MAXIMUM_SIZE = 1_000_000;

const VALID_STACK_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;

const MAX_RESOURCES = 500;
Expand Down Expand Up @@ -172,6 +175,18 @@ export interface StackProps {
* @default - no permissions boundary is applied
*/
readonly permissionsBoundary?: PermissionsBoundary;

/**
* Enable this flag to suppress indentation in generated
* CloudFormation templates.
*
* If not specified, the value of the `@aws-cdk/core:suppressTemplateIndentation`
* context key will be used. If that is not specified, then the
* default value `false` will be used.
*
* @default - the value of `@aws-cdk/core:suppressTemplateIndentation`, or `false` if that is not set.
*/
readonly suppressTemplateIndentation?: boolean;
}

/**
Expand Down Expand Up @@ -359,6 +374,18 @@ export class Stack extends Construct implements ITaggable {

private readonly _stackName: string;

/**
* Enable this flag to suppress indentation in generated
* CloudFormation templates.
*
* If not specified, the value of the `@aws-cdk/core:suppressTemplateIndentation`
* context key will be used. If that is not specified, then the
* default value `false` will be used.
*
* @default - the value of `@aws-cdk/core:suppressTemplateIndentation`, or `false` if that is not set.
*/
private readonly _suppressTemplateIndentation: boolean;

/**
* Creates a new stack.
*
Expand All @@ -385,6 +412,7 @@ export class Stack extends Construct implements ITaggable {
this._stackDependencies = { };
this.templateOptions = { };
this._crossRegionReferences = !!props.crossRegionReferences;
this._suppressTemplateIndentation = props.suppressTemplateIndentation ?? this.node.tryGetContext(SUPPRESS_TEMPLATE_INDENTATION_CONTEXT) ?? false;

Object.defineProperty(this, STACK_SYMBOL, { value: true });

Expand Down Expand Up @@ -1047,7 +1075,22 @@ export class Stack extends Construct implements ITaggable {
Annotations.of(this).addInfo(`Number of resources: ${numberOfResources} is approaching allowed maximum of ${this.maxResources}`);
}
}
fs.writeFileSync(outPath, JSON.stringify(template, undefined, 1));

const indent = this._suppressTemplateIndentation ? undefined : 1;
const templateData = JSON.stringify(template, undefined, indent);

if (templateData.length > (TEMPLATE_BODY_MAXIMUM_SIZE * 0.8)) {
const verb = templateData.length > TEMPLATE_BODY_MAXIMUM_SIZE ? 'exceeds' : 'is approaching';
const advice = this._suppressTemplateIndentation ?
'Split resources into multiple stacks to reduce template size' :
'Split resources into multiple stacks or set suppressTemplateIndentation to reduce template size';

const message = `Template size ${verb} limit: ${templateData.length}/${TEMPLATE_BODY_MAXIMUM_SIZE}. ${advice}.`;

Annotations.of(this).addWarning(message);
}

fs.writeFileSync(outPath, templateData);

for (const ctx of this._missingContext) {
if (lookupRoleArn != null) {
Expand Down
44 changes: 44 additions & 0 deletions packages/aws-cdk-lib/core/test/stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import { Construct, Node } from 'constructs';
import { toCloudFormation } from './util';
Expand Down Expand Up @@ -2104,6 +2105,49 @@ describe('stack', () => {
});
}).toThrowError('Region of stack environment must be a \'string\' but received \'number\'');
});

test('indent templates when suppressTemplateIndentation is not set', () => {
const app = new App();

const stack = new Stack(app, 'Stack');
new CfnResource(stack, 'MyResource', { type: 'MyResourceType' });

const assembly = app.synth();
const artifact = assembly.getStackArtifact(stack.artifactId);
const templateData = fs.readFileSync(artifact.templateFullPath, 'utf-8');

expect(templateData).toMatch(/^{\n \"Resources\": {\n \"MyResource\": {\n \"Type\": \"MyResourceType\"\n }\n }/);
});

test('do not indent templates when suppressTemplateIndentation is true', () => {
const app = new App();

const stack = new Stack(app, 'Stack', { suppressTemplateIndentation: true });
new CfnResource(stack, 'MyResource', { type: 'MyResourceType' });

const assembly = app.synth();
const artifact = assembly.getStackArtifact(stack.artifactId);
const templateData = fs.readFileSync(artifact.templateFullPath, 'utf-8');

expect(templateData).toMatch(/^{\"Resources\":{\"MyResource\":{\"Type\":\"MyResourceType\"}}/);
});

test('do not indent templates when @aws-cdk/core:suppressTemplateIndentation is true', () => {
const app = new App({
context: {
'@aws-cdk/core:suppressTemplateIndentation': true,
},
});

const stack = new Stack(app, 'Stack');
new CfnResource(stack, 'MyResource', { type: 'MyResourceType' });

const assembly = app.synth();
const artifact = assembly.getStackArtifact(stack.artifactId);
const templateData = fs.readFileSync(artifact.templateFullPath, 'utf-8');

expect(templateData).toMatch(/^{\"Resources\":{\"MyResource\":{\"Type\":\"MyResourceType\"}}/);
});
});

describe('permissions boundary', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ test('Policy sizes do not exceed the maximum size', () => {
}
}

Annotations.fromStack(pipelineStack).hasNoWarning('*', Match.anyValue());
// expect template size warning, but no other warnings
const annotations = Annotations.fromStack(pipelineStack);
annotations.hasWarning('*', Match.stringLikeRegexp('^Template size is approaching limit'));
const warnings = annotations.findWarning('*', Match.anyValue());
expect(warnings.length).toEqual(1);
});

test('CodeBuild action role has the right AssumeRolePolicyDocument', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/THIRD_PARTY_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH RE

----------------

** aws-sdk@2.1396.0 - https://www.npmjs.com/package/aws-sdk/v/2.1396.0 | Apache-2.0
** aws-sdk@2.1397.0 - https://www.npmjs.com/package/aws-sdk/v/2.1397.0 | Apache-2.0
AWS SDK for JavaScript
Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk/lib/context-providers/vpcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class VpcNetworkContextProviderPlugin implements ContextProviderPlugin {
if (type === undefined && subnet.MapPublicIpOnLaunch) { type = SubnetType.Public; }
if (type === undefined && routeTables.hasRouteToIgw(subnet.SubnetId)) { type = SubnetType.Public; }
if (type === undefined && routeTables.hasRouteToNatGateway(subnet.SubnetId)) { type = SubnetType.Private; }
if (type === undefined && routeTables.hasRouteToTransitGateway(subnet.SubnetId)) { type = SubnetType.Private; }
if (type === undefined) { type = SubnetType.Isolated; }

if (!isValidSubnetType(type)) {
Expand Down Expand Up @@ -176,6 +177,15 @@ class RouteTables {
return !!table && !!table.Routes && table.Routes.some(route => !!route.NatGatewayId && route.DestinationCidrBlock === '0.0.0.0/0');
}

/**
* Whether the given subnet has a route to a Transit Gateway
*/
public hasRouteToTransitGateway(subnetId: string | undefined): boolean {
const table = this.tableForSubnet(subnetId) || this.mainRouteTable;

return !!table && !!table.Routes && table.Routes.some(route => !!route.TransitGatewayId && route.DestinationCidrBlock === '0.0.0.0/0');
}

/**
* Whether the given subnet has a route to an IGW
*/
Expand Down
Loading

0 comments on commit d7c97b0

Please sign in to comment.