Shrestha Bikesh
Shrestha Bikesh

Reputation: 94

how to deploy in different environment (dev, uat ,prod) using cdk pipeline?

When I commit to develop branch it must deploy code to specific environment (dev). Similarly when i deploy to uat branch it must deploy to uat environment. How do i achieve this functionality in aws cdk pipeline ? There is stage and be deployed to multiple region but need to define if pushed to this branch then deploy to this environment likewise.

Upvotes: 7

Views: 3067

Answers (2)

nsquires
nsquires

Reputation: 1119

My team uses the inline context args to define an environment name, and from the environment name, it reads a json config file that defines many environment-dependent parameters.

cdk deploy --context env=Dev

We let the environment name determine the branch name and set it accordingly on the 'Branch' property of the 'GitHubSourceAction'. (C# code)

string env = (string)this.Node.TryGetContext("env");

var pipeline = new CdkPipeline(this, "My-Pipeline", new CdkPipelineProps()
{
    SourceAction = new GitHubSourceAction(new GitHubSourceActionProps()
    {
        Branch = env
    })
})

Upvotes: 3

Nick Cox
Nick Cox

Reputation: 6452

The best approach depends on a few factors including whether your stack is environment agnostic or not (i.e. whether it needs to look up resources from within a given account.)

For simply switching between different accounts and regions, the CDK team has a decent writeup here which recommends a small wrapper script for each environment that injects the configuration by way of CDK_DEPLOY_ACCOUNT and CDK_DEPLOY_REGION environment variables.

If you want to provide other synth time context then you can do so via the context API, which allows you to provide configuration 'in six different ways':

Automatically from the current AWS account.

Through the --context option to the cdk command.

In the project's cdk.context.json file.

In the project's cdk.json file.

In the context key of your ~/.cdk.json file.

In your AWS CDK app using the construct.node.setContext method.

Upvotes: 4

Related Questions