【问题标题】:How to launch an EFS in the default VPC?如何在默认 VPC 中启动 EFS?
【发布时间】:2020-06-24 06:45:06
【问题描述】:

我正在尝试在我的默认 VPC 中启动 EFS 文件系统。我能够创建 EFS,但无法在所有子网中安装目标。在subnet_id 我不确定如何传递默认 VPC 的所有子网 ID 的值。下面是我的 Terraform 代码:​​

$ cat ec2.tf

provider "aws" {
 [enter image description here][1]region  = "ap-south-1"
 profile = "saumikhp"
}


data "aws_vpc" "default" {
  default = true
}

data "aws_subnet_ids" "example" {
  vpc_id = var.vpc_id
}

data "aws_subnet" "example" {
  for_each = data.aws_subnet_ids.example.ids
  id       = each.value
}


resource "aws_key_pair" "key" {
  key_name   = "mykey12345"
  public_key = file("mykey12345.pub")
}

resource "aws_security_group" "web-sg" {
  name        = "web-sg"
  description = "Allow port 22 and 80"
  vpc_id      = "vpc-18819d70"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 2049
    to_port     = 2049
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  tags = {
    Name = "web-sg"
  }
}

resource "aws_instance" "myinstance" {
  ami             = "ami-0447a12f28fddb066"
  instance_type   = "t2.micro"
  key_name        = "mykey12345"
  security_groups = ["web-sg"]

  connection {
    type        = "ssh"
    user        = "ec2-user"
    private_key = file("mykey12345")
    host        = aws_instance.myinstance.public_ip
  }

  provisioner "remote-exec" {
    inline = [
      "sudo yum install httpd php git -y",
      "sudo systemctl restart httpd",
      "sudo systemctl enable httpd",
    ]
  }
  tags = {
    Name = "SaumikOS"
  }
}

resource "aws_efs_file_system" "efs" {
   creation_token = "efs"
   performance_mode = "generalPurpose"
   throughput_mode = "bursting"
   encrypted = "true"
 tags = {
     Name = "EfsExample"
   }
 }

resource "aws_efs_mount_target" "efs-mt" {
  depends_on = [
    aws_instance.myinstance,
  ]
  for_each        = data.aws_subnet_ids.example.ids
  subnet_id = each.value
  file_system_id  = "aws_efs_file_system.efs.id"
  security_groups = ["aws_security_group.web-sg.id"]
}

Error after running terraform apply

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

    您可以使用aws_vpcaws_subnet_ids data sources 的组合从默认VPC 获取子网。

    data "aws_vpc" "default" {
      default = true
    }
    
    data "aws_subnet_ids" "example" {
      vpc_id = var.vpc_id
    }
    

    然后,您可以通过循环在每个子网中创建 EFS 挂载目标(每个挂载目标只需要一个 subnet_id):

    resource "aws_efs_mount_target" "efs-mt" {
      for_each        = data.aws_subnet_ids.example.ids
      file_system_id  = aws_efs_file_system.efs.id
      subnet_id       = each.value
      security_groups = [aws_security_group.web-sg.id]
    }
    

    【讨论】:

    • 创建了一个包含 variable "vpc_id" {} 内容的 vars.tf 文件,并按照您的说明更新了 main.tf。现在我收到了这个错误。 --------- 错误:ValidationException:状态代码:400,请求 id:0e2c0f2b-1c18-434a-b133-cb2c8b34e588 在 ec2.tf 第 90 行,资源“aws_efs_mount_target”“efs-mt”:90:资源“aws_efs_mount_target”“efs-mt”{
    • 如果它仍然与最初的问题相关,您应该将其编辑到您的问题中。注释不允许格式化并且字符数有限,因此无法正确读取错误。此外,它们并不意味着无限期地保留,因此您可以添加到调试问题的任何有用的东西都应该在问题本身中。
    • 安全组是否也在默认VPC中?很难判断您何时对该 VPC ID 进行了硬编码,但随后为子网数据源的 VPC ID 使用了一个变量。我建议切换到使用数据源来获取您的 VPC ID(使用我的答案中建议的默认选择器或指定一个过滤器,该过滤器将为您提供另一个 VPC,例如通过标签)并避免传递任意 ID。跨度>
    • 如果可能,请给我完整的 terraform 代码。我是初学者,我很难找到实际的错误。非常感谢您到现在为止对我的帮助。
    • 此时你的问题是问了一堆不同的东西,其中一些非常基本。您应该考虑修改您的问题,以便它只提出一个问题,并尽可能多地显示任何错误,直接作为文本而不是图像。您可能会发现通读 stackoverflow.com/help/how-to-ask 并编辑您的问题以使其符合指南会有所帮助。
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多