AWS CDK Region Info
The `aws-cdk-region-info` library provides a comprehensive directory of AWS region-specific information, such as service principal names for IAM policies, S3 static website endpoints, and other essential regional facts. As a core component of the AWS Cloud Development Kit (CDK) v2, it enables CDK applications to dynamically adapt to regional differences. The library is actively maintained and receives frequent updates, typically alongside new AWS CDK releases, with the current version being 2.250.0.
Common errors
-
Error: Cannot find module 'aws-cdk-lib/core/lib/errors'
cause An internal dependency resolution issue within specific versions of `aws-cdk-region-info` prevents it from finding a required module from `aws-cdk-lib/core`.fixCheck your `aws-cdk-region-info` version. If it's within the affected range (e.g., 2.200.1 - 2.232.2), try downgrading to a stable preceding version (e.g., 2.199.0) or upgrading to a version released after the fix was applied. -
cdk synth / cdk deploy uses the wrong AWS account or region for deployment.
cause The AWS CDK CLI overwrites `CDK_DEFAULT_ACCOUNT` and `CDK_DEFAULT_REGION` if set as shell environment variables. Additionally, 'environment-agnostic' stacks might default to your AWS CLI's configured profile.fixAvoid using `CDK_DEFAULT_ACCOUNT` or `CDK_DEFAULT_REGION` directly. Instead, explicitly define the `env` property (account and region) for your CDK `Stack` instances in your code. For custom overrides, define and use your own environment variables (e.g., `MY_ACCOUNT`, `MY_REGION`) within your application code. -
region_info.RegionInfo.get('some-new-region').some_fact returns None or incorrect value.cause The built-in database of regional facts might not yet include information for very new AWS regions or specific, recently launched service features, or might contain outdated information.fixVerify the fact's existence and correctness in the official AWS documentation. If missing or incorrect, use `region_info.Fact.register()` to programmatically inject or override the specific fact for your application. Consider contributing the correction to the AWS CDK project via a GitHub issue.
Warnings
- breaking Specific versions of `aws-cdk-region-info` (e.g., 2.200.1 up to 2.232.2) have experienced a regression causing a `Cannot find module 'aws-cdk-lib/core/lib/errors'` error at runtime due to internal dependency issues within the CDK ecosystem. This can completely block CDK application execution.
- gotcha The AWS CDK CLI (e.g., `cdk synth`, `cdk deploy`) overrides any user-set `CDK_DEFAULT_ACCOUNT` and `CDK_DEFAULT_REGION` environment variables. If you rely on these for specific deployments, the CLI's behavior might lead to unintended targets.
- gotcha While `aws-cdk-region-info` aims to be comprehensive, new AWS regions or very specific service endpoints might initially be missing or have outdated facts. Relying solely on the built-in data might lead to `None` values or incorrect behavior for bleeding-edge features or newly launched regions.
Install
-
pip install aws-cdk-region-info
Imports
- RegionInfo
from aws_cdk.region_info import RegionInfo # Not the standard import pattern in Python CDK
from aws_cdk import region_info # ... info = region_info.RegionInfo.get("us-east-1") - Fact
from aws_cdk.region_info.Fact import find # Incorrect submodule access
from aws_cdk import region_info # ... endpoint = region_info.Fact.find("ap-northeast-1", region_info.FactName.S3_STATIC_WEBSITE_ENDPOINT)
Quickstart
import os
from aws_cdk import region_info
# Get information for a specific region
us_east_1_info = region_info.RegionInfo.get("us-east-1")
print(f"S3 Static Website Endpoint for us-east-1: {us_east_1_info.s3_static_website_endpoint}")
# Get a service principal for a region
s3_service_principal = us_east_1_info.service_principal("s3.amazonaws.com")
print(f"S3 Service Principal for us-east-1: {s3_service_principal}")
# Find a specific fact for a region using Fact.find()
# Example: Get the domain suffix for a China region
cn_north_1_domain_suffix = region_info.Fact.find("cn-north-1", region_info.FactName.DOMAIN_SUFFIX)
print(f"Domain Suffix for cn-north-1: {cn_north_1_domain_suffix}")
# Override or add a custom fact (e.g., for a new or hypothetical region)
# This is for demonstration; normally you'd implement IFact
class MyCustomFact:
def __init__(self, region, name, value):
self.region = region
self.name = name
self.value = value
@region_info.jsii.implements(region_info.IFact)
class CustomEndpointFact(MyCustomFact):
pass
custom_region = "bermuda-triangle-1"
custom_endpoint_fact = CustomEndpointFact(
region=custom_region,
name=region_info.FactName.S3_STATIC_WEBSITE_ENDPOINT,
value="s3-website.bermuda-triangle-1.nowhere.com"
)
region_info.Fact.register(custom_endpoint_fact)
# Retrieve the custom fact
retrieved_custom_endpoint = region_info.RegionInfo.get(custom_region).s3_static_website_endpoint
print(f"Custom S3 Static Website Endpoint for {custom_region}: {retrieved_custom_endpoint}")