Terraform:如何仅在支持请求的实例类型的区域中请求 AWS EC2 实例? [英] Terraform: How to request AWS EC2 instances only in zones where the requested instance type is supported?

查看:27
本文介绍了Terraform:如何仅在支持请求的实例类型的区域中请求 AWS EC2 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在请求实例之前获取实例类型(例如 t3.medium)可用的可用区?我正在尝试运行 以下代码 并且对于某些区域它失败并出现以下错误:

Is there a way to get the availability zones where an instance type (e.g. t3.medium) is available before requesting the instance? I'm trying to run the following code and for certain regions it fails with the following error:

Error: Error launching source instance: Unsupported: Your requested instance type (t3.micro) is not supported in your requested Availability Zone (us-east-1e). Please retry your request by not specifying an Availability Zone or choosing us-east-1a, us-east-1b, us-east-1c, us-east-1d, us-east-1f.

显然我可以手动将可用区指定为受支持的区域之一,但我想尽量减少硬编码可用区.

Obviously I can manually specify the availability zone to one of the supported, but I want to minimize hardcoding availability zones.

推荐答案

正如评论中提到的,如果您愿意在首选类型不可用的情况下启动不同类型的实例,那么您可以使用 aws_ec2_instance_type_offering数据源改为回退到受影响可用区中的 t2 实例系列.

As mentioned in the comments, if you're happy to spin up a different type of instance if the preferred type isn't available then you can use the aws_ec2_instance_type_offering data source to instead fallback to the t2 instance family in the affected availability zone.

以下 Terraform 代码将输出可用区域的地图到允许的实例类型,首选 t3.micro 但回退到 t2.micros,其中 >t3 系列不可用:

The following Terraform code will output a map of availability zones to the instance type allowed, preferring t3.micro but falling back to t2.micros where the t3 family isn't available:

provider "aws" {
  region = "us-east-1"
}

data "aws_availability_zones" "all" {}

data "aws_ec2_instance_type_offering" "example" {
  for_each = toset(data.aws_availability_zones.all.names)

  filter {
    name   = "instance-type"
    values = ["t2.micro", "t3.micro"]
  }

  filter {
    name   = "location"
    values = [each.value]
  }

  location_type = "availability-zone"

  preferred_instance_types = ["t3.micro", "t2.micro"]
}

output "foo" {
  value = { for az, details in data.aws_ec2_instance_type_offering.example : az => details.instance_type }
}

这个输出:

foo = {
  "us-east-1a" = "t3.micro"
  "us-east-1b" = "t3.micro"
  "us-east-1c" = "t3.micro"
  "us-east-1d" = "t3.micro"
  "us-east-1e" = "t2.micro"
  "us-east-1f" = "t3.micro"
}

您应该能够迭代可用区以设置 aws_instance 资源的实例类型,而不仅仅是输出此内容.

Instead of just outputting this you should be able to iterate over the availability zones to set the instance type for the aws_instance resource.

或者,您可以通过将输出更改为以下内容来过滤输出,以将其减少为可以提供 t3 实例系列的 AZ 列表:

Alternatively you could filter the output to reduce it to just a list of the AZs that can offer the t3 instance family by changing the output to the following:

output "foo" {
  value = keys({ for az, details in data.aws_ec2_instance_type_offering.example : az => details.instance_type if details.instance_type == "t3.micro" })
}

这会输出以下内容,跳过不包含 t3 实例系列的可用区:

This outputs the following, skipping the availability zone that doesn't include the t3 instance family:

foo = [
  "us-east-1a",
  "us-east-1b",
  "us-east-1c",
  "us-east-1d",
  "us-east-1f",
]

这篇关于Terraform:如何仅在支持请求的实例类型的区域中请求 AWS EC2 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