Metadata-Version: 2.1
Name: cdk-stack-resource-rename
Version: 0.0.16
Summary: cdk-stack-resource-rename
Home-page: https://github.com/yglcode/cdk-stack-resource-rename.git
Author: Yigong Liu<ygl.code@gmail.com>
License: Apache-2.0
Project-URL: Source, https://github.com/yglcode/cdk-stack-resource-rename.git
Platform: UNKNOWN
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: JavaScript
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Typing :: Typed
Classifier: Development Status :: 5 - Production/Stable
Classifier: License :: OSI Approved
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Requires-Dist: aws-cdk.core (<2.0.0,>=1.91.0)
Requires-Dist: constructs (<4.0.0,>=3.2.27)
Requires-Dist: jsii (<2.0.0,>=1.24.0)
Requires-Dist: publication (>=0.0.3)

[![NPM version](https://badge.fury.io/js/cdk-stack-resource-rename.svg)](https://badge.fury.io/js/cdk-stack-resource-rename)
[![PyPI version](https://badge.fury.io/py/cdk-stack-resource-rename.svg)](https://badge.fury.io/py/cdk-stack-resource-rename)
[![Nuget version](https://badge.fury.io/nu/cdk-stack-resource-rename.svg)](https://badge.fury.io/nu/CdkUtils.Aspects.ResourceRename)
![Release](https://github.com/yglcode/cdk-stack-resource-rename/workflows/Release/badge.svg)

## 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](https://github.com/yglcode/cdk-stack-resource-rename/blob/main/API.md)

Two main use cases:

1. rename custom resources names in stack, so that stack can be reused and replicated:

```python
# 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.

```python
# 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*

```python
# 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*

```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*

```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*

```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`


