amazon-web-services - 有没有办法使用cloudformation模板中的参数显示所有公共(public)AMI?

标签 amazon-web-services parameters aws-cloudformation

我有一个 cloudformation 模板来启动带有安全组的新实例。创建堆栈时会询问一个参数,我们只能在模板中给出一种实例类型,但我需要选择 AMI 架构(如 ubuntu、windows、Linux )。请分享您的想法或提供示例模板以实现相同的目标。 我的模板 json 如下,取自 AWS 站点以供引用(从示例复制)

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Sample Template EC2_Instance_With_Ephemeral_Drives: Example to show how to attach ephemeral drives using EC2 block device mappings. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",
"Parameters": {
    "KeyName": {
        "Description": "Name of an existing EC2 KeyPair to enable SSH access to the web server",
        "Type": "AWS::EC2::KeyPair::KeyName",
        "ConstraintDescription": "must be the name of an existing EC2 KeyPair."
    },
    "InstanceType": {
        "Description": "WebServer EC2 instance type",
        "Type": "String",
        "Default": "t2.small",
        "AllowedValues": [
            "t1.micro",
            "t2.nano",
            "t2.micro",
            "cc2.8xlarge"
        ],
        "ConstraintDescription": "must be a valid EC2 instance type."
    },
    "SSHLocation": {
        "Description": "Lockdown SSH access to the bastion host (default can be accessed from anywhere)",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "0.0.0.0/0",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "must be a valid CIDR range of the form x.x.x.x/x."
    }
},
"Mappings": {
    "AWSInstanceType2Arch": {
        "t1.micro": {
            "Arch": "PV64"
        },
        "t2.nano": {
            "Arch": "HVM64"
        },
        "t2.micro": {
            "Arch": "HVM64"
        },
        "cc2.8xlarge": {
            "Arch": "HVM64"
        }
    },
    "AWSInstanceType2NATArch": {
        "t1.micro": {
            "Arch": "NATPV64"
        },
        "t2.nano": {
            "Arch": "NATHVM64"
        },
        "t2.micro": {
            "Arch": "NATHVM64"
        },
        "cc2.8xlarge": {
            "Arch": "NATHVM64"
        }
    },
    "AWSRegionArch2AMI": {
        "us-east-1": {
            "PV64": "ami-2a69aa47",
            "HVM64": "ami-6869aa05",
            "HVMG2": "ami-3353c649"
        },
        "us-west-2": {
            "PV64": "ami-7f77b31f",
            "HVM64": "ami-7172b611",
            "HVMG2": "ami-58ce1220"
        },
        "us-west-1": {
            "PV64": "ami-a2490dc2",
            "HVM64": "ami-31490d51",
            "HVMG2": "ami-62ad9502"
        },
        "eu-west-1": {
            "PV64": "ami-4cdd453f",
            "HVM64": "ami-f9dd458a",
            "HVMG2": "ami-41bc0a38"
        },
        "eu-west-2": {
            "PV64": "NOT_SUPPORTED",
            "HVM64": "ami-886369ec",
            "HVMG2": "NOT_SUPPORTED"
        },
        "eu-west-3": {
            "PV64": "NOT_SUPPORTED",
            "HVM64": "NOT_SUPPORTED",
            "HVMG2": "NOT_SUPPORTED"
        },
        "eu-central-1": {
            "PV64": "ami-6527cf0a",
            "HVM64": "ami-ea26ce85",
            "HVMG2": "ami-b50d8fda"
        },
        "ap-northeast-1": {
            "PV64": "ami-3e42b65f",
            "HVM64": "ami-374db956",
            "HVMG2": "ami-14e45872"
        },
        "ap-northeast-2": {
            "PV64": "NOT_SUPPORTED",
            "HVM64": "ami-2b408b45",
            "HVMG2": "NOT_SUPPORTED"
        },
        "ap-southeast-1": {
            "PV64": "ami-df9e4cbc",
            "HVM64": "ami-a59b49c6",
            "HVMG2": "ami-2a80d649"
        },
        "ap-southeast-2": {
            "PV64": "ami-63351d00",
            "HVM64": "ami-dc361ebf",
            "HVMG2": "ami-02c42e60"
        },
        "ap-south-1": {
            "PV64": "NOT_SUPPORTED",
            "HVM64": "ami-ffbdd790",
            "HVMG2": "ami-f6165899"
        },
        "us-east-2": {
            "PV64": "NOT_SUPPORTED",
            "HVM64": "ami-f6035893",
            "HVMG2": "NOT_SUPPORTED"
        },
        "ca-central-1": {
            "PV64": "NOT_SUPPORTED",
            "HVM64": "ami-730ebd17",
            "HVMG2": "NOT_SUPPORTED"
        },
        "sa-east-1": {
            "PV64": "ami-1ad34676",
            "HVM64": "ami-6dd04501",
            "HVMG2": "NOT_SUPPORTED"
        },
        "cn-north-1": {
            "PV64": "ami-77559f1a",
            "HVM64": "ami-8e6aa0e3",
            "HVMG2": "NOT_SUPPORTED"
        },
        "cn-northwest-1": {
            "PV64": "ami-80707be2",
            "HVM64": "ami-cb858fa9",
            "HVMG2": "NOT_SUPPORTED"
        }
    }
},
"Resources": {
    "EC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "KeyName": {
                "Ref": "KeyName"
            },
            "InstanceType": {
                "Ref": "InstanceType"
            },
            "ImageId": {
                "Fn::FindInMap": [
                    "AWSRegionArch2AMI",
                    {
                        "Ref": "AWS::Region"
                    },
                    {
                        "Fn::FindInMap": [
                            "AWSInstanceType2Arch",
                            {
                                "Ref": "InstanceType"
                            },
                            "Arch"
                        ]
                    }
                ]
            },
            "SecurityGroups": [
                {
                    "Ref": "EC2SecurityGroup"
                }
            ],
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/sdc",
                    "VirtualName": "ephemeral0"
                }
            ]
        }
    },
    "EC2SecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupDescription": "SSH access",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "CidrIp": {
                        "Ref": "SSHLocation"
                    }
                }
            ]
        }
    }
},
"Outputs": {
    "Instance": {
        "Value": {
            "Fn::GetAtt": [
                "EC2Instance",
                "PublicDnsName"
            ]
        },
        "Description": "DNS Name of the newly created EC2 instance"
    }
}

}

