SHARE
How To Deploy Terraform LAMP Stack On The IBM Cloud

Terraform, an opensource project enables us to define infrastructure as code for better productivity, collaboration, and transparency. Terraform is an opensource tool to safely & efficiently build, change & manage the infrastructure. We can share the API configuration with the team members for editing, review, and versioning.

The Terraform infrastructure can manage from compute instances, storage, networking to DNS and more. When the change in configuration happens, Terraform can detect what changed and creates execution plans.

Here we’ll see how to configure, scale and destroy the LAMP (Linux, Apache, MySQL & PHP) stack with Terraform on the IBM Cloud for Terraform. We will need a virtual server & the object storage services of the IBM Cloud.

Step 1: Installation

First, you need to Install Terraform via its installer here.

On Windows, copy the files from the downloaded zip to C:\terraform – you’ll need to create a folder with that name.

Then use set PATH=%PATH%;C:\terraform by opening the command prompt as administrator to set the path for Terraform

Step 2: Set up IBM Cloud Provider for Terraform

Terraform works with providers, which understands the API connections & resources. So first we’ll set up the CLI for IBM Cloud provider.

  • Check Terraform installation by running terraform in your terminal or command prompt window.
terraform

You’ll need to download the required IBM Cloud Provider plugin for your system and then extract. You will see the terraform-provider-ibm binary plugin file.

On the non-Windows systems, create terraform.d/plugins folder inside the user’s home directory and to place the above-mentioned file in it. Then use the following

mkdir -p $HOME/.terraform.d/plugins
mv $HOME/Downloads/terraform-provider-ibm $HOME/.terraform.d/plugins/

On the Windows, however, the file needs to be placed in the same folder but under the “Application Data” & then run:

MD %USERPROFILE%\AppData\terraform.d\plugins
MOVE PATH_TO_UNZIPPED_PROVIDER_FILE\terraform-provider-ibm.exe  %USERPROFILE%\AppData\terraform.d\plugins

Then launch the Windows Powershell (Start + R > Powershell) and run the below command to create terraform.rc file

echo > $env:APPDATA\terraform.rc

And then use the following content for the same. You must use the absolute path (including the drive letter) with the front slashes.

# ~/.terraformrc
providers {
   ibm = "PATH_TO_YOUR_APPDATA_PLUGINS/terraform-provider-ibm.exe"
}

Step 3: Terraform configuration

IBM has provided us with a sample terraform configuration. Visit https://github.com/IBM-Cloud/LAMP-terraform-ibm & fork a copy & then clone your fork to your local environment.

git clone https://github.com/YOUR_USER_NAME/LAMP-terraform-ibm

Review the following config files:

  • yml – This contains the server installation config where you can add scripts related to your server to install. Check phpinfo(); injected into this file.
  • tf – It contains the variables related to the provider where we’ll add the provider username and API key
  • tf – It contains the server configurations to deploy the VM with specified variables.
  • tfvars – This file contains the SoftLayer username and API key, IBM Cloud API Key and your space/org names. Add these to avoid adding these credentials again & again. Note: You should not publish this file with your credentials.
  • Open the vm.tf file in an IDE of your choice and modify the file by adding your public SSH key. This will be used to access the VM created by this configuration. To copy the SSK key, use pbcopy < ~/.ssh/id_rsa.pub & paste in the above file.
  • Open the terraform.tfvars file with your IDE, modify the file by adding all the credentials listed, adding these credentials in that files means you don’t need to renter these credentials every time running terraform apply. You must add all the five credentials listed in the terraform.tfvars file

Step 4: Create a LAMP stack server

Now we’ll create a LAMP stack server from the terraform configuration sample. The config is to provision a virtual machine instance and then install: Apache, MySQL (MariaDB), and PHP

Use the following commands to initiate terraform & then apply the configuration.

cd LAMP-terraform-ibm
terraform init
terraform apply

Terraform LAMP Stack On The IBM Cloud

Then verify that the server is created as desired.

Terraform LAMP Stack On The IBM Cloud

Step 5: Add the IBM Cloud Object Storage

Now add a new IBM Cloud Object Storage service by creating a new file a new file “ibm-cloud-object-storage.tf“. Add the below code snippet to this file.

variable "org_name" {

  description = "You can get the org name under your IBM Cloud dashboard account"

}

variable "space_name" {

  description = "You can get your space name under your IBM Cloud dashboard account"

}

data "ibm_space" "space" {

  space = "${var.space_name}"

  org   = "${var.org_name}"

}

# a cloud object storage

resource "ibm_service_instance" "objectstorage" {

  name       = "lamp_objectstorage"

  space_guid = "${data.ibm_space.space.id}"

  service    = "cloud-object-storage"

  plan = "Premium"

}

You’ll need to edit “vm.tf” file to include something like this:

Increase disk size to 100GB
Increase number of CPU cores to 4 cores
Increase RAM (memory) to 4096

Then initiate & apply the Terraform config by using

terraform init
terraform apply

After this, you should see a new file “terraform.tfstate” in your directory. Don’t miss this file otherwise the terraform config is lost.

Step 6: Verify The VM

Go to “Infrastructure” to view the list of virtual servers. Then to “Devices > Device List”. Here you should see your server listed. Click on the server to see more information about the config.

Now test the server through the web browser. Enter the public IP address. You will see the server’s default page something like below.

Terraform LAMP Stack On The IBM Cloud

You should also verify the IBM Cloud Object Storage service by going to the IBM Cloud dashboard.

Conclusion

Terraform allows to easily use the same configurations over multiple places to save time & any mistakes. Thanks to IBM Cloud for being the provider for Terraform.

It is an ideal tool for building & managing infrastructure for multi-tier applications where a pool of web servers is using a database tier & makes sure it is available before the web servers are started.

You can also use it for self-service clusters, software demos, to rapidly build new environments for testing, or use with multi-cloud deployments to build & maintain the large-scale environment.

Please note: Testing a terraform configuration on your IBM Cloud may incur costs. Check the pricing calculator to understand the same. To delete after testing Terraform on IBM Cloud – you’ll need a SoftLayer admin permission. Or, you can also select “Cancel Device”.