Lab: AWS EC2 and Provisioners Q 13

We use the public IPv4 address to access this server. However, when this server is rebooted or recreated, this IP address would change.

To fix this, let’s create an Elastic IP Address.

An Elastic IP address is a static IPv4 address which does not change over time.

Create an Elastic IP resource with the following specifications:

  1. Resource Name: eip

  2. vpc: true

  3. instance: id of the EC2 instance created for resource cerberus (use a reference expression)

  4. create a local-exec provisioner for the eip resource and use it to print the attribute called public_dns to a file /root/cerberus_public_dns.txt on the iac-server.

If unsure, refer to the documentation. Documentation tab is available at the top right.

this main.tf file created

resource “aws_instance” “cerberus” {
ami = var.ami
instance_type = var.instance_type
user_data = file(“./install-nginx.sh”)

}
resource “aws_key_pair” “cerberus-key” {
key_name = “cerberus”
public_key = file(“.ssh/cerberus.pub”)
}
resource “aws_eip” “eip” {
vpc = true
instance = aws_instance.cerberus.id
provisioner “local-exec” {
command = “echo ${aws_eip.eip.public_dns} >> /root/cerberus_public_dns.txt”
}

}
variable “ami” {
default = “ami-06178cf087598769c”
}
variable “instance_type” {
default = “m5.large”

}
variable “region” {
default = “eu-west-2”
}

still it says
The public_dns for the EIP is not stored in /root/cerberus_public_dns.txt

Hi @shashwatshah101293,
Please run the terraform apply command.

Regards,