如何使用Python在AWS CDK中构造DHCPoptionAssociation

2024-05-15 03:53:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

from aws_cdk import (
    aws_ec2 as ec2,
    core,
)

class MyVpcStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
    super().__init__(scope, id, **kwargs)

    # The code that defines your stack goes here 
    vpc = ec2.Vpc(
        self, 'MyVpc',
        cidr='10.10.10.0/23',
        max_azs=2
    )

    dhcp_options = ec2.CfnDHCPOptions(
        self, 'MyDhcpOptions', 
        domain_name='aws-prod.mydomain.com', 
        domain_name_servers=['10.1.1.5','10.2.1.5'],
        ntp_servers=['10.1.1.250','10.2.1.250'],
    )

    dhcp_options_associations = ec2.CfnVPCDHCPOptionsAssociation(
        self, 'MyDhcpOptionsAssociation', 
        dhcp_options_id=dhcp_options.logical_id, 
        vpc_id=vpc.vpc_id
    )

它在CloudFormation模板中错误地为该生成了VPCDHCPOptionsAssociation属性,如下所示:

  MyDhcpOptionsAssociation:
    Type: AWS::EC2::VPCDHCPOptionsAssociation
    Properties:
      DhcpOptionsId: MyDhcpOptions
      VpcId:
        Ref: myvpcAB8B6A91

我需要CloudFormation模板中的此部分如下(正确):

  MyDhcpOptionsAssociation:
    Type: AWS::EC2::VPCDHCPOptionsAssociation
    Properties:
      DhcpOptionsId: 
        Ref: MyDhcpOptions
      VpcId:
        Ref: myvpcAB8B6A91

如果我使用dhcp_options_id=dhcp_options.id,我会得到错误AttributeError: 'CfnDHCPOptions' object has no attribute 'id'

如果我使用dhcp_options_id=dhcp_options.dhcp_options_id,我会得到错误AttributeError: 'CfnDHCPOptions' object has no attribute 'dhcp_options_id'

以下是此的CDKAPI参考:https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2/CfnVPCDHCPOptionsAssociation.html


Tags: coreselfawsrefid错误ec2vpc
1条回答
网友
1楼 · 发布于 2024-05-15 03:53:18

我找到了。它必须是.ref,尽管与其他资源属性不一致

dhcp_options_associations = ec2.CfnVPCDHCPOptionsAssociation(
    self, 'MyDhcpOptionsAssociation', 
    dhcp_options_id=dhcp_options.ref, 
    vpc_id=vpc.vpc_id
)

相关问题 更多 >

    热门问题