代码之家  ›  专栏  ›  技术社区  ›  crmepham

如何使用地形动态创建ec2实例

  •  0
  • crmepham  · 技术社区  · 4 年前

    我正在尝试使用 for_each .但我有一个错误:

    │ Error: Missing required argument
    │
    │   with aws_instance.ec2-instance,
    │   on main.tf line 76, in resource "aws_instance" "ec2-instance":
    │   76: resource "aws_instance" "ec2-instance" {
    │
    │ "instance_type": one of `instance_type,launch_template` must be specified
    

    这里是地形:

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 3.27"
        }
      }
    
      required_version = ">= 0.14.9"
    }
    
    variable "instance_name" {
      description = "Value of the Name tag for the EC2 instance"
      type        = string
      default     = "ChangedName"
    }
    
    variable "aws_region" {
      description = "AWS Region"
      type        = string
      default     = "eu-west-2"
    }
    
    variable "instance_size_small" {
      description = "Instance size small"
      type        = string
      default     = "t3.micro"
    }
    
    variable "redundant_count" {
      description         = "Default redundancy - base number of instances to create for redundant services"
      type                = number
      default             = 1
    }
    
    variable "ami" {
      description = "Ubuntu 20.04 AMI"
      type        = string
      default     = "ami-0015a39e4b7c0966f"
    }
    
    provider "aws" {
      profile = "sandbox"
      region  = var.aws_region
    }
    
    variable "environment_name" {
      description         = "Environment Name"
      type                = string
      default             = "dev"
    }
    
    variable "client_name" {
      description         = "Client Name"
      type                = string
      default             = "sandbox"
    }
    
    variable "instances" {
      description = "Map of modules names to configuration."
      type        = map
      default     = {
        testing-sandbox-dev = {
          instance_count          = 2,
          instance_type           = "t3.micro",
          environment             = "dev"
        },
        testing-sandbox-test = {
          instance_count          = 1,
          instance_type           = "t3.micro",
          environment             = "test"
        }
      }
    }
    
    resource "aws_instance" "ec2-instance" {
      ami                = var.ami
    
      for_each           = var.instances
    
      tags = {
        Name = "testing-${var.instances.index}.${var.environment_name}.${var.client_name}"
        client = var.client_name
        environment = var.environment_name
      }
    }
    

    instance_type 是在被迭代的映射中定义的键之一。那么,为什么Terraform不接收它呢?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Martin Atkins    4 年前

    你还没有定义一个 instance_type 内部的争论 resource "aws_instance" "ec2-instance" 块,这是Terraform报告错误的地方。

    你需要用一个表达式写出你想设置的每个参数,这个表达式告诉Terraform你想如何设置它。如果要设置为从 for_each 元素,然后你可以这样写:

    resource "aws_instance" "ec2-instance" {
      for_each = var.instances
    
      ami           = var.ami
      instance_type = each.value.instance_type
    
      tags = {
        Name = "testing-${var.instances.index}.${var.environment_name}.${var.client_name}"
        client = var.client_name
        environment = var.environment_name
      }
    }
    

    似乎你还有另一个问题,关于如何创造 each.value.instance_count 这个映射的每个元素的实例,但我认为,一旦成功填充,最好作为一个单独的问题 实例类型 对于每个元素的单个实例。