Terraform uses state to properly and correctly manage your infrastructure resources. The state is essential for comparing the current infrastructure with the desired configuration, planning changes, and applying them while leaving valid resources untouched.
- Examines the current infrastructure state during execution.
- Identifies differences between the current state and the desired state.
- Determines necessary changes and applies them without affecting valid infrastructure.
In a previous lab, you wrote some Terraform configuration using the HashiCorp Configuration Language to create a new VPC in AWS. Now that the VPC exists, we will build an EC2 instance in one of the public subnets. Since the VPC still exists, the only change that Terraform needs to address is the addition of the EC2 instance.
On your workstation, navigate to the /workstation/terraform directory. To view the applied configuration, utilize the terraform show command to view the resources created and find the IP address for your instance.
Note: If this command doesn't yield any information, you will need to redeploy your VPC infrastructure following the steps in Objective 1b. This directory should contain both a main.tf and variables.tf file.
terraform showTerraform can perform in-place updates after changes are made to the main.tf configuration file. Update your main.tf to include an EC2 instance in the public subnet:
Append the following code to main.tf:
# Terraform Data Block - To Lookup Latest Ubuntu 20.04 AMI Image
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
# Terraform Resource Block - To Build EC2 instance in Public Subnet
resource "aws_instance" "web_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
subnet_id = aws_subnet.public_subnets["public_subnet_1"].id
tags = {
Name = "Ubuntu EC2 Server"
}
}Save the configuration.
Plan and apply the changes you just made and note the output differences for additions, deletions, and in-place changes.
Run a terraform plan to see the updates that Terraform needs to make.
terraform planRun a terraform apply to execute the updates that Terraform needs to make.
terraform applyTo view the applied configuration, utilize the terraform show command to view the resources created. Look for the aws_instance.web_server, which is now present within Terraform's managed state.
terraform showAlternatively, you can run terraform state list to list all items in Terraform's managed state.
Example:
terraform state list
data.aws_ami.ubuntu
aws_instance.web_server
...
