smallbirds
smallbirds

Reputation: 1087

How to change StackName when using CDK Deploy

I am having a very simple cdk project:

import * as cdk from '@aws-cdk/core';
import { TestStack } from '../lib/test-stack';

const app = new cdk.App();
new TestStack(app, 'TestStack');

I can easily deploy the stack using cdk deploy and the name of the stack will be "TestStack" on AWS. So far so good. However I want to control the name of the stack in cloudformation. Is there any way I can fdo cdk deploy and change the default name of the stack in AWS?

something like cdk deploy --stack-name SomeName?? ((stack-name is a made up command))

I want to know this because I want to be able to build a deploy system that can build several stacks from a single cdk project. Each of these stacks will then be slightly different based on the input parameters. Something like

cdk deploy --stackName Stack1 --parameters parameters1
cdk deploy --stackName Stack2 --parameters parameters2
cdk deploy --stackName Stack3 --parameters parameters3

And I will end up having both Stack1, Stack2 and Stack3 in AWS.

Upvotes: 14

Views: 11342

Answers (3)

Idan Shemesh
Idan Shemesh

Reputation: 146

I did it with context

//Get data from Conext
const stackName = app.node.tryGetContext('stackName')
new TestCdkProjectStack(app, 'TestCdkProjectStack', {stackName: stackName});

Run the CDK deploy with --context option:

cdk deploy --context stackName=YourStack

You can also add your context data in cdk.json file

Upvotes: 13

Balu Vyamajala
Balu Vyamajala

Reputation: 10393

We can pass stackName as a property, if it's not passed, then it uses Id HelloCdkStack as stack name.

We can pass something like process.env.STACK_NAME to stackName and override it from command line

const app = new cdk.App();
const env = { account: CRM, region: AWS_REGION };
new HelloCdkStack(app, "HelloCdkStack", {
  env,
  stackName: process.env.STACK_NAME,
});

Then

export STACK_NAME=MyStack1;cdk deploy
       OR
export STACK_NAME=MyStack2;cdk deploy --stackName MyStack2

Upvotes: 8

smallbirds
smallbirds

Reputation: 1087

I found a workaround. I can change the stack name in the manifest.json file by using

cat manifest.json | jq '.artifacts.TestStack3.properties.stackName = "MyNewStackName"' >> manifest.json

before each deplot

but I am still interested if anyone know a more clean solution

Upvotes: 1

Related Questions