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

查看:342
本文介绍了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.

推荐答案

如注释中所述,如果您不愿意使用首选类型的实例而又愿意旋转其他类型的实例,则可以使用<一个href ="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_instance_type_offering" rel ="nofollow noreferrer"> 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.micro s,其中> 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 实例族的可用区列表:

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天全站免登陆