Search code examples
identifieraws-cdk

AWS CDK: fixed logical ids


Currently logical ID of a resource is formed by concatenating the names of all of the constructs in the resource’s path and appending an eight-character MD5 hash.

This produces garbage like VpcPrivateSubnet1DefaultRouteBE02A9ED and unfortunatelly makes it unable to query the resources by their logical id.

Is there any way to control how logical ids are named?


Solution

  • In TypeScript the method you are looking for is overrideLogicalId. But you have to get the lower level CfnVpc construct first by using the following code (TypeScript again):

     let vpc = new ec2.Vpc(this, 'vpc', { natGateways: 1 })
     let cfnVpc = vpc.node.defaultChild as ec2.CfnVPC
     cfnVpc.overrideLogicalId('MainVpc')
    

    Results in the following yaml:

      MainVpc:
        Type: AWS::EC2::VPC