Skip to content

The ec2_eip.tf File

Filename Location Group Project/Repository
ec2_eip.tf ./ec2_eip.tf infrastructure terraform

Why?

Our EC2 instances will each have a static public IPv4 address. This will allow us to SSH into the instances directly so we can provision them. Without public IP addresses we cannot route traffic from the public Internet to the instance nor from the instance to the public Internet.

In the future we'll replace these (E)IPs with a bastion host and perhaps a NAT Gateway.

Breakdown

Defining an EIP is very simple:

1
2
3
4
resource "aws_eip" "meow_1" {
  instance = aws_instance.meow_1.id
  vpc      = true
}

We use the instance attribute to inform AWS what EC2 Instance we want thr EIP to be attached to.

Finally we use the vpc attribute to create a VPC based EIP, because we're using a VPC.

And of course we create our second EIP:

1
2
3
4
resource "aws_eip" "meow_2" {
  instance = aws_instance.meow_2.id
  vpc      = true
}

The Solution

1
2
3
4
5
6
7
8
9
resource "aws_eip" "meow_1" {
  instance = aws_instance.meow_1.id
  vpc      = true
}

resource "aws_eip" "meow_2" {
  instance = aws_instance.meow_2.id
  vpc      = true
}

Terraform Documentation

Type Documentation
aws_eip Terraform AWS Provider

Committing the Code

  1. Set your working directory to the infrastructure/terraform repository
  2. Save the file as ec2_eip.tf and use git add ec2_eip.tf to add it to the Git staging area
  3. Use git commit -am 'creating a pair of ipv4 ips for our ec2 instances' to commit the file to our repository
  4. Push the code to GitLab.com: git push

Last update: August 25, 2021