IBM Cloud Docs
Setting up Terraform for subnets

Setting up Terraform for subnets

Terraform on IBM Cloud® enables predictable and consistent provisioning of IBM Cloud services so that you can rapidly build complex, multi-tier 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 subnet resources 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 have to 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 subnets

Before you can create an authorization by using Terraform, make sure that you have completed the following:

  • Make sure that you have the required access to create and work with subnet resources.
  • Install the Terraform CLI and configure the IBM Cloud Provider plug-in for Terraform. For more information, see the tutorial for Getting started with Terraform on IBM Cloud®. The plug-in abstracts the IBM Cloud APIs that are used to complete this task.
  • Create a Terraform configuration file that is named main.tf. In this file, you define resources by using HashiCorp Configuration Language. For more information, see the Terraform documentation.
  1. Create a Terraform configuration file that is named main.tf. In this file, you add the configuration to create a subnet resource 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 following example creates an ibm_subnet resource with a type of Portable and the private property set to true. This subnet has an IP version of 4, a capacity of 4, and is associated with a vlan_id of 1234567.

    For more information, see the ibm_subnet usage example.

       resource "ibm_subnet" "portable_subnet" {
         type       = "Portable"
         private    = true
         ip_version = 4
         capacity   = 4
         vlan_id    = 1234567
         notes      = "portable_subnet"
    
         //User can increase timeouts
         timeouts {
           create = "45m"
         }
       }
    
  2. After you finish building your configuration file, initialize the Terraform CLI. For more information, see Initializing Working Directories.

    terraform init
    
  3. Create a Terraform execution plan. The Terraform execution plan summarizes all the actions that need to be run to create the subnet resource in your account.

    terraform plan
    
  4. Create the subnet resource in IBM Cloud.

    terraform apply
    
  5. From the IBM Cloud resource list, select the subnet resource that you created and note the resource ID.

What's next?

Now that you successfully created your subnet instance with Terraform on IBM Cloud, you can visit the Subnet Terraform registry to perform additional tasks using Terraform.