Skip to content

Commit

Permalink
chore(overrides): add README to "overrides" example
Browse files Browse the repository at this point in the history
The README explains that the example does not end up in a deployable
state.

Also slighly edit example to use some of the nicer new features.

Fixes #68.
  • Loading branch information
rix0rrr committed Jul 8, 2019
1 parent 8ec63ed commit 23098ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
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

0 comments on commit 23098ca

Please sign in to comment.