我在 AWS 上的 CloudFormation 服务中使用 YAML 文件。此文件使用刚刚创建的 EC2 服务器的 IP 在 CloudFormation 中创建具有公共(public) IP 和堆栈输出的 EC2 服务器。然而,输出并不是我们经常使用的。因此,我想时不时地关闭服务器,并在需要时再次打开它。问题是,每次我再次启动 EC2 服务器时,它都会更改其公共(public) IP,并且堆栈输出的 IP 不会随之更改。
我找到了一种使其静态化的方法,即使用 EC2 服务中的弹性 IP 地址。但是,在创建堆栈时选择属性时,我似乎找不到选择该 IP 地址的方法。所以我需要一些帮助。
最佳答案
您无法自己定义 IP 地址,但可以在生成后提取它。
在您的 Cloudformation 模板中,添加一个 Outputs
部分,如下所示:
Outputs:
myEc2IP: # just some identifier
Value: !GetAtt myInstance.PublicIp # assuming that "myInstance" is your resource
然后,在部署堆栈后,您可以使用 AWS CLI 提取值:
aws cloudformation describe-stacks --stack-name $YOUR_STACK \
--query 'Stacks[0].Outputs[?OutputKey==`myEc2IP`].OutputValue' \
--output text
您甚至可以通过类似的方式将其加载到 shell 变量中
export MY_ROLE_ARN="$(aws cloudformation describe-stacks …)"
了解有关输出的更多信息:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
查看 Cloudformation 中 EC2 实例的其他可能的输出/返回值:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html (在“返回值”部分)。
关于amazon-web-services - 使用 CloudFormation 设置公共(public) IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75005712/