AWS CDK: fixed logical ids

六眼飞鱼酱① 提交于 2020-08-23 05:01:10

问题


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?


回答1:


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



回答2:


A bit late to the party but here is my implementation. I removed the random characters at the end of the string and replaced it with the logical ID which are unique throughout the project.

protected allocateLogicalId(cfnElement: CfnElement): string {
  return cfnElement.logicalId.split('.')[1];
}



来源:https://stackoverflow.com/questions/57213637/aws-cdk-fixed-logical-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!