我试图在cloudformation堆栈中设置我的Windows 2016实例,这需要在参数部分获取用户的一些输入才能将其传输到实例中,并且内部的软件将需要这些参数。直到现在,我只发现我可以通过将其保存在文本文件中的同时在powershell脚本中进行解析来发送它。还有其他方法吗?或更有效的方法来做到这一点?
jsjloveqx 回答:将用户/元数据/运行时数据从cloudformation解析为EC2
请参见Bootstrapping AWS CloudFormation Windows Stacks。
简而言之,您通常使用UserData
中的AWS::CloudFormation::Init元数据和cfn-init
来引导和初始化实例。
这里是一个AWS CloudFormation模板,可以从CloudFormation堆栈中获取一个参数并将该信息放到Windows实例上:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
Username:
Type: String
Description: Ask for a username,will be placed on the instance
WindowsAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: /aws/service/ami-windows-latest/Windows_Server-2019-English-Full-Base
KeyName:
Type: String
Description: Keyname for the keypair to use when launching EC2 instances
Resources:
AppInstance:
Type: AWS::EC2::Instance
Properties:
KeyName: !Ref KeyName
ImageId: !Ref WindowsAmiId
InstanceType: t2.large
UserData:
Fn::Base64: !Sub |
<powershell>
Set-Content -Path 'C:\username.txt' -Value '${Username}'
</powershell>
结果是C:\username.txt
中的文件,其中包含启动堆栈时提供的Username
参数。
或者,您可以调用describe-stack
来检索参数。