Estimated duration: 30 mins
This lab will teach how to use Terraform in IBM Cloud and provision the following Cloud Services:
- a Resource Group
- a VPC
- a Subnet
- a SSH Key
- a VSI (Virtual Server Instance)
- a Floating IP
You can then ssh into the newly created VSI.
Terraform | Estimation Duration |
---|---|
Apply | ~2 mins |
Destroy | ~3 mins |
This lab requires the following command lines:
Unless you are Administrator of the Cloud Account, you need permissions to be able to provision VPC Resources. Ask the Administrator run the Terraform in
iam
folder.
-
Clone this repository
git clone https://github.com/lionelmace/learn-ibm-terraform
-
Login to IBM Cloud
ibmcloud login
-
Create and store the value of an API KEY as environment variable
export IBMCLOUD_API_KEY=$(ibmcloud iam api-key-create my-api-key --output json | jq -r .apikey)
If the variable "ibmcloud_api_key" is set in your provider, you can initialize it using the following command export TF_VAR_ibmcloud_api_key="Your IBM Cloud API Key"
-
Terraform must initialize the provider before it can be used.
terraform init
-
Review the plan
terraform plan
-
Start provisioning.
Estimated duration: 2 mins
terraform apply
Outputs look like this:
ssh_connection_string = "ssh -i ./rsakey.pem root@VSI-FLOATING-IP" vsi_private_ip = "10.10.10.4" vsi_public_ip = "158.177.3.170"
-
Connect to the VSI
ssh -i ./rsakey.pem root@PUBLIC_IP
-
Clean up the resources to avoid cost
terraform destroy
-
Once provisioned, reads and outputs a Terraform state or plan file in a human-readable form.
terraform show
-
Terraform graph outputs the visual execution graph of Terraform resources. The graph is outputted in DOT format, which can easily be converted to an image by making use of dot provided by GraphViz:
terraform graph | dot -Tsvg > graph.svg