首页 文章

AWS安全组未使用Terraform处于VPC错误状态

提问于
浏览
0

总而言之,使用Terraform,我想

  • 在AWS中创建VM

  • 将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相关联 .

我假设我在某处发生了一个基本错误,需要一些帮助 .

1 回答

  • 2

    每个安全组都需要用引号括起来 . 你现在有这条线:

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

相关问题