Skip to content
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

chore(overrides): add README to "overrides" example #69

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions typescript/resource-overrides/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Resource Override Example
=========================

This example shows the use of the resource overrides ("escape hatch") mechanism.
We add an `AWS::S3::Bucket` resource, and then proceed to change the properties
of the underlying CloudFormation resource.

There are two steps:

* Access the underlying CloudFormation resource by using
`construct.node.defaultChild` or `construct.node.findChild()`.
* Change the resource by the various `add[Property]Override()` methods,
or assigning to properties or `cfnOptions`.

**NOTE** The point is to show how to change various aspects of the generated
CloudFormation template. The end result is a template that cannot be succesfully
deployed!
12 changes: 7 additions & 5 deletions typescript/resource-overrides/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ class ResourceOverridesExample extends cdk.Stack {
encryption: s3.BucketEncryption.KMS_MANAGED
});

const bucketResource2 = bucket.node.findChild('Resource') as s3.CfnBucket;
const bucketResource2 = bucket.node.defaultChild as s3.CfnBucket;
bucketResource2.addPropertyOverride('BucketEncryption.ServerSideEncryptionConfiguration.0.EncryptEverythingAndAlways', true);
bucketResource2.addPropertyDeletionOverride('BucketEncryption.ServerSideEncryptionConfiguration.0.ServerSideEncryptionByDefault');

//
// Accessing the L1 bucket resource from an L2 bucket
//

const bucketResource = bucket.node.findChild('Resource') as s3.CfnBucket;
const bucketResource = bucket.node.defaultChild as s3.CfnBucket;
const anotherWay = bucket.node.children.find(c => (c as cdk.CfnResource).cfnResourceType === 'AWS::S3::Bucket') as s3.CfnBucket;
assert.equal(bucketResource, anotherWay);

//
// This is how to specify resource options such as dependencies, metadata, update policy
//

bucketResource.node.addDependency(otherBucket.node.findChild('Resource') as cdk.CfnResource);
bucketResource.node.addDependency(otherBucket.node.defaultChild as cdk.CfnResource);
bucketResource.cfnOptions.metadata = { MetadataKey: 'MetadataValue' };
bucketResource.cfnOptions.updatePolicy = {
autoScalingRollingUpdate: {
Expand All @@ -56,7 +56,8 @@ class ResourceOverridesExample extends cdk.Stack {
bucketResource.addPropertyOverride('Token', otherBucket.bucketArn); // use tokens
bucketResource.addPropertyOverride('LoggingConfiguration.DestinationBucketName', otherBucket.bucketName);

bucketResource.addPropertyOverride('AnalyticsConfigurations', [
// Assign completely new property value
bucketResource.analyticsConfigurations = [
{
id: 'config1',
storageClassAnalysis: {
Expand All @@ -69,8 +70,9 @@ class ResourceOverridesExample extends cdk.Stack {
}
}
}
]);
];

// Or selectively override parts of it
bucketResource.addPropertyOverride('CorsConfiguration.CorsRules', [
{
AllowedMethods: [ 'GET' ],
Expand Down