Search code examples
aws-cdk

aws cdk synth takes wrong region


In learning the AWS CDK (Python version), I am seeing a strange effect in the Default Region used by "cdk synth" command.

It looks like it changes the "CDK_DEFAULT_REGION" value somwhere, and I can't find where (and cannot change it)

This is the situation:

Env Vars:

(.venv) $ set | grep CDK
CDK_DEFAULT_ACCOUNT=99999999999999
CDK_DEFAULT_REGION=eu-central-1

cdk.json

Only the first 2 lines, to prove that app.py is used

{
  "app": "python3 app.py",

app.py

As you see, an empty app.py, no Stacks defined.

#!/usr/bin/env python3
import os
import logging

import aws_cdk
from packages.cdk_testing.cdk_testing_stack import CdkTestingStack


if __name__ == "__main__":

    logging.basicConfig(format='%(asctime)s [%(levelname)5s] %(message)s',
                        datefmt='%Y-%m-%dT%H:%M:%S',
                        level=logging.NOTSET)

    app = aws_cdk.App()
    logging.info(f"{os.environ['CDK_DEFAULT_ACCOUNT']} is account, and {os.environ['CDK_DEFAULT_REGION']} is region")

    CdkTestingStack(
        app,
        "cdk-testing",
        env=aws_cdk.Environment(
            account=os.getenv('CDK_DEFAULT_ACCOUNT'),
            region=os.getenv('CDK_DEFAULT_REGION')
        ),
    )
    
    app.synth()

Stack file

from aws_cdk import (
    Stack
)
from constructs import Construct
from os import path


class CdkTestingStack(Stack):

    def __init__(self, scope: Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Empty Stack, for now

Running it in Python

(.venv) $ py app.py
2023-08-20T12:01:57 [ INFO] 99999999999999 is account, and eu-central-1 is region

CDK_DEFAULT_REGION (eu-central-1) is used.

Running the 'cdk synth'

(.venv) $ cdk synth
2023-08-20T12:02:47 [ INFO] 99999999999999 is account, and us-east-1 is region

This app contains no stacks

But 'cdk synth' decides to use eu-east-1 !!??

Question

  • Where is 'cdk synth' taking the region from
  • How can I make sure that my own CDK_DEFAULT_REGION is used?

Solution !!

Thanks to @fedonev for pointing this out.

Don't use the ENV Vars called CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION.

They are used by AWS CDK itself.

I've fixed it by changing them to MY_AWS_DEFAULT_ACCOUNT and MY_AWS_DEFAULT_REGION and used those in the code. And now it works.


Solution

  • Where is 'cdk synth' taking the region from?

    Docs: [CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION] are set based on the AWS profile specified using the --profile option, or the default AWS profile if you don't specify one.

    How can I make sure that my own CDK_DEFAULT_REGION is used?

    You can't. The AWS CDK CLI sets the variable when you run toolkit commands like cdk synth, overwriting any value you have previously set.

    Instead, define your own env vars:

    Docs: You can set env however you like, using any valid expression. For example, you might write your stack to support two additional environment variables to let you override the account and Region at synthesis time. We'll call these CDK_DEPLOY_ACCOUNT and CDK_DEPLOY_REGION here, but you could name them anything you like, as they are not set by the AWS CDK.