Skip to content

Instantly share code, notes, and snippets.

@matef
Created September 23, 2020 13:05
Show Gist options
  • Save matef/9c1626ce6549a86894187901d5c85ee7 to your computer and use it in GitHub Desktop.
Save matef/9c1626ce6549a86894187901d5c85ee7 to your computer and use it in GitHub Desktop.
Fix cdk template URL for nested stack.
import json
import os
import sys
## read file args [asset_file, root_template_file, s3_object_prefix]
asset_file = sys.argv[1]
template_file = sys.argv[2]
object_prefix = sys.argv[3]
hash_map = {}
## read from asset file to build the asset_hash/s3_asset_path map
with open(asset_file,'r') as file:
assets = json.loads(file.read())
for asset_key in assets['files']:
destinations = assets['files'][asset_key]['destinations']
source = assets['files'][asset_key]['source']
for dest in destinations:
hash_map['/'+destinations[dest]['objectKey']] = object_prefix + source['path']
## rewrite the template file after setting TemplateURL to s3_asset_path
with open(template_file,'r+') as file:
template_body = json.loads(file.read())
for resource in template_body['Resources']:
object_key = template_body['Resources'][resource]['Properties']['TemplateURL']['Fn::Join'][-1][-1]
template_body['Resources'][resource]['Properties']['TemplateURL'] = hash_map[object_key]
file.seek(0)
json.dump(template_body, file, indent=2)
file.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment