You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform获取云及基础设施中的可用镜像列表?

Great question! Let's break this down into two clear parts to cover exactly what you're asking:

1. Retrieving Available Cloud Images with Terraform

Terraform doesn't have a universal command to pull image lists, but it uses data sources to fetch dynamic information directly from your cloud provider. Each cloud platform has its own dedicated data source for images—here are practical examples for the major providers:

Example for AWS (using aws_ami data source)

This snippet fetches the latest Ubuntu 22.04 AMI for x86_64 architecture:

data "aws_ami" "latest_ubuntu" {
  most_recent = true # Grab the newest matching image
  owners      = ["099720109477"] # Canonical's official AWS account ID

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  }

  filter {
    name   = "architecture"
    values = ["x86_64"]
  }
}

# Output key details of the retrieved image
output "ubuntu_ami_details" {
  value = {
    id         = data.aws_ami.latest_ubuntu.id
    name       = data.aws_ami.latest_ubuntu.name
    created_at = data.aws_ami.latest_ubuntu.creation_date
  }
}

After running terraform init and terraform apply, you'll see the AMI details in the output. To get a list of multiple images, adjust your filters or use for_each to iterate over different criteria (e.g., different OS versions).

Example for Azure (using azurerm_image data source)

For Azure, you can pull images from a custom gallery or the Azure Marketplace:

data "azurerm_image" "windows_server_2022" {
  name                = "WindowsServer-2022-Datacenter"
  resource_group_name = "my-production-resources"
}

output "windows_image_id" {
  value = data.azurerm_image.windows_server_2022.id
}

Example for GCP (using google_compute_image data source)

For Google Cloud, fetch official OS images like this:

data "google_compute_image" "debian_11" {
  family  = "debian-11"
  project = "debian-cloud"
}

output "debian_image_self_link" {
  value = data.google_compute_image.debian_11.self_link
}
2. Is There a Command Like terraform show providers images?

Short answer: No, Terraform doesn't include a built-in command like terraform show providers images to directly browse available images from your cloud provider.

Terraform's core commands are focused on managing your infrastructure state, planning changes, and applying configurations—not exploring raw cloud provider resources. For direct image browsing, your best options are:

  • Use your cloud provider's native CLI tools:
    • AWS: aws ec2 describe-images --filters "Name=name,Values=ubuntu*"
    • Azure: az image list --publisher Canonical --offer 0001-com-ubuntu-server-jammy --sku 22_04-lts
    • GCP: gcloud compute images list --filter="family:debian-11"
  • Combine Terraform data sources with terraform plan (to preview results before applying) or terraform output (to view retrieved images after applying your configuration).

内容的提问来源于stack exchange,提问作者alexsnemos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:07:47