EIP with several instances

I am new in Terraform. Can someone help me out. I want to create a terraform file multiple instances with EIP allocation to each instances. I am not able to understand how to do it. I am using these systax and I am getting error. Please help me out someone.

# EC2 intance details

resource "aws_instance" "ubuntu" {
  ami                         = var.ami
  instance_type               = var.instance-type
  count                       = var.aws_instance_count
  associate_public_ip_address = true
  subnet_id                   = aws_subnet.PublicSubnet.id
  vpc_security_group_ids      = [aws_security_group.EKS_SG.id]
  key_name                    = var.SSH-key
  

  # Attach block device

  ebs_block_device {
    device_name           = "/dev/sda1" 
    volume_type           = "gp2"       
    volume_size           = 20          
    delete_on_termination = true      
  }
  tags = {
    Name = "TerraformEC2-${count.index + 1}"
  }
}

# Key_pair details

resource "aws_key_pair" "tf-ec2" {
  key_name = var.SSH-key
  # public_key = file("${path.module/tf-key.pub}")
  public_key = "ssh-rsa MTwG7CwJ"

}

# Elactic_IP allocate for e Single instance

resource "aws_eip" "Tf-eif" {
  instance = aws_instance.ubuntu.id
  vpc = true
  depends_on = ["aws_instance.ubuntu"]

  tags = {
    Name = "EKS_eip"
  }
}




variable "location" {
    default = "ap-south-1"
}

variable "access_key" {
    default = ""
}

variable "secret_key" {
    default = ""
}

variable "ami" {
    default = "ami-02eb7a4783e7e9317"
}

variable "aws_instance_count" {
    type = number
    default = "2"
}

variable "SSH-key" {
    default = "tf-demo-key"
}

variable "instance-type" {
    default = "t2.micro"
}

variable "vpc-cidr" {
    default = "10.10.0.0/16"  
}

variable "Pub_subnet1-cidr" {
    default = "10.10.1.0/24"
  
}

variable "Private_subnet2-cidr" {
    default = "10.10.2.0/24"
  
}
variable "subent_az_1" {
    default =  "ap-south-1a"  
}

variable "subent_az_2" {
    default =  "ap-south-1b"  
}





# Create the vpc

resource "aws_vpc" "EKS_vpc" {
  cidr_block = "10.10.0.0/16"
  tags = {
    name = "vpc_for_EKs"
  }

}

# Create Public Subnet

resource "aws_subnet" "PublicSubnet" {
  vpc_id                  = aws_vpc.EKS_vpc.id
  cidr_block              = "10.10.1.0/24"
  availability_zone       = var.subent_az_1
  map_public_ip_on_launch = "true"
  tags = {
    name = "PublicSN"
  }
}


# Create Private Subnet

resource "aws_subnet" "PrivateSubnet" {
  vpc_id            = aws_vpc.EKS_vpc.id
  cidr_block        = "10.10.2.0/24"
  availability_zone = var.subent_az_2
  tags = {
    name = "PrivateSN"
  }

}


# Internet Gateway

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.EKS_vpc.id
  tags = {
    name = "IGT"
  }
}

# Create Route Table for Public subnet

resource "aws_route_table" "PublicSubnet" {
  vpc_id = aws_vpc.EKS_vpc.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }
}

# Route Table association with public subnet

resource "aws_route_table_association" "PublicRTassociation" {
  subnet_id      = aws_subnet.PublicSubnet.id
  route_table_id = aws_route_table.PublicSubnet.id

}

The formatting of your copy/pasted code is really hard to read. See Welcome to the forum - please reformat your message.

I personally have no experience with AWS Elastic IPs, but there are examples written right there in the documentation of the aws_eip resource, so you might want to start there.

Thanks for replying… But can u please send me something where I can get the manifest file with multiple ec2 instance attach with multiple elastic ips…

You seem to have chosen to not act on either of the points in my previous reply, so I won’t reply further.