IBM Cloud Docs
Setting up Terraform for IBM Cloud Load Balancer

Setting up Terraform for IBM Cloud Load Balancer

Terraform on IBM Cloud® enables predictable and consistent provisioning of IBM Cloud services so that you can rapidly build complex, multitier cloud environments following Infrastructure as Code (IaC) principles. Similar to using the IBM Cloud CLI or API and SDKs, you can automate the provisioning, update, and deletion of your load balancer instances by using HashiCorp Configuration Language (HCL).

Looking for a managed Terraform on IBM Cloud® solution? Try out IBM Cloud® Schematics. With Schematics, you can use the Terraform scripting language that you are familiar with, but you don't must worry about setting up and maintaining the Terraform command line and the IBM Cloud® Provider plug-in. Schematics also provides pre-defined Terraform templates that you can easily install from the IBM Cloud® catalog.

Installing Terraform and configuring resources for IBM Cloud Load Balancer

  1. Follow the Terraform on IBM Cloud® getting started tutorial to install the Terraform CLI and configure the IBM Cloud® Provider plug-in for Terraform. The plug-in abstracts the IBM Cloud® APIs that are used to provision, update, or delete IBM Cloud Load Balancer service instances and resources.

  2. Create a Terraform configuration file that is named main.tf. In this file, you add the configuration to create a load balancer and to assign a user an access policy in Identity and Access Management (IAM) for that instance by using HashiCorp Configuration Language (HCL). For more information, see the Terraform documentation.

    The load balancer instance in the following example is named test_lb_local_service and is enabled on port 80. The ID of the local load balancer service group is ibm_lb_service_group.test_service_group.service_group_id and the weight for the load balancer service group is 1.

    For more information, see the ibm_lb_service usage example.

    resource "ibm_lb_service" "test_lb_local_service" {
     port = 80
     enabled = true
     service_group_id = ibm_lb_service_group.test_service_group.service_group_id
     weight = 1
     health_check_type = "DNS"
     ip_address_id = ibm_compute_vm_instance.test_server.ip_address_id
     }
    
  3. Initialize the Terraform CLI.

    terraform init
    
  4. Create a Terraform execution plan. The Terraform execution plan summarizes all the actions that need to be run to create the load balancer in your account.

    terraform plan
    
  5. Create the load balancer instance and IAM access policy in IBM Cloud.

    terraform apply
    
  6. From the IBM Cloud resource list, select the load balancer instance that you created and note the instance ID.