Complete the code to define a new AWS EC2 instance resource.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "[1]" }
The instance_type must be a valid AWS instance type. Here, t3.micro is a common choice for small instances.
Complete the code to add a tag to the AWS instance for identification.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" tags = { Name = "[1]" } }
Tags help identify resources. Here, MyServer is a friendly name for the instance.
Fix the error in the code to ensure immutable infrastructure by replacing the instance on change.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t3.micro" lifecycle { [1] = true } }
The create_before_destroy lifecycle rule tells Terraform to create the new instance before destroying the old one, supporting immutable infrastructure.
Fill both blanks to create a new versioned AMI and use it in the instance resource.
resource "aws_ami" "example_ami" { name = "example-ami-[1]" virtualization_type = "hvm" root_device_name = "/dev/sda1" ebs_block_device { device_name = "/dev/sda1" volume_size = 8 } } resource "aws_instance" "example" { ami = aws_ami.example_ami.[2] instance_type = "t3.micro" }
The AMI name includes a version like v1 to track changes. The instance uses the AMI's id to launch.
Fill all three blanks to define a Terraform output that shows the public IP of the new instance.
output "instance_public_ip" { value = aws_instance.example.[1] description = "[2] of the instance" sensitive = [3] }
The output shows the public_ip attribute. The description explains it is the Public IP address. The output is not sensitive, so false is used.