0

Hello i have tf file for create my ec2 instance

resource "aws_vpc" "magazin-vpc" {
  cidr_block = 10.249.0.0/16
}

resource "aws_subnet" "magazin-subnet" {
  vpc_id            = aws_vpc.magazin-vpc.id
  cidr_block        = "10.249.2.0/28"
}

resource "aws_instance" "magazin-vm" {
  ami           = "ami-058c02d7640104f1e"
  instance_type = "t2.micro"
  private_ip             = "10.249.2.5"
  subnet_id              = aws_subnet.magazin-subnet.id
  vpc_security_group_ids = [aws_security_group.magazin-sg.id]


  credit_specification {
    cpu_credits = "unlimited"
  }
}

resource "aws_ebs_volume" "magazin-ebs" {
  availability_zone = "eu-north-1a"
  size              = 10

  tags = {
    Name = "magazin-ebs"
  }
}

resource "aws_volume_attachment" "magazin-ebs-att" {
  device_name = "/dev/sdh"
  volume_id   = aws_ebs_volume.magazin-ebs.id
  instance_id = aws_instance.magazin-vm.id
}

resource "aws_security_group" "magazin-sg" {
  name        = "magazin-sg"

  ingress {
    description = "Allow port SSH from office"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["172.16.0.0/24"]
  }

  ingress {
    description = "Allow port HTTPS"
    from_port   = 9200
    to_port     = 9200
    protocol    = "tcp"
    cidr_blocks = ["172.16.0.0/24"]
  }


  ingress {
    description = "Allow port HTTPS"
    from_port   = -1
    to_port     = -1
    protocol    = "icmp"
    cidr_blocks = ["172.16.0.0/24"]
  }



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

and when i launch terraform apply i got


│ Error: Error launching source instance: InvalidParameter: Security group sg-090289f530fb61f8d and subnet subnet-08d14b2d736d10286 belong to different networks.
│       status code: 400, request id: 953d0bb8-cf92-4d8c-9923-d911cec3b453
│
│   with aws_instance.magazin-vm,
│   on dev-aerospike.tf line 6, in resource "aws_instance" "magazin-vm":
│    6: resource "aws_instance" "magazin-vm" {
│

why this error happens? because i declarate vpc and subnet in my terraform file i'm using terraform 1.1.6 p.s the site says that the text should be longer but I don't know what else to write so I'll write that terraform is a cool thing, though I still don't know how to use it

1 Answer 1

1

You have to specify vpc_id in your aws_security_group. Without that your group will be created in a default VPC, not the one you are creating:

resource "aws_security_group" "magazin-sg" {
  name        = "magazin-sg"

  vpc_id      = aws_vpc.magazin-vpc.id

  ingress {
    description = "Allow port SSH from office"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["172.16.0.0/24"]
  }

  ingress {
    description = "Allow port HTTPS"
    from_port   = 9200
    to_port     = 9200
    protocol    = "tcp"
    cidr_blocks = ["172.16.0.0/24"]
  }


  ingress {
    description = "Allow port HTTPS"
    from_port   = -1
    to_port     = -1
    protocol    = "icmp"
    cidr_blocks = ["172.16.0.0/24"]
  }



  egress {
    description = "Allow ALL ports"
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
3
  • go this ╷ │ Error: Error launching source instance: Unsupported: The requested configuration is currently not supported. Please check the documentation for supported configurations. │ status code: 400, request id: 02bc10d5-045f-47a8-83cd-476b37850900 │ │ with aws_instance.magazin-vm, │ on dev-aerospike.tf line 6, in resource "aws_instance" "magazin-vm": │ 6: resource "aws_instance" "magazin-vm" { │ Commented Apr 19, 2022 at 8:42
  • @krutoiadmin This is a new error, related to the instance, not its security group. Thus new question should be asked.
    – Marcin
    Commented Apr 19, 2022 at 8:46
  • @krutoiadmin can you provide the full output? Probably it has something to do with your attributes for private_ip and subnet_id. I thought, only one of those parameters are possible at the same time. Commented Apr 19, 2022 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.