aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.36k stars 3.77k forks source link

cdk: missing ca-west-1 region in aws-entities causes cdk vended custom resources to fail deployment #30694

Open dreamorosi opened 1 week ago

dreamorosi commented 1 week ago

Describe the bug

From 2.146.0 onwards, cdk vended custom resources are now region aware - this change seems to have been introduced in #30108.

To my understanding this means that when deploying a cdk vended custom resource, the LATEST_NODE_RUNTIME_MAP is used to lookup the Node.js version to use in the underlying Lambda function.

This in turn appears to look up the regions in the AWS_REGIONS_AND_RULES map in the packages/aws-cdk-lib/region-info/lib/aws-entities.ts file - which is missing an AWS region (ca-west-1).

Expected Behavior

I should be able to deploy a CDK stack that uses cdk-vended custom resources in this region.

Current Behavior

When deploying in ca-west-1, the stack fails due to the missing region in the Mapping node of the generated CloudFormation stack:

❌ Deployment failed: Error [ValidationError]: Template error: Unable to get mapping for LatestNodeRuntimeMap::ca-west-1::value

Reproduction Steps

  1. Initialize a new CDK app with npx aws-cdk@latest init app --language typescript
  2. Open lib/test-stack.ts
  3. Paste code below
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Code, Function as LambdaFunction, Runtime } from 'aws-cdk-lib/aws-lambda';

export class TestRegionIssueStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new LambdaFunction(this, 'TestFunction', {
      runtime: Runtime.NODEJS_20_X,
      handler: 'index.handler',
      code: Code.fromInline('exports.handler = async () => "Hello, world!";'),
      logRetention: 7, // this adds a cdk vended custom resource
    });
  }
}

This will generate a cdk vended custom resource because of the logRetention prop.

  1. Run npm run cdk synth to generate the CloudFormation stack file
  2. Open the cdk.out/TestStack.template.json and find the Mappings.LatestNodeRuntimeMap
  3. Observe that the ca-west-1 region is missing from the mapping
  4. Set region to ca-west-1 via export AWS_REGION=ca-west-1
  5. Observe error during CloudFormation changeset creation
 ❌  TestStack failed: Error [ValidationError]: Template error: Unable to get mapping for LatestNodeRuntimeMap::ca-west-1::value

Possible Solution

No response

Additional Information/Context

My team Powertools for AWS Lambda deploys Lambda layers in all regions, including the missing one.

We discovered the issue in our canaries which were now failing to deploy in ca-west-1. Downgrading to 2.145.0 fixed the issue.

CDK CLI Version

2.146.0

Framework Version

No response

Node.js Version

20.x

OS

Linux, Mac

Language

TypeScript

Language Version

No response

Other information

No response

ashishdhingra commented 1 week ago

Findings: