Skip to content

Commit

Permalink
feat(cli): assets can now depend on stacks (#25536)
Browse files Browse the repository at this point in the history
Introduce a work graph, in which building assets, publishing assets, and deploying stacks are nodes. Each can have their own sets of dependencies which will be respected in a parallel deployment.

This change supports an upcoming change for asset publishing.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr committed May 16, 2023
1 parent d11021d commit 25d5d60
Show file tree
Hide file tree
Showing 37 changed files with 1,695 additions and 778 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cx-api/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
coverageThreshold: {
global: {
...baseConfig.coverageThreshold.global,
branches: 75,
branches: 70,
},
},
};
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/cloud-assembly-schema/lib/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export interface LoadManifestOptions {
* @default false
*/
readonly skipEnumCheck?: boolean;

/**
* Topologically sort all artifacts
*
* This parameter is only respected by the constructor of `CloudAssembly`. The
* property lives here for backwards compatibility reasons.
*
* @default true
*/
readonly topoSort?: boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@ import { CloudArtifact } from '../cloud-artifact';
import type { CloudAssembly } from '../cloud-assembly';
import { Environment, EnvironmentUtils } from '../environment';

const CLOUDFORMATION_STACK_ARTIFACT_SYM = Symbol.for('@aws-cdk/cx-api.CloudFormationStackArtifact');

export class CloudFormationStackArtifact extends CloudArtifact {
/**
* Checks if `art` is an instance of this class.
*
* Use this method instead of `instanceof` to properly detect `CloudFormationStackArtifact`
* instances, even when the construct library is symlinked.
*
* Explanation: in JavaScript, multiple copies of the `cx-api` library on
* disk are seen as independent, completely different libraries. As a
* consequence, the class `CloudFormationStackArtifact` in each copy of the `cx-api` library
* is seen as a different class, and an instance of one class will not test as
* `instanceof` the other class. `npm install` will not create installations
* like this, but users may manually symlink construct libraries together or
* use a monorepo tool: in those cases, multiple copies of the `cx-api`
* library can be accidentally installed, and `instanceof` will behave
* unpredictably. It is safest to avoid using `instanceof`, and using
* this type-testing method instead.
*/
public static isCloudFormationStackArtifact(art: any): art is CloudFormationStackArtifact {
return art && typeof art === 'object' && art[CLOUDFORMATION_STACK_ARTIFACT_SYM];
}

/**
* The file name of the template.
*/
Expand Down Expand Up @@ -183,3 +206,15 @@ export class CloudFormationStackArtifact extends CloudArtifact {
return ret;
}
}

/**
* Mark all instances of 'CloudFormationStackArtifact'
*
* Why not put this in the constructor? Because this is a class property,
* not an instance property. It applies to all instances of the class.
*/
Object.defineProperty(CloudFormationStackArtifact.prototype, CLOUDFORMATION_STACK_ARTIFACT_SYM, {
value: true,
enumerable: false,
writable: false,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@ import * as cxschema from '../../../cloud-assembly-schema';
import { CloudArtifact } from '../cloud-artifact';
import type { CloudAssembly } from '../cloud-assembly';

const NESTED_CLOUD_ASSEMBLY_SYM = Symbol.for('@aws-cdk/cx-api.NestedCloudAssemblyArtifact');

/**
* Asset manifest is a description of a set of assets which need to be built and published
*/
export class NestedCloudAssemblyArtifact extends CloudArtifact {
/**
* Checks if `art` is an instance of this class.
*
* Use this method instead of `instanceof` to properly detect `NestedCloudAssemblyArtifact`
* instances, even when the construct library is symlinked.
*
* Explanation: in JavaScript, multiple copies of the `cx-api` library on
* disk are seen as independent, completely different libraries. As a
* consequence, the class `NestedCloudAssemblyArtifact` in each copy of the `cx-api` library
* is seen as a different class, and an instance of one class will not test as
* `instanceof` the other class. `npm install` will not create installations
* like this, but users may manually symlink construct libraries together or
* use a monorepo tool: in those cases, multiple copies of the `cx-api`
* library can be accidentally installed, and `instanceof` will behave
* unpredictably. It is safest to avoid using `instanceof`, and using
* this type-testing method instead.
*/
public static isNestedCloudAssemblyArtifact(art: any): art is NestedCloudAssemblyArtifact {
return art && typeof art === 'object' && art[NESTED_CLOUD_ASSEMBLY_SYM];
}

/**
* The relative directory name of the asset manifest
*/
Expand Down Expand Up @@ -40,4 +63,16 @@ export interface NestedCloudAssemblyArtifact {
readonly nestedAssembly: CloudAssembly;

// Declared in a different file
}
}

/**
* Mark all instances of 'NestedCloudAssemblyArtifact'
*
* Why not put this in the constructor? Because this is a class property,
* not an instance property. It applies to all instances of the class.
*/
Object.defineProperty(NestedCloudAssemblyArtifact.prototype, NESTED_CLOUD_ASSEMBLY_SYM, {
value: true,
enumerable: false,
writable: false,
});
37 changes: 36 additions & 1 deletion packages/aws-cdk-lib/cx-api/lib/artifacts/tree-cloud-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ import * as cxschema from '../../../cloud-assembly-schema';
import { CloudArtifact } from '../cloud-artifact';
import { CloudAssembly } from '../cloud-assembly';

const TREE_CLOUD_ARTIFACT_SYM = Symbol.for('@aws-cdk/cx-api.TreeCloudArtifact');

export class TreeCloudArtifact extends CloudArtifact {
/**
* Checks if `art` is an instance of this class.
*
* Use this method instead of `instanceof` to properly detect `TreeCloudArtifact`
* instances, even when the construct library is symlinked.
*
* Explanation: in JavaScript, multiple copies of the `cx-api` library on
* disk are seen as independent, completely different libraries. As a
* consequence, the class `TreeCloudArtifact` in each copy of the `cx-api` library
* is seen as a different class, and an instance of one class will not test as
* `instanceof` the other class. `npm install` will not create installations
* like this, but users may manually symlink construct libraries together or
* use a monorepo tool: in those cases, multiple copies of the `cx-api`
* library can be accidentally installed, and `instanceof` will behave
* unpredictably. It is safest to avoid using `instanceof`, and using
* this type-testing method instead.
*/
public static isTreeCloudArtifact(art: any): art is TreeCloudArtifact {
return art && typeof art === 'object' && art[TREE_CLOUD_ARTIFACT_SYM];
}

public readonly file: string;

constructor(assembly: CloudAssembly, name: string, artifact: cxschema.ArtifactManifest) {
Expand All @@ -14,4 +37,16 @@ export class TreeCloudArtifact extends CloudArtifact {
}
this.file = properties.file;
}
}
}

/**
* Mark all instances of 'TreeCloudArtifact'
*
* Why not put this in the constructor? Because this is a class property,
* not an instance property. It applies to all instances of the class.
*/
Object.defineProperty(TreeCloudArtifact.prototype, TREE_CLOUD_ARTIFACT_SYM, {
value: true,
enumerable: false,
writable: false,
});
16 changes: 11 additions & 5 deletions packages/aws-cdk-lib/cx-api/lib/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { NestedCloudAssemblyArtifact } from './artifacts/nested-cloud-assembly-a
import { TreeCloudArtifact } from './artifacts/tree-cloud-artifact';
import { CloudArtifact } from './cloud-artifact';
import { topologicalSort } from './toposort';
import { LoadManifestOptions } from '../../cloud-assembly-schema';
import * as cxschema from '../../cloud-assembly-schema';

/**
Expand Down Expand Up @@ -47,12 +46,12 @@ export class CloudAssembly {
* Reads a cloud assembly from the specified directory.
* @param directory The root directory of the assembly.
*/
constructor(directory: string, loadOptions?: LoadManifestOptions) {
constructor(directory: string, loadOptions?: cxschema.LoadManifestOptions) {
this.directory = directory;

this.manifest = cxschema.Manifest.loadAssemblyManifest(path.join(directory, MANIFEST_FILE), loadOptions);
this.version = this.manifest.version;
this.artifacts = this.renderArtifacts();
this.artifacts = this.renderArtifacts(loadOptions?.topoSort ?? true);
this.runtime = this.manifest.runtime || { libraries: { } };

// force validation of deps by accessing 'depends' on all artifacts
Expand Down Expand Up @@ -219,7 +218,7 @@ export class CloudAssembly {
}
}

private renderArtifacts() {
private renderArtifacts(topoSort: boolean) {
const result = new Array<CloudArtifact>();
for (const [name, artifact] of Object.entries(this.manifest.artifacts || { })) {
const cloudartifact = CloudArtifact.fromManifest(this, name, artifact);
Expand All @@ -228,7 +227,7 @@ export class CloudAssembly {
}
}

return topologicalSort(result, x => x.id, x => x._dependencyIDs);
return topoSort ? topologicalSort(result, x => x.id, x => x._dependencyIDs) : result;
}
}

Expand Down Expand Up @@ -357,6 +356,13 @@ export class CloudAssemblyBuilder {
parentBuilder: this,
});
}

/**
* Delete the cloud assembly directory
*/
public delete() {
fs.rmSync(this.outdir, { recursive: true, force: true });
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/cx-api/test/cloud-assembly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ test('can read assembly with asset manifest', () => {
expect(assembly.artifacts).toHaveLength(2);
});

test('can toposort assembly with asset dependency', () => {
const assembly = new CloudAssembly(path.join(FIXTURES, 'asset-depends'));
expect(assembly.stacks).toHaveLength(2);
expect(assembly.artifacts).toHaveLength(3);
expect(assembly.artifacts[0].id).toEqual('StagingStack');
});

test('getStackArtifact retrieves a stack by artifact id from a nested assembly', () => {
const assembly = new CloudAssembly(path.join(FIXTURES, 'nested-assemblies'));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "0.0.0",
"artifacts": {
"MyStackName": {
"type": "aws:cloudformation:stack",
"environment": "aws://37736633/us-region-1",
"properties": {
"templateFile": "template.json"
},
"dependencies": ["AssetManifest"],
"metadata": {
}
},
"AssetManifest": {
"type": "cdk:asset-manifest",
"properties": {
"file": "asset.json"
},
"dependencies": ["StagingStack"]
},
"StagingStack": {
"type": "aws:cloudformation:stack",
"environment": "aws://1111/us-region-1",
"properties": {
"templateFile": "template.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Resources": {
"MyBucket": {
"Type": "AWS::S3::Bucket"
}
}
}
80 changes: 0 additions & 80 deletions packages/aws-cdk/THIRD_PARTY_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -1143,32 +1143,6 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


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

** eventemitter3@4.0.7 - https://www.npmjs.com/package/eventemitter3/v/4.0.7 | MIT
The MIT License (MIT)

Copyright (c) 2014 Arnout Kazemier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


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

** fast-deep-equal@3.1.3 - https://www.npmjs.com/package/fast-deep-equal/v/3.1.3 | MIT
Expand Down Expand Up @@ -2300,60 +2274,6 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


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

** p-finally@1.0.0 - https://www.npmjs.com/package/p-finally/v/1.0.0 | MIT
The MIT License (MIT)

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


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

** p-queue@6.6.2 - https://www.npmjs.com/package/p-queue/v/6.6.2 | MIT
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


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

** p-timeout@3.2.0 - https://www.npmjs.com/package/p-timeout/v/3.2.0 | MIT
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


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

** pac-proxy-agent@5.0.0 - https://www.npmjs.com/package/pac-proxy-agent/v/5.0.0 | MIT
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/api/aws-auth/sdk-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class SdkProvider {
options?: CredentialsOptions,
): Promise<SdkForEnvironment> {
const env = await this.resolveEnvironment(environment);

const baseCreds = await this.obtainBaseCredentials(env.account, mode);

// At this point, we need at least SOME credentials
Expand Down
5 changes: 4 additions & 1 deletion packages/aws-cdk/lib/api/cxapp/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ export async function execProgram(aws: SdkProvider, config: Configuration): Prom
*/
export function createAssembly(appDir: string) {
try {
return new cxapi.CloudAssembly(appDir);
return new cxapi.CloudAssembly(appDir, {
// We sort as we deploy
topoSort: false,
});
} catch (error: any) {
if (error.message.includes(cxschema.VERSION_MISMATCH)) {
// this means the CLI version is too old.
Expand Down
Loading

0 comments on commit 25d5d60

Please sign in to comment.