Skip to content

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 plan

The 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 apply

Terraform 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 show

This 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 destroy

Terraform 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 apply to create resources and terraform destroy to remove them.
  • The user_data script must match the AMI operating system; Amazon Linux uses yum and httpd.
  • 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

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Apply, verify, and destroy the server | Build a Cloud Web Server with Terraform | Rolebound