``` provider "aws" { region = "eu-west-2" access_key = "**********J" secr . . .

Stanislaus Tebid:

provider "aws" {
  region = "eu-west-2"
  access_key = "**********J"
  secret_key = "××××××9"
}
resource "aws_network_interface" "webservernic" {
  subnet_id       = aws_subnet.TerraVPC-Subnet1.id
  private_ips     = ["10.0.1.50"]
  security_groups = [aws_security_group.allow_web.id]

  attachment {
    instance     = aws_instance.r1.id
    device_index = 0
  }
}

resource "aws_eip" "one" {
  vpc                       = true
  network_interface         = aws_network_interface.webservernic.id
  associate_with_private_ip = "10.0.1.50"
  depends_on = [<http://aws_internet_gateway.gw|aws_internet_gateway.gw>]
}

#create ubuntu Server
resource "aws_instance" "r1" {
  ami           = "ami-0574da719dca65348"
  instance_type = "t2.large"
  availability_zone = "us-east-1a"
  key_name = "terrakey"
  subnet_id = "subnet-0bdcb3e0cc6b9c867"
  network_interface {
      device_index = 0
      network_interface_id = aws_network_interface.webservernic.id
  }
  user_data = &lt;&lt;-EOF
              #!/bin/bash
              sudo apt update -y
              sudo apt install apache2 -y
              sudo systemctl start apache2
              sudo  bash -c 'echo goood!! &gt; /var/www/html/index.html'
              EOF
  tags = {
    Name = "FireTest-Ubuntu"
  }

}

resource "aws_vpc" "LearnVPC" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "Tera-VPC"
  }
}
#create subnet
resource "aws_subnet" "TerraVPC-Subnet1" {
  vpc_id     = aws_vpc.LearnVPC.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-east-1a"
  tags = {
    Name = "prodsubnet-teraMain"
  }
}
# create internet gateway
resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.LearnVPC.id

  tags = {
    Name = "Teragateway"
  }
}


#route table
resource "aws_route_table" "rttera" {
  vpc_id = aws_vpc.LearnVPC.id

  route {
    cidr_block = "0.0.0.0/24"
    gateway_id = aws_internet_gateway.gw.id
  }

  route {
    ipv6_cidr_block        = "::/0"
    egress_only_gateway_id = aws_egress_only_internet_gateway.gw.id
  }

  tags = {
    Name = "RouteTable-Tera"
  }
}

resource "aws_route_table_association" "asso1" {
  subnet_id      = aws_subnet.TerraVPC-Subnet1.id
  route_table_id = aws_route_table.rttera.id
}

resource "aws_security_group" "allow_web" {
  name        = "allow_web_trafic"
  description = "Allow web traffic"
  vpc_id      = aws_vpc.LearnVPC.id
  ingress {
    description      = "http trafic"
    from_port        = 80
    to_port          = 80
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
    
  }
  ingress {
    description      = "httpstrafic"
    from_port        = 443
    to_port          = 443
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
    
  }
  ingress {
    description      = "sshtrafic"
    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"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "allow_tls"
  }
}

Guys when I run terraform apply, I get the following errors: │ Error: Cycle: aws_network_interface.webservernic, aws_instance.r1

Stanislaus Tebid:
Please anyone help me know what i am missing here

Alistair Mackay:
It is telling you that you have a circular dependency - a “chicken and egg” problem.

In resource "aws_network_interface" "webservernic" you have a reference to aws_instance.r1.id which means that the AWS instance must already be created to use this reference

In resource "aws_instance" "r1" you have a reference to aws_network_interface.webservernic.id which means that the NIC must already be created to use this reference.

See the problem here?

You don’t need to explicitly create a NIC separately. It can all be done within the aws_instance resource.

Stanislaus Tebid:
Thank you!!