【问题标题】:Network ACL association to subnet in nondefault VPC at the time of acl creation创建 acl 时与非默认 VPC 中的子网的网络 ACL 关联
【发布时间】:2015-06-15 18:42:40
【问题描述】:

我正在尝试使用 aws cli 从头开始​​设置新的 VPC。 我需要的 VPC 基础设施需要各种子网和与之关联的网络 ACL。默认情况下,我创建的网络 ACL 不与任何子网关联,因为它是一个 非默认 VPC,因此默认关联不存在。 这种情况给我带来了两种情况:

  1. 在创建网络 ACL 时将网络 ACL 关联到子网- create-network-acl aws cli 命令不支持。

  2. 替换旧的子网关联- 要替换子网关联,我需要有一个旧的关联 ID。如前所述,没有子网关联已经存在,因此没有过去的关联可以替换。

是否有任何解决此问题的方法,或者 aws cli 是否支持案例 1,是否有任何命令。

【问题讨论】:

  • 堆栈溢出指南中的哪个要求没有满足。小心解释!
  • 嗨!你找到解决这个问题的方法了吗?我也在尝试使用 AWS CLI 进行相同的操作——我创建了一个子网和一个 ACL,但不幸的是我无法关联它们......@subham

标签: amazon-web-services amazon-vpc aws-cli


【解决方案1】:

我对 AWS 很陌生,我也有任务去做。我是如何做到的:

# get both resource and client
ec2 = boto3.resource( 'ec2' )
ec2_client = boto3.client('ec2' )

# associating an ACL with a subnet is a mess:
# 1) create your own ACL
# 2) all subnets are connected to a default ACL - find this ACL
# 3) get all the association IDs of this connection
# 4) call replace_network_acl_association with your own ACL ID

# create the ACL in your vpc
networkACL = ec2.create_network_acl( VpcId = vpc.id )

# get default ACL
response = ec2_client.describe_network_acls( NetworkAclIds=[], Filters=[] )

# get association IDs
myAssociations = []
for acl in response['NetworkAcls']:
    if( acl["VpcId"] == vpc.id and len( acl['Associations'] ) > 0 ):
        myAssociations = acl['Associations']
        break

# replace them to our ACL
for a in myAssociations:
    ec2_client.replace_network_acl_association(
        AssociationId = a['NetworkAclAssociationId'],
        NetworkAclId = networkACL.id
    )

【讨论】:

    【解决方案2】:

    我看到 CLI 或 API(例如 BOTO3)的最佳方法是重新关联子网。最初创建子网时,它与默认 VPC ACL 关联。然后您可以自己将其重新关联到正确的 ACL。

    【讨论】:

      【解决方案3】:

      使用 CLI 构建复杂的堆栈是一个非常低级的解决方案,我不建议这样做。 我会创建一个 cloudformation 堆栈。它可以是一个修复堆栈或更通用的带有输入参数的堆栈。您可以轻松地手动测试它,如果它运行良好,那么您可以通过 CLI 调用 create Cloudformation 将其集成到您的应用程序中。

      【讨论】:

      • 是的,我理解你所说的,但我的要求迫使我使用 aws cli。你能对此提出一些建议吗?谢谢:)
      猜你喜欢
      • 2020-12-09
      • 2016-02-23
      • 2021-11-16
      • 1970-01-01
      • 2010-10-03
      • 2020-12-31
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      相关资源
      最近更新 更多