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

标签 python python-3.x aws-cloudformation aws-cdk

我有以下代码:

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'

以下是 CDK API 引用:https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2/CfnVPCDHCPOptionsAssociation.html

最佳答案

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

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

关于python - 如何使用 Python 在 AWS CDK 中构建 DHCPOptionsAssociation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61927576/

相关文章:

python - 从 python 重启本地计算机

java - 如何使用 python 和 Java 运行 Docker?

用于 Unicode 字符的 Python 3 len() 函数

aws-cloudformation - 如何使用 CloudFormation 不间断地迁移到 OpenSearch?

amazon-web-services - 向发布到 CloudWatch 的 API Gateway 日志添加保留策略

python - 定义顺序在模块命名空间中可用吗?

python - 如何在python中循环2个函数?

python - 根据关键字和位置数据识别文档中的 block /词组?

python - 如何让streamHandler每次都覆盖

amazon-web-services - "dynamodb:*Reserved*"有什么作用?