gpt4 book ai didi

amazon-web-services - 如何使用cloudformation将弹性IP从一个EC2实例切换到另一个实例?

转载 作者:行者123 更新时间:2023-12-03 07:46:48 24 4
gpt4 key购买 nike

我正在将弹性 IP 关联到我通过 cloudformation 创建的 EC2 实例。我通过

将其附加到我的 EC2 实例
AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: <ami-id>
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-1
myEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
EIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt myEIP.AllocationId
NetworkInterfaceId: !Ref myENI
DependsOn: Instance

我正在使用aws cloudformation create-stack --stack-name ec2-example --template-body file://test-instance.yaml

创建堆栈

这会创建我的资源,包括弹性 IP。 35.82.18.137,一个实例,EIAssociation 及其自己的 ENI。

现在,我通过使用以下命令将标签值更改为 test-instance-2 来更新现有堆栈,从而启动另一个 EC2 实例 aws cloudformation create-stack --stack-name ec2-example-1 - -template-body 文件://test-instance.yaml

AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: <ami-id>
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-2
myEIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
EIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt myEIP.AllocationId
NetworkInterfaceId: !Ref myENI
DependsOn: Instance

它创建一个新实例并颁发一个新的 EIP:44.229.115.61、一个实例、EIAssociation 及其自己的 ENI。

如何在不删除旧实例的情况下将旧弹性 IP 35.82.18.137 从旧实例切换到新实例?使用 cloudformation 是否可以实现这一点(如果可能的话,无需点击操作)?

最佳答案

是的,这是可能的,如文档中所述:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/refactor-stacks.html

这是一个演示(下面的所有代码片段都在 bash 中运行)。


假设我们有这两个模板(请注意,第一个模板必须将 EIP 的删除策略设置为保留):

template1.yml:

AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-02d7ab1717c7787ce
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-1
myEIP:
Type: AWS::EC2::EIP
DeletionPolicy: Retain
Properties:
Domain: vpc
EIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt myEIP.AllocationId
NetworkInterfaceId: !Ref myENI
DependsOn: Instance

template2.yml:

AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-02d7ab1717c7787ce
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-2

然后让我们部署它们:

$ aws cloudformation deploy --template-file template1.yml --stack-name stack1
$ aws cloudformation deploy --template-file template2.yml --stack-name stack2

部署堆栈后,我们需要获取第一个堆栈中部署的弹性IP的分配ID和公共(public)IP:

$ EIP_PHYSICAL_RESOURCE_ID=$(aws cloudformation describe-stack-resources --stack-name stack1 --query 'StackResources[?LogicalResourceId == `myEIP`].PhysicalResourceId' --output text)
$ EIP_ALLOCATION_ID=$(aws ec2 describe-addresses --query "Addresses[?PublicIp == '${EIP_PHYSICAL_RESOURCE_ID}'].AllocationId" --output text)

我们现在可以继续从第一个堆栈中删除 EIP 和 EIP 关联:

template1.yml

AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-02d7ab1717c7787ce
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-1

让我们更新第一个堆栈:

$ aws cloudformation update-stack --template-body file://template1.yml --stack-name stack1

现在我们必须修改第二个模板以包含 EIP。

template2.yml:

AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-02d7ab1717c7787ce
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-2
myEIP:
Type: AWS::EC2::EIP
DeletionPolicy: Retain
Properties:
Domain: vpc

现在让我们创建以下文件 (file.txt),稍后将在导入操作中使用该文件:

[
{
"ResourceType":"AWS::EC2::EIP",
"LogicalResourceId":"myEIP",
"ResourceIdentifier": {
"AllocationId": "$EIP_ALLOCATION_ID",
"PublicIp": "$EIP_PHYSICAL_RESOURCE_ID"
}
}
]

现在让我们填充它(envsubst 是将两个变量替换到文本文件中的便捷方法,但显然您也可以通过其他方式执行此操作):

$ export EIP_PHYSICAL_RESOURCE_ID
$ export EIP_ALLOCATION_ID
$ envsubst < file.txt > resourcesToImport.txt

resourcesToImport.txt 现在包含 EIP 的分配 id 和公共(public) ip。让我们运行导入操作:

$ aws cloudformation create-change-set \
--stack-name stack2 --change-set-name ImportChangeSet \
--change-set-type IMPORT \
--resources-to-import file://resourcesToImport.txt \
--template-body file://template2.yml
$ aws cloudformation execute-change-set --change-set-name ImportChangeSet --stack-name stack2

第二个堆栈现在已导入 EIP。

最后,让我们创建与现有实例的关联

template2.yml:

AWSTemplateFormatVersion: 2010-09-09
Description:
Testing stuff

Resources:
myENI:
Type: AWS::EC2::NetworkInterface
Properties:
SubnetId: <subnet-id>
Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-02d7ab1717c7787ce
InstanceType: t2.micro
NetworkInterfaces:
- NetworkInterfaceId: !Ref myENI
DeviceIndex: '0'
Tags:
- Key: Name
Value: test-instance-2
myEIP:
Type: AWS::EC2::EIP
DeletionPolicy: Retain
Properties:
Domain: vpc
EIPAssociation:
Type: AWS::EC2::EIPAssociation
Properties:
AllocationId: !GetAtt myEIP.AllocationId
NetworkInterfaceId: !Ref myENI
DependsOn: Instance

让我们更新堆栈:

$ aws cloudformation update-stack --template-body file://template2.yml --stack-name stack2

原来与第一个堆栈中的实例关联的 EIP 现在与第二个堆栈中的实例关联

关于amazon-web-services - 如何使用cloudformation将弹性IP从一个EC2实例切换到另一个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77159055/

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