gpt4 book ai didi

amazon-ec2 - 通过 AWS 和 CloudFormation 自动分配 IPv6 地址

转载 作者:行者123 更新时间:2023-12-02 02:10:53 26 4
gpt4 key购买 nike

有没有办法在自动扩展组+启动配置中将 IPv6 地址自动分配给 EC2 实例?

VPC 和子网均已针对 IPv6 设置。手动创建实例就可以了。我也可以手动分配它们,但我似乎找不到在 CloudFormation 中执行此操作的方法。

最佳答案

当前状态是 CloudFormation 对 IPv6 的支持是可行的。不有趣也不完整,但你可以用它构建一个堆栈 - 我必须使用 2 个自定义资源:

  • 第一个是通用资源,我将其用于其他用途,也在此处重用,以解决缺少的功能,从 VPC 的/56 自动提供的网络构建子网/64 CIDR block
  • 我必须专门添加另一个来解决 CloudFormation 正确使用的 EC2 API 中的错误。

这是我的设置:

1。将 IPv6 CIDR block 添加到您的 VPC:

VPCipv6:
Type: "AWS::EC2::VPCCidrBlock"
Properties:
VpcId: !Ref VPC
AmazonProvidedIpv6CidrBlock: true

2。提取网络前缀以创建/64 子网:

As explained in this answer.

VPCipv6Prefix:
Type: Custom::Variable
Properties:
ServiceToken: !GetAtt [ IdentityFunc, Arn ]
Value: !Select [ 0, !Split [ "00::/", !Select [ 0, !GetAtt VPC.Ipv6CidrBlocks ] ] ]

IdentityFunc 是在 Lambda 中为“自定义变量”实现的“身份函数”,as described in this answer 。与这个链接的答案不同,我直接在同一个堆栈中实现该函数,因此更容易维护。 See here for the gist .

3。将 IPv6 默认路由添加到您的互联网网关:

RouteInternet6:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref RouteTableMain
DestinationIpv6CidrBlock: "::/0"
GatewayId: !Ref IGWPublicNet
DependsOn:
- IGWNetAttachment

IGWNetAttachment 是对堆栈中定义的 AWS::EC2::VPCGatewayAttachment 的引用。如果不等待,可能无法正确设置路由

4。将 IPv6 CIDR block 添加到您的子网:

SubnetA:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [ 0, !GetAZs { Ref: "AWS::Region" } ]
CidrBlock: 172.20.0.0/24
MapPublicIpOnLaunch: true
# The following does not work if MapPublicIpOnLaunch because of EC2 bug
## AssignIpv6AddressOnCreation: true
Ipv6CidrBlock: !Sub "${VPCipv6Prefix.Value}00::/64"
VpcId:
Ref: VPC

关于被注释掉的AssignIpv6AddressOnCreation - 这通常是您想要做的,但显然,EC2 API 中有一个错误阻止了它的工作 - 这并不是 CloudFormation 的错误。这记录在this AWS forums thread中,以及我接下来将在这里介绍的解决方案。

5。使用另一个 lambda 修复 AssignIpv6AddressOnCreation 问题:

这是 lambda 设置:

IPv6WorkaroundRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: !Sub "ipv6-fix-logs-${AWS::StackName}"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- PolicyName: !Sub "ipv6-fix-modify-${AWS::StackName}"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ec2:ModifySubnetAttribute
Resource: "*"

IPv6WorkaroundLambda:
Type: AWS::Lambda::Function
Properties:
Handler: "index.lambda_handler"
Code: #import cfnresponse below required to send respose back to CFN
ZipFile:
Fn::Sub: |
import cfnresponse
import boto3

def lambda_handler(event, context):
if event['RequestType'] is 'Delete':
cfnresponse.send(event, context, cfnresponse.SUCCESS)
return

responseValue = event['ResourceProperties']['SubnetId']
ec2 = boto3.client('ec2', region_name='${AWS::Region}')
ec2.modify_subnet_attribute(AssignIpv6AddressOnCreation={
'Value': True
},
SubnetId=responseValue)
responseData = {}
responseData['SubnetId'] = responseValue
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "CustomResourcePhysicalID")
Runtime: python2.7
Role: !GetAtt IPv6WorkaroundRole.Arn
Timeout: 30

这就是你如何使用它:

IPv6WorkaroundSubnetA:
Type: Custom::SubnetModify
Properties:
ServiceToken: !GetAtt IPv6WorkaroundLambda.Arn
SubnetId: !Ref SubnetA

此调用与自动缩放组竞争以完成设置,但它不太可能失败 - 我运行了几十次,并且在第一个实例启动之前正确设置字段从未出现过问题。

关于amazon-ec2 - 通过 AWS 和 CloudFormation 自动分配 IPv6 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42047071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com