AWS CDK:固定逻辑 ID
AWS CDK: fixed logical ids
目前,资源的逻辑 ID 是通过连接资源路径中所有构造的名称并附加八个字符的 MD5 哈希值形成的。
这会产生类似 VpcPrivateSubnet1DefaultRouteBE02A9ED
的垃圾,不幸的是它无法通过逻辑 ID 查询资源。
有什么方法可以控制逻辑 ID 的命名方式吗?
在 TypeScript 中,您要查找的方法是 overrideLogicalId
。但是您必须首先使用以下代码(再次使用 TypeScript)获得较低级别的 CfnVpc 构造:
let vpc = new ec2.Vpc(this, 'vpc', { natGateways: 1 })
let cfnVpc = vpc.node.defaultChild as ec2.CfnVPC
cfnVpc.overrideLogicalId('MainVpc')
结果如下:
MainVpc:
Type: AWS::EC2::VPC
晚会有点晚了,但这是我的实现。我删除了字符串末尾的随机字符,并将其替换为整个项目中唯一的逻辑 ID。
protected allocateLogicalId(cfnElement: CfnElement): string {
return cfnElement.logicalId.split('.')[1];
}
对于在 Python
中寻找解决方案的任何人:
from aws_cdk import aws_lambda as lambda_
api_function = lambda_.Function(
self,
"lambda_func",
code=lambda_.Code.from_bucket(bucket=s3_bucket, key='lambda.zip'),
runtime=typing.cast(lambda_.Runtime, lambda_.Runtime.PYTHON_3_9), # Bug in CDK/PyLance; use cast to mitigate for now
timeout=Duration.seconds(900),
handler="rest_client.handler"
)
cfn_func = api_function.node.default_child
cfn_func.override_logical_id("NewLogicalId")
(注意:PyLance
会抛出以下错误,但 cdk ls
和 cdk synth
会按预期工作:
override_logical_id: Unknown
Cannot access member "override_logical_id" for type "IConstruct"
Member "override_logical_id" is unknownPylancereportGeneralTypeIssues
"override_logical_id" is not a known member of "None"
请注意:通常不建议更改逻辑 ID,因为更改资源的逻辑 ID 会导致资源被替换。旧资源将被删除,并创建具有新逻辑 ID 的新资源。
来自文档:
Avoid changing the logical ID of a resource after it has been created. Since AWS CloudFormation identifies resources by their logical ID, if you change the logical ID of a resource, AWS CloudFormation deletes the existing resource, and then creates a new resource with the new logical ID, which may cause service interruption or data loss.
目前,资源的逻辑 ID 是通过连接资源路径中所有构造的名称并附加八个字符的 MD5 哈希值形成的。
这会产生类似 VpcPrivateSubnet1DefaultRouteBE02A9ED
的垃圾,不幸的是它无法通过逻辑 ID 查询资源。
有什么方法可以控制逻辑 ID 的命名方式吗?
在 TypeScript 中,您要查找的方法是 overrideLogicalId
。但是您必须首先使用以下代码(再次使用 TypeScript)获得较低级别的 CfnVpc 构造:
let vpc = new ec2.Vpc(this, 'vpc', { natGateways: 1 })
let cfnVpc = vpc.node.defaultChild as ec2.CfnVPC
cfnVpc.overrideLogicalId('MainVpc')
结果如下:
MainVpc:
Type: AWS::EC2::VPC
晚会有点晚了,但这是我的实现。我删除了字符串末尾的随机字符,并将其替换为整个项目中唯一的逻辑 ID。
protected allocateLogicalId(cfnElement: CfnElement): string {
return cfnElement.logicalId.split('.')[1];
}
对于在 Python
中寻找解决方案的任何人:
from aws_cdk import aws_lambda as lambda_
api_function = lambda_.Function(
self,
"lambda_func",
code=lambda_.Code.from_bucket(bucket=s3_bucket, key='lambda.zip'),
runtime=typing.cast(lambda_.Runtime, lambda_.Runtime.PYTHON_3_9), # Bug in CDK/PyLance; use cast to mitigate for now
timeout=Duration.seconds(900),
handler="rest_client.handler"
)
cfn_func = api_function.node.default_child
cfn_func.override_logical_id("NewLogicalId")
(注意:PyLance
会抛出以下错误,但 cdk ls
和 cdk synth
会按预期工作:
override_logical_id: Unknown
Cannot access member "override_logical_id" for type "IConstruct"
Member "override_logical_id" is unknownPylancereportGeneralTypeIssues
"override_logical_id" is not a known member of "None"
请注意:通常不建议更改逻辑 ID,因为更改资源的逻辑 ID 会导致资源被替换。旧资源将被删除,并创建具有新逻辑 ID 的新资源。
来自文档:
Avoid changing the logical ID of a resource after it has been created. Since AWS CloudFormation identifies resources by their logical ID, if you change the logical ID of a resource, AWS CloudFormation deletes the existing resource, and then creates a new resource with the new logical ID, which may cause service interruption or data loss.