最佳答案

您可以做的是创建一个参数,提供一个可供选择的列表。

下面的示例允许您在特定区域内选择 3 种操作系统类型。我对 AMI 标识符进行了硬编码,因此如果 AWS 更改 AMI ID,该标识符可能会过时。该模板使用映射从参数中指定的值和运行模板的区域中选择正确的 AMI ID。您可以轻松地将其扩展到许多不同的区域或操作系统类型。

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "osType": {
            "Description": "OS Type",
            "Type": "String",
            "AllowedValues": [
                "Server2016",
                "SUSE",
                "RHEL"
            ],
            "ConstraintDescription": "must be a prod or test"
        }
    },
    "Mappings": {
        "RegionAndInstanceTypeToAMIID": {
            "us-east-1": {
                "Server2016": "ami-8ff710e2",
                "SUSE": "ami-f5f41398",
                "RHEL": "ami-26ebbc5c"
            },
            "us-west-2": {
                "Server2016": "ami-8ff710e2",
                "SUSE": "ami-f5f41398",
                "RHEL": "ami-26ebbc5c"
            }
        }
    },
    "Resources": {
        "testInstance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": {
                    "Fn::FindInMap": [
                        "RegionAndInstanceTypeToAMIID",
                        {
                            "Ref": "AWS::Region"
                        },
                        {
                            "Ref": "osType"
                        }
                    ]
                }
            }
        }
    }
}

关于amazon-web-services - 有没有办法使用cloudformation模板中的参数显示所有公共(public)AMI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48397691/

相关文章:

amazon-web-services - 使用脚本为现有 SQS 队列创建警报

amazon-web-services - 哪个 FSx 系统附加到哪个 EC2 实例?

javascript - 传递带参数的函数

aws-cloudformation - 接管 CloudFormation 中的现有域 (HostedZone)

amazon-web-services - 错误: The provided credentials do not have permission to create the service-linked role for EC2 Spot Instances

amazon-web-services - AWS Elastic Beanstalk,运行 cronjob

MySQL 准备好的带有参数的语句不返回数据

c++ - 推送到作为函数参数传递的队列

amazon-web-services - 使用 CloudFormation 添加对 AWS websocket API 的集成响应

amazon-web-services - AWS ssm发送命令: modify timeout in CLI