【问题标题】:AWS Security Group not in VPC error with Terraform使用 Terraform 的 AWS 安全组不在 VPC 中的错误
【发布时间】:2019-02-05 20:45:56
【问题描述】:

长话短说,我想使用 Terraform

  1. 在 AWS 中创建虚拟机
  2. 将 VM 置于允许端口 80、443 和 22 的安全组中。

完成第 1 项很简单。 为了完成第 2 项,我明白:

  • 我需要先创建一个 VPC。这行得通。
  • 然后我需要在 vpc 中创建一个子网。这行得通。
  • 然后我需要创建与 VPC 关联的安全组。这行得通。
  • 然后我需要将 VPC 安全组 ID 添加到我的 aws_instance。这条线导致它失败。 vpc_security_group_ids = ["${aws_security_group.allow_ssh.id},${aws_security_group.allow_web.id}"]

我有以下 Terraform 计划:

# Provider Details
provider "aws" {
  region                  = "us-east-1"
  shared_credentials_file = "/Users/default/.aws/credentials"
  profile                 = "my-profile"
}

# Main VPC
resource "aws_vpc" "vpc_main" {
  cidr_block = "10.0.0.0/16"

  enable_dns_support   = true
  enable_dns_hostnames = true

  tags {
    Name = "Main VPC"
  }
}

resource "aws_subnet" "public" {
  vpc_id                  = "${aws_vpc.vpc_main.id}"
  cidr_block              = "10.0.0.1/16" 
  map_public_ip_on_launch = true
  tags {
    Name                  = "Public Subnet"
  }
}

resource "aws_security_group" "allow_web" {
  name        = "allow-web-traffic"
  description = "Allow all inbound/outbound traffic on 80 443"
  vpc_id      = "${aws_vpc.vpc_main.id}"

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

  ingress {
    from_port = 443
    to_port   = 443
    protocol  = "tcp"
  }

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

resource "aws_security_group" "allow_ssh" {
  name        = "allow-ssh-traffic"
  description = "Allow ssh traffic on 22"
  vpc_id      = "${aws_vpc.vpc_main.id}"

  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"]
  }
}

resource "aws_instance" "proxy_server" {
  ami           = "ami-6871a115" # RHEL 7.5 HVM SSD
  instance_type = "t2.micro"     
  key_name      = "cwood_sa"
  vpc_security_group_ids = ["${aws_security_group.allow_ssh.id},${aws_security_group.allow_web.id}"] # this breaks it
  subnet_id     = "${aws_subnet.public.id}"      
}

由此产生的错误。

* aws_instance.proxy_server: Error launching source instance: InvalidGroup.NotFound: The security group 'sg-063c2b4b4836f18aa,sg-07e562845b70bf125' does not exist in VPC 'vpc-0397460a8f633574c'
status code: 400, request id: dae8b8e8-8259-4ef1-b9c2-a8b782f96235

但如果我查看 AWS 控制台,那些安全组与 VPC 相关联。

我假设我在这里某个地方犯了一个根本性错误,需要一些帮助。

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    每个安全组都需要用引号括起来。您目前有以下线路:

    vpc_security_group_ids = ["${aws_security_groups.allow_ssh.id},${aws_security_group.allow_web.id}"]
    

    这不是 valid HCL list syntax 。将您的安全组行更新为:

    vpc_security_group_ids = ["${aws_security_groups.allow_ssh.id}","${aws_security_group.allow_web.id}"]
    

    【讨论】:

    • 感谢 micarlise。简单的修复,但我会永远独自一人。大帮助。我很感激。
    • @ColinWood np。根据我对 terraform 的经验,错误消息需要改进。并且有许多 github 问题正在努力解决这个问题;所以有时语法错误看起来像 AWS 错误;这会使它们看起来比实际更复杂。
    猜你喜欢
    • 2021-06-28
    • 2015-08-01
    • 2023-03-20
    • 2021-08-20
    • 1970-01-01
    • 2017-08-08
    • 2021-03-14
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多