Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 576
smallbirds Avatar asked Apr 07 '21 09:04

smallbirds


2 Answers

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
like image 63
Balu Vyamajala Avatar answered Oct 22 '22 02:10

Balu Vyamajala


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

like image 3
Idan Shemesh Avatar answered Oct 22 '22 03:10

Idan Shemesh