【问题标题】:Terraform is asking for VPC ID even though its implied in the subnetTerraform 要求提供 VPC ID,即使它隐含在子网中
【发布时间】:2023-01-29 23:30:19
【问题描述】:

我有以下简单的 EC2 创建地形脚本:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }

  required_version = ">= 0.14.9"
}

provider "aws" {
  profile = "default"
  region  = "us-east-1" # virginia
}


resource "aws_network_interface" "network" {
  subnet_id       = "subnet-0*******"
  security_groups = ["sg-******"]

  attachment {
    instance     = aws_instance.general_instance.id
    device_index = 0
  }
}

resource "aws_instance" "general_instance" {
  ami           = "ami-00874d747dde814fa" # unbutu server
  instance_type = "m5.2xlarge"
  key_name      = "my-key"
  root_block_device {
    delete_on_termination = true
    volume_size           = 500
    tags                  = { Name = "Root Volume" }
  }
  # user_data = file("startup.sh") # file directive can install stuff
  tags = {
    Name = "General"
  }
}

我得到以下信息:

Error: Error launching source instance: VPCIdNotSpecified: No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC.

我觉得这很奇怪,因为经典的流程是创建一个 VPC,创建一个子网,然后创建一个网络接口。但是,我有一个我想使用的 VPC,它与我正在使用的子网相关联。所以我想知道如果我将它与我请求的子网相关联,它会要求一个 VPC id。

提前致谢

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    我已经想通了。

    resource "aws_instance" "general_instance" {
      ami           = "ami-00874d747dde814fa" # unbutu server
      instance_type = "m5.2xlarge"
      key_name      = "EC2-foundry"
    
      network_interface {
        network_interface_id = aws_network_interface.network.id
        device_index         = 0
      }
    
      root_block_device {
        delete_on_termination = true
        volume_size           = 500
        tags                  = { Name = "Foundry Root Volume" }
      }
      # user_data = file("startup.sh") # file directive can install stuff
      tags = {
        Name = "Foundry General"
      }
    }
    

    网络接口必须附加在 aws 资源中

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 2020-05-06
      • 2022-11-14
      • 2019-02-04
      相关资源
      最近更新 更多