About Developing Infrastructure Components Using Terraform

Use Terraform to deploy, change, and remove your resources in the cloud efficiently.

Create Infrastructure Components Using Terraform

After you set the variables in terraform.tfvars file, use the Terraform CLI to deploy the resources defined in the Terraform configuration.

Before you begin, ensure that your local machine has internet access. If your local machine is behind a firewall, use an appropriate proxy to allow Terraform to reach the internet.
  1. Go to the directory that contains the terraform.tfvars file.
  2. Initialize Terraform:
    terraform init
    The command downloads a plugin and sets up the directory for use by Terraform.
  3. Verify that the syntax of the configuration has no errors:
    terraform validate
  4. If a syntax error exists, then fix the error, and repeat the previous step.

    To debug problems, you can enable logging:

    • Configure the log level by setting the TF_LOG environment variable to TRACE, DEBUG, INFO, WARN, or ERROR. The TRACE level is the most verbose.
    • Set the log file path by using the TF_LOG_PATH environment variable.
  5. Review the resources defined in the configuration:
    terraform plan
    The output shows the details of all the actions that are performed when you apply this configuration.
  6. If you want to make any changes, edit the configuration, validate it, and review the revised plan.
  7. Create the resources:
    terraform apply
  8. At the prompt Do you want to perform these actions?, enter yes.
    As Terraform creates each resource, it displays the status of the operation.

    When all the resources are created, the message Apply complete is displayed, along with the number of resources added, changed, and destroyed.

    The ssh_to_bastion output displays the ssh command that you can use to connect to the bastion host.

    You can view the Terraform output at any time by running the command terraform output

  9. Make a note of the value of the ssh_to_bastion output. That's the command to connect to the bastion host using SSH.
You can expand this environment by creating other Oracle Cloud Infrastructure resources, such as compute instances and databases attached to private subnets.

Modify the Topology

To modify the topology, you must update the resource definitions in the appropriate Terraform configurations, and then apply the revised configuration. Identifying the resource definitions that need to be modified requires a thorough understanding of the example code, specifically the Terraform modules referenced in each directory and the inter-module dependencies.

The instructions to modify the topology are outside the scope of this solution.

Remove All the Resources

  1. On the computer where you installed Terraform, go to the directory that contains the terraform.tfvars file.
  2. Enter the following command:
    terraform destroy
    Terraform displays the details of the resources that will be destroyed, as shown in this example:
    Plan: 0 to add, 0 to change, 12 to destroy.
  3. At the prompt Do you really want to destroy all resources?, enter yes
    As Terraform removes each resource, it displays the status of the operation.
    After all the resources are removed, the message Destroy complete is displayed, along with the number of resources destroyed, as shown in the following example:
    Destroy complete! Resources: 12 destroyed.