Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): write Metadata resource in core framework (#10306)
The Metadata resource used to be added by the CLI, which led to a bug. The better, less error-prone way to do it is to have the framework add the metadata resource to the stack template upon synthesis. The resources need to be added just-in-time (before synthesis), because if we do it in the constructor `node.setContext()` will stop working (for the `Stack` already having children). We only add the Metadata resource if we're running via the CLI. If we did not do this, all unit tests everywhere that use `toMatchTemplate()`/`toExactlyMatchTemplate()`/`toMatch()` will break. There are hundreds alone in our codebase, nevermind however many other ones are out there. The consequences of this are that we [still] will not record users who are doing in-memory synthesis. The CLI only does the work when the `runtimeInfo` field of the assembly is filled, which we just never do anymore. However, the code cannot be removed from the CLI because old versions of the framework might still set that field and expect the resource to be added to the template. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
19 changed files
with
309 additions
and
79 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/@aws-cdk/cloud-assembly-schema/schema/cloud-assembly.version.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"6.0.0"} | ||
{"version":"6.0.0"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as cxapi from '@aws-cdk/cx-api'; | ||
import { RegionInfo } from '@aws-cdk/region-info'; | ||
import { CfnCondition } from '../cfn-condition'; | ||
import { Fn } from '../cfn-fn'; | ||
import { Aws } from '../cfn-pseudo'; | ||
import { CfnResource } from '../cfn-resource'; | ||
import { Construct } from '../construct-compat'; | ||
import { Lazy } from '../lazy'; | ||
import { Stack } from '../stack'; | ||
import { Token } from '../token'; | ||
import { collectRuntimeInformation } from './runtime-info'; | ||
|
||
/** | ||
* Construct that will render the metadata resource | ||
*/ | ||
export class MetadataResource extends Construct { | ||
/** | ||
* Clear the modules cache | ||
* | ||
* The next time the MetadataResource is rendered, it will do a lookup of the | ||
* modules from the NodeJS module cache again. | ||
* | ||
* Used only for unit tests. | ||
*/ | ||
public static clearModulesCache() { | ||
this._modulesPropertyCache = undefined; | ||
} | ||
|
||
/** | ||
* Cached version of the _modulesProperty() accessor | ||
* | ||
* No point in calculating this fairly expensive list more than once. | ||
*/ | ||
private static _modulesPropertyCache?: string; | ||
|
||
/** | ||
* Calculate the modules property | ||
*/ | ||
private static modulesProperty(): string { | ||
if (this._modulesPropertyCache === undefined) { | ||
this._modulesPropertyCache = formatModules(collectRuntimeInformation()); | ||
} | ||
return this._modulesPropertyCache; | ||
} | ||
|
||
constructor(scope: Stack, id: string) { | ||
super(scope, id); | ||
|
||
const metadataServiceExists = Token.isUnresolved(scope.region) || RegionInfo.get(scope.region).cdkMetadataResourceAvailable; | ||
if (metadataServiceExists) { | ||
const resource = new CfnResource(this, 'Default', { | ||
type: 'AWS::CDK::Metadata', | ||
properties: { | ||
Modules: Lazy.stringValue({ produce: () => MetadataResource.modulesProperty() }), | ||
}, | ||
}); | ||
|
||
// In case we don't actually know the region, add a condition to determine it at deploy time | ||
if (Token.isUnresolved(scope.region)) { | ||
const condition = new CfnCondition(this, 'Condition', { | ||
expression: makeCdkMetadataAvailableCondition(), | ||
}); | ||
|
||
// To not cause undue template changes | ||
condition.overrideLogicalId('CDKMetadataAvailable'); | ||
|
||
resource.cfnOptions.condition = condition; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function makeCdkMetadataAvailableCondition() { | ||
return Fn.conditionOr(...RegionInfo.regions | ||
.filter(ri => ri.cdkMetadataResourceAvailable) | ||
.map(ri => Fn.conditionEquals(Aws.REGION, ri.name))); | ||
} | ||
|
||
function formatModules(runtime: cxapi.RuntimeInfo): string { | ||
const modules = new Array<string>(); | ||
|
||
// inject toolkit version to list of modules | ||
const cliVersion = process.env[cxapi.CLI_VERSION_ENV]; | ||
if (cliVersion) { | ||
modules.push(`aws-cdk=${cliVersion}`); | ||
} | ||
|
||
for (const key of Object.keys(runtime.libraries).sort()) { | ||
modules.push(`${key}=${runtime.libraries[key]}`); | ||
} | ||
return modules.join(','); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.