简体   繁体   中英

AWS Security Group not in VPC error with Terraform

To make a long story short, using Terraform, I want to

  1. Create a VM in AWS
  2. Have the VM in a security group that allows port 80, 443 and 22.

Accomplishing item 1 was straightforward. To accomplish item 2 I understood that:

  • I needed to create a VPC first. This worked.
  • I then needed a su.net inside the vpc. This worked.
  • I then needed to create the security groups associated with the VPC. This worked.
  • I then needed to add the VPC security group ids to my aws_instance. THIS LINE CAUSES IT TO FAIL. vpc_security_group_ids = ["${aws_security_group.allow_ssh.id},${aws_security_group.allow_web.id}"]

I have the following Terraform plan:

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

The resulting error.

* 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

But if I look in the AWS Console, those security groups are associated with the VPC.

I'm assuming I'm making a fundamental error here somewhere and need some help.

Each security group needs to be surrounded by quotes. You currently have the line:

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

this is not valid HCL list syntax . Update your security group line to:

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

Had a similar issue. I was having this error:

aws_instance.mac: Creating...
╷
│ Error: creating EC2 Instance: InvalidGroup.NotFound: The security group '["sg-0762f148621bc8649"]' does not exist in VPC 'vpc-0cgh1e611ae423ccf'
│       status code: 400, request id: 153ea6fb-5467-41f5-ba75-1a6f60tg8279
│ 
│   with aws_instance.mac,
│   on main.tf line 1, in resource "aws_instance" "mac":
│    1: resource "aws_instance" "mac" {
│ 
╵
Releasing state lock. This may take a few moments...
ERRO[0049] 1 error occurred:
        * exit status 1

I had the following in my setup :

module file

resource "aws_instance" "mac" {


  ami                    = var.custom_ami
  instance_type          = "mac1.metal"
  key_name               = var.key_name
  availability_zone      = var.availability_zone
  host_id                = var.dedicated_host_id
  subnet_id              = var.subnet_id
  vpc_security_group_ids = [var.vpc_security_group_ids]

  root_block_device {
    volume_size = 512
    volume_type = "gp3"
  }

  tags = {
    Name = var.name
  }
}

variables file

variable "custom_ami" {}

variable "name" {}

variable "availability_zone" {}

variable "dedicated_host_id" {}

variable "key_name" {}

variable "subnet_id" {}

variable "vpc_security_group_ids" {}

resource file

include {
  path = find_in_parent_folders()
}

locals {
  common_vars = read_terragrunt_config(find_in_parent_folders("common.hcl"))
  environment = local.common_vars.locals.environment_name
  tags        = local.common_vars.locals.common_tags
}

dependency "vpc" {
  config_path = "../../vpc/"
}

dependency "key_pair" {
  config_path = "../../key_pair/"
}

dependency "security_group" {
  config_path = "../../security-groups/macos/"
}

dependency "ec2_host" {
  config_path = "../ec2-host/"
}

terraform {
  source = "../../../../../../modules/aws/m1-macos///"
}

inputs = {

  name = "${local.environment}-macos-m1-01"

  custom_ami             = "ami-0db9238c33c33525b"
  key_name               = dependency.key_pair.outputs.key_pair_key_name
  availability_zone      = "eu-central-1a"
  dedicated_host_id      = dependency.ec2_host.outputs.ec2_host_id
  vpc_security_group_ids = [dependency.security_group.outputs.security_group_id]
  subnet_id              = dependency.vpc.outputs.private_subnets[0]

  tags = local.tags
}

How I fixed it :

All I had to do was to remove the [] around the vpc_security_group_ids = [var.vpc_security_group_ids] in the module file , and then added a type = list(string) to the variable vpc_security_group_ids in the variables file .

module file

resource "aws_instance" "mac" {
  ami                    = var.custom_ami
  instance_type          = "mac1.metal"
  key_name               = var.key_name
  availability_zone      = var.availability_zone
  host_id                = var.dedicated_host_id
  subnet_id              = var.subnet_id
  vpc_security_group_ids = var.vpc_security_group_ids

  root_block_device {
    volume_size = 512
    volume_type = "gp3"
  }

  tags = {
    Name = var.name
  }
}

variables file

variable "custom_ami" {}

variable "name" {}

variable "availability_zone" {}

variable "dedicated_host_id" {}

variable "key_name" {}

variable "subnet_id" {}

variable "vpc_security_group_ids" {
  description = "A list of security group IDs to associate with"
  type        = list(string)
  default     = null
}

resource file

include {

path = find_in_parent_folders()

}

locals {
  common_vars = read_terragrunt_config(find_in_parent_folders("common.hcl"))
  environment = local.common_vars.locals.environment_name
  tags        = local.common_vars.locals.common_tags
}

dependency "vpc" {
  config_path = "../../vpc/"
}

dependency "key_pair" {
  config_path = "../../key_pair/"
}

dependency "security_group" {
  config_path = "../../security-groups/macos/"
}

dependency "ec2_host" {
  config_path = "../ec2-host/"
}

terraform {
  source = "../../../../../../modules/aws/m1-macos///"
}

inputs = {

  name = "${local.environment}-macos-m1-01"

  custom_ami             = "ami-0db9238c33c33525b"
  key_name               = dependency.key_pair.outputs.key_pair_key_name
  availability_zone      = "eu-central-1a"
  dedicated_host_id      = dependency.ec2_host.outputs.ec2_host_id
  vpc_security_group_ids = [dependency.security_group.outputs.security_group_id]
  subnet_id              = dependency.vpc.outputs.private_subnets[0]

  tags = local.tags
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM