-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(servicecatalog): allow creating a CFN Product Version with CDK c…
…ode (#17144) Add ability to define a product version entirely within CDK as opposed to referencing templates or local assets. The service catalog `ProductStack` is similar to `NestedStacks` that do not deploy themselves but rather are referenced by the parent stacks. The resources defined in your product are added to the product stack like any other cdk app. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* Co-authored-by: Dillon Ponzo <dponzo18@gmail.com>
- Loading branch information
Showing
11 changed files
with
485 additions
and
6 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
packages/@aws-cdk/aws-servicecatalog/lib/private/product-stack-synthesizer.ts
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,34 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
|
||
/** | ||
* Deployment environment for an AWS Service Catalog product stack. | ||
* | ||
* Interoperates with the StackSynthesizer of the parent stack. | ||
*/ | ||
export class ProductStackSynthesizer extends cdk.StackSynthesizer { | ||
private stack?: cdk.Stack; | ||
|
||
public bind(stack: cdk.Stack): void { | ||
if (this.stack !== undefined) { | ||
throw new Error('A Stack Synthesizer can only be bound once, create a new instance to use with a different Stack'); | ||
} | ||
this.stack = stack; | ||
} | ||
|
||
public addFileAsset(_asset: cdk.FileAssetSource): cdk.FileAssetLocation { | ||
throw new Error('Service Catalog Product Stacks cannot use Assets'); | ||
} | ||
|
||
public addDockerImageAsset(_asset: cdk.DockerImageAssetSource): cdk.DockerImageAssetLocation { | ||
throw new Error('Service Catalog Product Stacks cannot use Assets'); | ||
} | ||
|
||
public synthesize(session: cdk.ISynthesisSession): void { | ||
if (!this.stack) { | ||
throw new Error('You must call bindStack() first'); | ||
} | ||
// Synthesize the template, but don't emit as a cloud assembly artifact. | ||
// It will be registered as an S3 asset of its parent instead. | ||
this.synthesizeStackTemplate(this.stack, session); | ||
} | ||
} |
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,77 @@ | ||
import * as crypto from 'crypto'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { ProductStackSynthesizer } from './private/product-stack-synthesizer'; | ||
|
||
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from 'constructs'; | ||
|
||
/** | ||
* A Service Catalog product stack, which is similar in form to a Cloudformation nested stack. | ||
* You can add the resources to this stack that you want to define for your service catalog product. | ||
* | ||
* This stack will not be treated as an independent deployment | ||
* artifact (won't be listed in "cdk list" or deployable through "cdk deploy"), | ||
* but rather only synthesized as a template and uploaded as an asset to S3. | ||
* | ||
*/ | ||
export class ProductStack extends cdk.Stack { | ||
public readonly templateFile: string; | ||
private _templateUrl?: string; | ||
private _parentStack: cdk.Stack; | ||
|
||
constructor(scope: Construct, id: string) { | ||
super(scope, id, { | ||
synthesizer: new ProductStackSynthesizer(), | ||
}); | ||
|
||
this._parentStack = findParentStack(scope); | ||
|
||
// this is the file name of the synthesized template file within the cloud assembly | ||
this.templateFile = `${cdk.Names.uniqueId(this)}.product.template.json`; | ||
} | ||
|
||
/** | ||
* Fetch the template URL. | ||
* | ||
* @internal | ||
*/ | ||
public _getTemplateUrl(): string { | ||
return cdk.Lazy.uncachedString({ produce: () => this._templateUrl }); | ||
} | ||
|
||
/** | ||
* Synthesize the product stack template, overrides the `super` class method. | ||
* | ||
* Defines an asset at the parent stack which represents the template of this | ||
* product stack. | ||
* | ||
* @internal | ||
*/ | ||
public _synthesizeTemplate(session: cdk.ISynthesisSession): void { | ||
const cfn = JSON.stringify(this._toCloudFormation(), undefined, 2); | ||
const templateHash = crypto.createHash('sha256').update(cfn).digest('hex'); | ||
|
||
this._templateUrl = this._parentStack.synthesizer.addFileAsset({ | ||
packaging: cdk.FileAssetPackaging.FILE, | ||
sourceHash: templateHash, | ||
fileName: this.templateFile, | ||
}).httpUrl; | ||
|
||
fs.writeFileSync(path.join(session.assembly.outdir, this.templateFile), cfn); | ||
} | ||
} | ||
|
||
/** | ||
* Validates the scope for a product stack, which must be defined within the scope of another `Stack`. | ||
*/ | ||
function findParentStack(scope: Construct): cdk.Stack { | ||
try { | ||
const parentStack = cdk.Stack.of(scope); | ||
return parentStack as cdk.Stack; | ||
} catch (e) { | ||
throw new Error('Product stacks must be defined within scope of another non-product stack'); | ||
} | ||
} |
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
6 changes: 3 additions & 3 deletions
6
packages/@aws-cdk/aws-servicecatalog/rosetta/basic-portfolio.ts-fixture
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.