FenryrMKIII
FenryrMKIII

Reputation: 1198

What is the correct way to identify production & development environment in AWS CDK?

I am learning AWS Cloud Development Kit (CDK).

As part of this learning, I am trying to understand how I am supposed to correctly handle production and development environment.

I know AWS CDK provides the environment parameter to allow deploying stacks to specific account.

But then, how to have specific options for development versus production stacks ? It does not seem to be provided by default by AWS CDK or am I missing/misunderstanding something ?

A very simple example could be that I want a S3 bucket called my-s3-bucket-dev for my development account and one named my-s3-bucket-prod for my production account. But then how to have e.g. a variable stage correctly handled in AWS CDK ?

I know I can add parameters in the cdk.json file but again, I don't know how to correctly use this file to depend upon the deployed stack i.e. production vs development.

Thanks for the support

Upvotes: 1

Views: 2315

Answers (1)

mchlfchr
mchlfchr

Reputation: 4278

Welcome to AWS CDK. Enjoy the ride. ;)

Actually, there is no semantic (in your case the stage) in an account itself. This has nothing to do with CDK or Cloud Formation. You need to take care of this.

You're right, that you could use the CDK context in the cdk.json. There's no schema enforcement in the context, except for some internally used variables by CDK. You could define your dev and prod objects within. There are other ways of defining the context. Here is an example, what it could look like:

{
  "app": "node app",
  // usually there's some internal definition for your CDK project
  "context": {
    "dev": {
      "accountId" : "prod_account",
      "accountRegion" : "us-east-1",
      "name": "dev",
      "resourceConfig":
      {
       // here you could differentiate the config per AWS resource-type
       // e.g. dev has lower hardware specs
      }
    },
    "prod": {
      "accountId" : "prod_account",
      "accountRegion" : "us-east-1",
      "name": "prod",
      "resourceConfig":
      {
       // here you could differentiate the config per AWS resource-type
       // prod has higher hardware specs or more cluster nodes
      }
    }

  }
}

With this being defined, you need to run your CDK application with the -c flag to specify which configuration object (dev or prod), you want to have. For instance, you could run it with cdk synth -c stage=prod. This sets the stage variable in your context and makes it available. When it was successful, you can re-access the context again and fetch the appropriate config object.

const app = new cdk.App();
const stage = app.node.tryGetContext('stage');
// the following step is only needed, if you have a different config per account 
const stageConfig = app.node.tryGetContext(stage );
// ... do some validation and pass the config to the stacks as constructor argument

As I said, the context is one way of doing this. However, there are drawbacks to it. It's JSON and no code.

What I prefer is to have TypeScript types per resource configuration (e.g. S3) and wire them all together as a plain object. The object maps the account/region information and the corresponding resource configurations.

Upvotes: 7

Related Questions