amazon-web-services - 如何在CloudFormation中指定AMI?

标签 amazon-web-services amazon-ec2 aws-cloudformation

我的任务是基本的 - 我想使用 CloudFormation 来设置一个带有我喜欢的任意 AMI 的 EC2 实例。然而,由于某种原因,这并不像只输入我想要的图像的 ID 号那么简单。

正如我们在他们的 tutorial CloudFormation template 中看到的那样,对于 ImageId 部分,他们有:

Properties:
    ImageId: !FindInMap [ AWSRegionArch2AMI, !Ref 'AWS::Region' , !FindInMap [ AWSInstanceType2Arch, !Ref InstanceType, Arch ] ]

在可用的 EC2 镜像中搜索时,我没有看到任何名为“AWSRegionArch2AMI”的内容,因此我不知道为什么此关键字能够指定其所需的 AMI 并允许此模板运行。

例如,假设我想让我的 CloudFormation 模板使用这个流行的、公开可用的 AMI 创建一个实例:

Red Hat Enterprise Linux 8 (HVM), SSD Volume Type - ami-08949fb6466dd2cf3

我应该在 ImageId 字段中指定什么才能让 CloudFormation 使用此 AMI?

此外,为什么使用这些复杂的编程参数而不只是指示我想要的图像的 UID 是标准做法?

最佳答案

简短回答:没有必要经历这个查找过程。如果您计划使用单个 AMI 在单个区域中运行,那么将该 AMI 指定为参数并直接使用它是完全合理的。

但是,AWS 示例必须能够在多个区域中运行,因为 AWS 拥有使用这些不同区域的客户。为此,它定义了映射(在 example templateMappings 部分中,可用于从实例类型转换为 AMI。


编辑:这是我当前使用的模板的摘录,该模板将 AMI 作为参数传递(默认)。它采用 JSON 格式,省略了很多内容,但应该可以让您了解正在发生的事情的要点:

"Parameters" : {
    "AmiId" : {
        "Description" : "The ID of the AMI to use for the EC2 instance",
        "Default": "ami-059f4aad319ff1bc3",
        "Type": "String"
    }


"Resources" : {

    "EC2Instance" : {
        "Type" : "AWS::EC2::Instance",
        "Properties" : {
            "ImageId" : { "Ref" : "AmiId" },

关于amazon-web-services - 如何在CloudFormation中指定AMI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56280999/

相关文章:

amazon-web-services - 用于阻止非 MFA 用户的 SCP 正在阻止其他 AWS 操作

python - aws cli dynamodb(ValidationException)错误

amazon-web-services - AWS 暂存到生产

amazon-web-services - 部署cloudformation模板时指定S3存储桶

amazon-web-services - EC2 上的 'stopped' 实例是否需要付费?

aws-lambda - AWS Lambda/SAM : How to get invocation URI from CLI?

amazon-web-services - 在 ECS 容器的 aws-cdk 中串联 AWS Secret

linux - EC2 上的 SSH 连接问题

docker - 如何从 ECS 存储库运行 Docker 镜像?

amazon-web-services - 如何调试由 CircleCI 编排的 Serverless 生成的 CloudFormation 模板