Reputation: 11831
Is it possible to deploy a CDK app to the same account multiple times? I want to run synth
once and the run cdk deploy
against that synthesised template multiple times.
I can see that the recent 1.28.0 release of the CDK allows for passing CloudFormation parameters into the deploy command
(via #1237). This means I can parameterize the contents of a stack, but I don't know how to change the name/id of the app itself.
For example, here is a simple app:
public class ExampleApp {
public static void main(final String[] args) {
App app = new App();
new ExampleStack(app, "ExampleStack");
app.synth();
}
}
and here is a simple do-nothing stack:
public class ExampleStack extends Stack {
public ExampleStack(final Construct scope, final String id) {
this(scope, id, null);
}
public ExampleStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
CfnParameter someVar = CfnParameter.Builder.create(this, "SomeVar")
.description("Some variable that can be passed in at deploy-time.")
.type("String")
.build();
// rest of stack here
}
}
I can run cdk synth
and output the template somewhere, then run
cdk --app path/to/cdk.out deploy ExampleStack --parameters "ExampleStack:SomeVar=SomeValue"
and the parameter will be passed into the stack at deploy-time.
However, I don't see how to deploy the app multiple times with different names (or ids). Is this possible?
The background to why I want to do this, NOT run synth
multiple times, is because for compliance reasons, I need a single artifact - the cdk.out
directory - and then deploy that multiple times. To that end, I can't use answers based around multiple runs of synth
.
Upvotes: 5
Views: 3703
Reputation: 5501
Try this:
Any resources in your stack with with service level names or IDs need to also be modified, for example two stacks can't create a secret with the same name
In my solution I derived from the StackProps object to add a "PrefixName" property to the stack and the prefix can be used within my stack to influence the naming or resources.
My program.cs looks as follows:
using Amazon.CDK;
namespace DevconnectListener
{
sealed class Program
{
public static void Main(string[] args)
{
var app = new App();
var props = new DevConnectStackProps()
{
PrefixName = args[0],
StackName = args[0] + "-DevconnectListenerStack"
};
var s = new DevconnectListenerStack(app, "DevconnectListenerStack", props);
app.Synth();
}
}
}
Here is my custom DevConnectStackProps class:
using System.Collections.Generic;
using Amazon.CDK;
using Amazon.CDK.AWS.DynamoDB;
using Amazon.CDK.AWS.Lambda;
namespace DevconnectListener
{
public class DevConnectStackProps : StackProps, IStackProps
{
public DevConnectStackProps()
{
}
public string PrefixName { get; set; }
}
}
My cdk.json look like this, removed the app property:
{
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"@aws-cdk/core:stackRelativeExports": "true"
}
}
Then there is a CMD file named deploy-dev.cmd (which contains the aws profile i want to use and the dev prefix parameter:
cdk deploy --app "dotnet run -p src/DevconnectListener/DevconnectListener.csproj dev" --profile dev_aws_profile
If you run this again with a different prefix, you'll see a new stack in the CloudFormation console.
Upvotes: 7