-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(servicecatalog): allow creating a CFN Product Version with CDK code #17144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8db1d9c
feat(servicecatalog): Add `ProductStack` resource and ability to crea…
1d255d7
remove no console
d6c365a
Merge branch 'master' into prod_stack
arcrank 4020dd4
fix typo in bucketname
4508693
Merge from master
44e2962
addressing comments
4c058f0
simplify parent stack helper
39e2d74
small typo
9cf4842
changes to properly resolve template assets
513c81e
fixing doc string
e7e8c48
Merge branch 'master' into prod_stack
arcrank 7568e24
pkglint failure
82fdbb3
pkglint issue
91431e3
merge updates
7820238
updates
6c6ba43
cleaning up changes to core to revert
82f523a
missing spacing
dba4e7d
simplify
c92f520
Merge branch 'master' into prod_stack
arcrank 5185584
updates
e02b621
Merge branch 'prod_stack' of github.com:arcrank/aws-cdk into prod_stack
0aae44c
Merge branch 'master' into prod_stack
arcrank 4502669
removing no longer necessary exemption
96e9689
Merge branch 'master' into prod_stack
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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); | ||
skinny85 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or 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; | ||
skinny85 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.