Create a null_resource to Use with Your Terraform Configuration

15 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Hey there, Gurus! Welcome to the lab! This lab will demonstrate how to create a `null_resource` that can be used to call a script to run against your newly created Terraform resources. This will allow you to add some functionality and flexibility to your Terraform configuration and perform useful tasks. For instance, in this lab, we will query the status of our EC2 instance and display useful information as our resources get deployed.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a `null_resource`
  1. In the terminal, once logged in to your instance, change to the Use_null_resource directory.
  2. In the Use_null_resource directory, edit the variables.tf file and add in your ami and subnet_id from the resource_ids.txt file.
  3. In the Use_null_resource directory, edit the main.tf file and add the following:
    resource "null_resource" "ec2_status" {
      provisioner "local-exec" {
        command = "./scripts/health.sh"
      }
    }
  4. Examine the health.sh script in the Use_null_resource/scripts directory to see what it does.
  5. Initialize your working directory.
  6. Deploy your resources.
Confirm Your EC2 Instance Has Been Created
  1. Check your outputs to confirm your instance was deployed.
  2. Check the lab-provided AWS console or use the AWS CLI to confirm your EC2 instance was created.

Additional Resources

You are an admin who supports one of the largest online comic book retailers in the world. You are one of a handful of admins who have been assigned to use Terraform to manage the company's cloud deployments. You are being asked to stand up 1 of many EC2 instances in your dev environment. You have created a script that you want to run against your new EC2 instance so you can see the status of the instance, along with the instance ID, the Availability Zone it is running in, and what state the health check is in while your instance is being deployed. You have your configuration and script, so you just need to add the script to your configuration. Add a resource block using a null_resource that can be used to call your script and deploy your infrastructure to complete this lab. Let’s get started!

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?