Skip to content

Instantly share code, notes, and snippets.

@jitran
Last active September 18, 2022 00:09
Show Gist options
  • Save jitran/60b92b5bbf678db76f3fc28c51b26bfb to your computer and use it in GitHub Desktop.
Save jitran/60b92b5bbf678db76f3fc28c51b26bfb to your computer and use it in GitHub Desktop.
AWS CDK Python - Overriding the resource logical IDs

Install the tools

pyenv install 3.8.1
pyenv local 3.8.1
brew install node
npm install -g aws-cdk@1.31.0

Setup the project

mkdir my_python_sample
cd my_python_sample
cdk init --language python sample-app
source .env/bin/activate
pip install -r requirements.txt

Replace local app.py and my_python_sample_stack.py files with the gists

Synthesise the CloudFormation template

cdk synth

Update the code to use the following and examine the synthesised output:

  • UseOriginalConstructID aspect
  • reset_logical_id()
  • or override_logical_id()
#!/usr/bin/env python3
from aws_cdk import core
from my_python_sample.my_python_sample_stack import MyPythonSampleStack
app = core.App()
MyPythonSampleStack(app, 'my-python-sample', env={'region': 'ap-southeast-2'})
app.synth()
from aws_cdk import (
aws_s3 as s3,
core
)
import jsii
def override_logical_id(resource, logical_id):
""" Overrides the resource logical ID with a specific ID
"""
resource.node.default_child.override_logical_id(logical_id);
def reset_logical_id(resource):
""" Resets the resource logical ID to the original construct ID
"""
resource.node.default_child.override_logical_id(resource.node.id);
@jsii.implements(core.IAspect)
class UseOriginalConstructID:
def visit(self, element):
""" Traverses all the Construct's elements and resets their resource logical ID to the original construct ID
Note:
* This method works best for all explicitly defined resources
* Implicit dependent resources created by the CDK may contain hash based or conflicting IDs
"""
if isinstance(element.node.default_child, core.Construct):
element.node.default_child.override_logical_id(element.node.id)
class MyPythonSampleStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
bucket = s3.Bucket(self, 'MyBucket', bucket_name='my-bucket-name')
# reset_logical_id(bucket)
# override_logical_id(bucket, 'S3BucketResource')
# Apply the UseOriginalConstructID apsect to correct all the resource logical ids
# self.node.apply_aspect(UseOriginalConstructID())
# Auto generated logical ids
Resources:
MyBucketF68F3FF0:
Type: AWS::S3::Bucket
Properties:
BucketName: my-bucket-name
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Metadata:
aws:cdk:path: my-python-sample/MyBucket/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.31.0,...
# Resetting the logical id to the original construct id
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-bucket-name
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Metadata:
aws:cdk:path: my-python-sample/MyBucket/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.31.0,...
# Overriding the logical id with a custom value
Resources:
S3BucketResource:
Type: AWS::S3::Bucket
Properties:
BucketName: my-bucket-name
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Metadata:
aws:cdk:path: my-python-sample/MyBucket/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.31.0,...
@arogozhnikov
Copy link

that's a super-clean example, thank you for summarizing this

@gauravve
Copy link

Great example!!! I am wondering how to override the stack name as resources still get named using stackName-OverridenReourceName-Randomhash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment