Lukasz Szczepaniak:
Hi,
I do not know how to resolve that issue? I have created <http://modules.tf|modules.tf> file with that code:
module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"
  name = "my-vpc"
  cidr = "10.0.0.0/16"
  azs             = ["us-east-1a", "us-east-1f"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]
  enable_nat_gateway = true
  enable_vpn_gateway = true
  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}
Also in <http://main.tf|main.tf> file I added aws_security_group :
resource "aws_security_group" "servers" {
  name        = "allowservers"
  description = "Allow TCP:8080 inbound traffic to servers"
  vpc_id      = module.vpc.vpc_id
  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
And assigned it to aws_instance
resource "aws_instance" "ubuntu" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t2.micro"
  vpc_security_group_ids = [aws_security_group.servers.id]
  user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p 8080 &
              EOF
  tags = {
    Name = "HelloWorld"
  }
}
When I comment that line vpc_security_group_ids = [aws_security_group.servers.id] it works, but I need it to have assigned aws_security_group which means that with uncommented that above line it throws the error:
 Error: creating EC2 Instance: VPCIdNotSpecified: No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC.
│       status code: 400, request id: 0d882d0e-a9d1-4898-91e3-77924b36629b
│
│   with aws_instance.ubuntu,
│   on <http://main.tf|main.tf> line 164, in resource "aws_instance" "ubuntu":
│  164: resource "aws_instance" "ubuntu" {
Does anyone know why and how to resolve it?