Setting up Terraform for VPC
Terraform on IBM Cloud® enables predictable and consistent provisioning of IBM Cloud services so that you can rapidly build complex, multitier cloud environments by 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 VPC 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 Terraform without setting up or maintaining the CLI 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 VPC
Before you begin, make sure that you have the required access to create and work with VPC resources.
-
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 VPC service instances and resources.
-
Create a Terraform configuration file that is named
main.tf. In this file, you add the configuration to create an IBM Cloud® Virtual Private Cloud and associated resources by using HashiCorp Configuration Language (HCL). For more information, see Provisioning infrastructure with Terraform and Terraform documentation.The following example creates a VPC, a subnet in the
us-south-1zone, an SSH key, a virtual network interface, and a virtual server instance with boot volume encryption and two network attachments. For other supported regions, see Regions and endpoints.resource "ibm_is_vpc" "example" { name = "example-vpc" } resource "ibm_is_subnet" "example" { name = "example-subnet" vpc = ibm_is_vpc.example.id zone = "us-south-1" ipv4_cidr_block = "10.240.0.0/24" } resource "ibm_is_ssh_key" "example" { name = "example-ssh" public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR" } resource "ibm_is_virtual_network_interface" "example" { name = "example-vni" allow_ip_spoofing = false enable_infrastructure_nat = true primary_ip { auto_delete = false address = "10.240.0.8" } subnet = ibm_is_subnet.example.id } resource "ibm_is_instance" "example" { name = "example-instance" image = ibm_is_image.example.id profile = "bx2-2x8" metadata_service_enabled = false boot_volume { encryption = "crn:v1:bluemix:public:kms:us-south:a/<account_id>:<kms_instance_id>:key:<key_id>" } primary_network_attachment { name = "example-primary-att" virtual_network_interface { id = ibm_is_virtual_network_interface.example.id } } network_attachments { name = "example-network-att" virtual_network_interface { name = "example-net-vni" auto_delete = true enable_infrastructure_nat = true primary_ip { auto_delete = true address = "10.240.0.6" } subnet = ibm_is_subnet.example.id } } vpc = ibm_is_vpc.example.id zone = ibm_is_subnet.example.zone keys = [ibm_is_ssh_key.example.id] //User can configure timeouts timeouts { create = "15m" update = "15m" delete = "15m" } }For more IBM VPC Terraform examples, see the Terraform Registry for IBM VPC infrastructure.
-
Initialize the Terraform CLI.
terraform init -
Create a Terraform execution plan. The Terraform execution plan summarizes all the actions that need to be run to create the Virtual Servers for VPC instance in your account.
terraform plan -
Create the Virtual Servers for VPC instance and associated resources in IBM Cloud.
terraform apply -
From the IBM Cloud resource list, select the IBM Cloud® Virtual Private Cloud instance that you created and note the instance ID.
-
Verify that the instance is running. For more information, see Managing virtual server instances.
Using IBM Terraform modules for VPC
In addition to writing individual Terraform resources, you can use IBM Cloud Terraform modules to provision VPC infrastructure. IBM Cloud Terraform modules are a collection of open source Terraform modules that help you build, update, and version complex and compliant environments in IBM Cloud. The modules are curated and actively supported by the IBM Cloud development organization and are published in the terraform-ibm-modules GitHub organization.
Rather than defining each ibm_is_* resource individually, you reference a module in a single module block and supply input variables. The module manages the underlying resources for you.
IBM Terraform modules are available for an expanding set of VPC services. The following modules are currently available:
| Service | Module |
|---|---|
| File storage | terraform-ibm-vpc-file-storage |
The file storage module supports zonal file shares with the dp2 profile only.
For service-specific usage examples, see the Terraform tab in each service's documentation.
What's next?
Now that you successfully created your first IBM Cloud VPC service instance with Terraform on IBM Cloud, you can choose between the following tasks: