11

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
smallbirds
  • 877
  • 12
  • 35

3 Answers3

10

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

Idan Shemesh
  • 116
  • 1
  • 2
7

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
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42
  • Thanks but this only works for me when before synthesizing and not after... e.g. export STACK_NAME=MyStack;cdk deploy --app 'cdk.out/' does not work as intended – smallbirds Apr 07 '21 at 15:39
  • 1
    if template is already synthesized, why do we need to use cdk command, we can just do [aws clooudformation deploy] ?(https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/deploy/index.html) – Balu Vyamajala Apr 07 '21 at 15:52
  • thanks :) it works. I guess i was too caught up in cdk to realize that I can use aws cloudformation for this purpose – smallbirds Apr 07 '21 at 16:34
  • We have all been there – Gustav Rasmussen Jul 23 '21 at 13:41
  • one reason is that aws cloudformation deploy has zero feedback on progress or failures. – Adrian Baker Sep 09 '21 at 03:39
1

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

smallbirds
  • 877
  • 12
  • 35