AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77587388
Accepted
Dan
Dan
Asked: 2023-12-02 02:33:42 +0800 CST2023-12-02 02:33:42 +0800 CST 2023-12-02 02:33:42 +0800 CST

Terraform 索引计数

  • 772

我是 Terraform 的新手,正在练习创建我的第一个 terraform。我遇到了一个我不确定的错误,并在网上查看相同的错误,不清楚我应该做什么。感觉这里存在一个基本的“类型”问题。有人可以强调需要做什么以及为什么。

错误:

 terraform validate
╷
│ Error: Missing resource instance key
│
│   on variables.tf line 32, in output "public_ip":
│   32:   value       = aws_instance.my_rhel9_vm.public_ip
│
│ Because aws_instance.my_rhel9_vm has "count" set, its attributes must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│     aws_instance.my_rhel9_vm[count.index]
╵
╷
│ Error: Missing resource instance key
│
│   on variables.tf line 37, in output "instance_id":
│   37:   value       = aws_instance.my_rhel9_vm.id
│
│ Because aws_instance.my_rhel9_vm has "count" set, its attributes must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│     aws_instance.my_rhel9_vm[count.index]
╵

main.tf

provider "aws" {
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_access_key
  region     = "us-east-1"
}

resource "aws_instance" "my_rhel9_vm" {
  ami           = var.ami //RHEL 9 AMI
  instance_type = var.type
  count         = var.number_of_instances
  key_name      = var.ami_key_pair_name

  tags = {
    Name = "${var.name_tag}"
  }
}

variables.tf

variable "ami" {
  type        = string
  description = "RHEL 9 AMI ID in US-East-1"
  default     = "ami-05a5f6298acdb05b6"
}

variable "type" {
  type        = string
  description = "Instance Type"
  default     = "t2.micro"
}

variable "name_tag" {
  type        = string
  description = "Name of the EC2 instance"
  default     = "My EC2 RHEL 9 instance"
}

variable "number_of_instances" {
  type        = number
  description = "Number of instances to be created"
  default     = 1
}

variable "ami_key_pair_name" {
  type        = string
  description = "SSH Key Pair"
  default     = "tomcat"
}

output "public_ip" {
  value       = aws_instance.my_rhel9_vm.public_ip
  description = "Public IP Address of EC2 instance"
}

output "instance_id" {
  value       = aws_instance.my_rhel9_vm.id
  description = "Instance ID"
}

variable "aws_access_key_id" {}
variable "aws_secret_access_key" {}

#locals {
# ami      = "ami-05a5f6298acdb05b6"
# type     = "t2.micro"
# name_tag = "My EC2 RHEL 9 instance"
#}
  • 1 1 个回答
  • 28 Views

1 个回答

  • Voted
  1. Best Answer
    user2314737
    2023-12-02T03:22:52+08:002023-12-02T03:22:52+08:00

    错误消息告诉你,由于aws_instance.my_rhel9_vm有一个count属性,所以可以有多个实例,所以你需要指定你引用的是哪个实例。基本上,Terraform 期望aws_instance.my_rhel9_vm是一个资源数组,您可以使用aws_instance.my_rhel9_vm[0]、aws_instance.my_rhel9_vm[1]等索引来引用其中的每个资源。

    您可以更改aws_instance.my_rhel9_vm为aws_instance.my_rhel9_vm[0]invariables.tf以获得有效的配置

    output "public_ip" {
      value       = aws_instance.my_rhel9_vm[0].public_ip
      description = "Public IP Address of EC2 instance"
    }
    
    output "instance_id" {
      value       = aws_instance.my_rhel9_vm[0].id
      description = "Instance ID"
    }
    

    来自Terraform 文档:

    设置后count,Terraform 会区分块本身以及与其关联的多个资源或模块实例。实例由索引号标识,从 0 开始。

    • 2

相关问题

  • 将复制活动的序列号添加到 Blob

  • Packer 动态源重复工件

  • 选择每组连续 1 的行

  • 图形 API 调用列表 subscribedSkus 状态权限不足,但已授予权限

  • 根据列值创建单独的 DF 的函数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve