如何在AWS CDK中使用Python构建DHCPOptionsAssociation?

问题描述 投票:0回答:1

我有以下代码。

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
    )

它产生了 VPCDHCPOptionsAssociation 属性不正确,CloudFormation 模板中的此部分是这样的。

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

我需要CloudFormation模板中的这部分是这样的(正确的)。

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

如果我在CloudFormation模板中使用 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'.

这里是CDK API的参考。https:/docs.aws.amazon.comcdkapilatestpythonaws_cdk.aws_ec2CfnVPCDHCPOptionsAssociation.html。

python python-3.x amazon-cloudformation aws-cdk
1个回答
2
投票

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

dhcp_options_associations = ec2.CfnVPCDHCPOptionsAssociation(
    self, 'MyDhcpOptionsAssociation', 
    dhcp_options_id=dhcp_options.ref, 
    vpc_id=vpc.vpc_id
)
© www.soinside.com 2019 - 2024. All rights reserved.