Lesson 6 of 6
Apply, verify, and destroy the server
Run terraform apply to create everything, visit the web page to confirm it works, then run terraform destroy to remove all resources.
6 minInfrastructure
After this lesson you will be able to create the web server in the cloud, check that it serves the web page, and remove all the resources when you are done.
Fix the instance startup script
The instance AMI from lesson 3 is Amazon Linux 2, which uses yum, not apt-get. The earlier user_data script used apt-get, so Apache would not install and the web page would not load. Before applying, replace the aws_instance resource in main.tf with the version below. The rest of main.tf stays the same from lesson 5. The new script installs the Apache package named httpd on Amazon Linux.
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.web.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
echo "Hello from Terraform" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
EOF
tags = {
Name = "web-server"
}
}Run terraform fmt and terraform validate to check the file. terraform validate should report that the configuration is valid.
Apply the configuration
Before applying, run terraform plan to see what Terraform will do.
terraform planThe plan shows that the instance will be replaced because user_data changed. Terraform will destroy the old instance and create a new one. The security group already exists from lesson 4 and is not changed, so it will not be created again. When you are happy with the plan, run terraform apply.
terraform applyTerraform shows the plan again and asks you to confirm. Type yes and press Enter. Terraform then replaces the instance. This can take a minute or two. When it finishes, you see a message like this:
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
Outputs:
instance_ip = "203.0.113.15"The IP address is different for your instance. Write it down because you will use it in the next step. It may be different from the one you saw in lesson 4 because the instance was replaced.
Visit the web page
The instance runs a web server and shows a page that says Hello from Terraform. Open a web browser and go to http://203.0.113.15 but use your own IP address instead of the example one.
You should see the text Hello from Terraform in the browser. If the page does not load, wait a minute and try again. The instance needs time to start the web server after it is created. If you still cannot see the page, check that the security group allows inbound traffic on port 80 from any address, and that the user_data script now uses yum and httpd.
Inspect the state
Terraform keeps a record of the resources it manages in a file called terraform.tfstate. This file is created when you run terraform apply. It contains the current state of your infrastructure, including the instance ID and public IP address. You can view the state with the terraform show command:
terraform showThis prints a summary of all resources and their attributes. For example, you will see the security group with its ingress rules and the instance with its AMI and instance type. The state file is important because Terraform uses it to plan future changes. Never edit this file by hand. If you want to see only the outputs, use terraform output as you did in lesson 4.
Destroy the resources
When you have finished testing, remove the resources so you do not pay for them. Run this command:
terraform destroyTerraform shows what it will remove and asks for confirmation. Type yes and press Enter. Terraform deletes the instance and the security group. When it finishes, you see a message like this:
Destroy complete! Resources: 2 destroyed.Now the resources are gone. You can run terraform show to check that no resources are left. It will show an empty state.
Key takeaways
- Run
terraform applyto create resources andterraform destroyto remove them. - The
user_datascript must match the AMI operating system; Amazon Linux usesyumandhttpd. - Always check the plan before applying to avoid surprises.
- Use the output IP address to visit the web page and confirm the server works.
- Destroy resources when you are done to avoid ongoing costs.
Try it
Run terraform apply to create the resources. Then open the web page using the IP address from the output and confirm you see Hello from Terraform. Finally, run terraform destroy and check that the resources are removed. A good result is that the web page loads correctly and terraform destroy completes without errors.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.