Skip to main content

cdk-stack-resource-rename

Project description

NPM version PyPI version Nuget version Release

StackResourceRenamer

A CDK aspect, StackResourceRenamer renames CDK stack name and stack's subordinate resources' physical names, so that a CDK stack can be used to create multiple stacks in same AWS environment without confliction.

API: API.md

Two main use cases:

  1. rename custom resources names in stack, so that stack can be reused and replicated:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
StackResourceRenamer.rename(stack,
    rename=(resName, _)=>{
            return resName+'-'+alias;
        }
)
  1. for resources without custom name, which by default will use unique id AWS auto generate as its physical id, we can create a more readable and identifiable name, for testing, debugging or metrics monitoring environments.
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
StackResourceRenamer.rename(stack, {
    "rename": (_, typeName)=>{
            counts[typeName]++;
            return projectName+'-'+serviceName+'-'+typeName+'-'+counts[typeName];
        }
}, user_custom_name_only=False)

Samples

typescript

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk_stack_resource_rename import StackResourceRenamer

app = core.App()
stack = core.Stack(app, "my-stack")

alias = stack.node.try_get_context("alias")
if alias:
    # if alias is defined, rename stack and resources' custom names
    StackResourceRenamer.rename(stack,
        rename=(resName, _)=>{
                    return resName+'-'+alias;
                }
    )

# resources in stack
bucket = s3.Bucket(stack, "bucket",
    bucket_name="my-bucket"
)

python

from cdk_stack_resource_rename import (StackResourceRenamer, IRenameOperation)

@jsii.implements(IRenameOperation)
class RenameOper:
    def __init__(self, alias):
        self.alias=alias
    def rename(self, resName, typeName):
        return resName+'-'+self.alias

class AppStack(core.Stack):
    def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
        ......
        alias = self.node.try_get_context("alias")
        if alias != None:
            # if alias is defined, rename stack/resources' custom names
            StackResourceRenamer.rename(self, RenameOper(alias))

java

import io.github.yglcode.cdkutils.aspects.resourcerename.StackResourceRenamer;
import io.github.yglcode.cdkutils.aspects.resourcerename.IRenameOperation;

public class AppStack extends Stack {
    ......
    String alias = (String) this.getNode().tryGetContext("alias");
    if (alias != null) {
        StackResourceRenamer.rename(this, new IRenameOperation() {
            public String rename(String resName, String typeName) {
                return resName + "-"+alias;
            }
        });
    }

csharp

using CdkUtils.Aspects.ResourceRename;
public class RenameOper: Amazon.JSII.Runtime.Deputy.DeputyBase, IRenameOperation {
    private string alias;
    public RenameOper(string alias) {
        this.alias=alias;
    }
    public string Rename(string resName, string typeName) {
        return resName+"-"+alias;
    }
}
public class AppStack : Stack
{
    internal AppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
    {
        ......
        var alias = (string)this.Node.TryGetContext("alias");
        if (alias!=null) {
            StackResourceRenamer.Rename(this, new RenameOper(alias));
        }

To create multiple stacks:

cdk -c alias=a1 deploy will create a stack: my-stack-a1 with my-bucket-a1.

To create more stacks: my-stack-a2 with my-bucket-a2, my-stack-a3 with my-bucket-a3:

cdk -c alias=a2 deploy

cdk -c alias=a3 deploy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-stack-resource-rename-0.0.20.tar.gz (40.1 kB view details)

Uploaded Source

Built Distribution

cdk_stack_resource_rename-0.0.20-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

Details for the file cdk-stack-resource-rename-0.0.20.tar.gz.

File metadata

  • Download URL: cdk-stack-resource-rename-0.0.20.tar.gz
  • Upload date:
  • Size: 40.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.9

File hashes

Hashes for cdk-stack-resource-rename-0.0.20.tar.gz
Algorithm Hash digest
SHA256 3f28a14dea2e69d27c3a975816db48477a548815d2cf74647bc0135ce76f7a85
MD5 1df0fe12c4b1841b6a85f1b5ee4db99f
BLAKE2b-256 d9b4cfb68a5d72847908dd469d64348f5b64073b019f2b9272d87ae0f89a2973

See more details on using hashes here.

File details

Details for the file cdk_stack_resource_rename-0.0.20-py3-none-any.whl.

File metadata

  • Download URL: cdk_stack_resource_rename-0.0.20-py3-none-any.whl
  • Upload date:
  • Size: 42.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.9

File hashes

Hashes for cdk_stack_resource_rename-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 ae11cc5e501e1fb598a3c14e151c8946b25dd70875c12a62d43e25be73e2c635
MD5 7eeb5c5123d3c82d348213be012ca344
BLAKE2b-256 9355eff2550dfd7fc4c39f0a2a1432b8663c7755f5a4f36e00a2a3b9f6d8eaa3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page