subcollection | copyright | lastupdated | lasttested | ||
---|---|---|---|---|---|
solution-tutorials |
|
2019-06-25 |
2019-06-25 |
{:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
{: #infrastructure-as-code-terraform}
Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
In this tutorial, you will use a sample configuration to provision a Linux virtual server, with Apache web server, MySQL, and PHP server termed as LAMP stack. You will then update the configuration to add the {{site.data.keyword.cos_full_notm}} service and scale the resources to tune the environment (memory, CPU, and disk size). Finish by deleting all of the resources created by the configuration.
{: #objectives}
- Configure Terraform and the {{site.data.keyword.Bluemix_notm}} Provider for Terraform.
- Use Terraform to create, update, scale and finally destroy a LAMP stack configuration.
{: #services}
This tutorial uses the following runtimes and services:
This tutorial may incur costs. Use the Pricing Calculator to generate a cost estimate based on your projected usage.
{: #architecture}
- A set of Terraform files are created to describe the LAMP stack configuration.
terraform
is invoked from the configuration directory.terraform
calls the {{site.data.keyword.cloud_notm}} API to provision the resources.
{: #prereqs}
Contact your Infrastructure master user to get the following permissions:
- Network (to add Public and Private Network Uplink)
- API Key
{: #prereq}
Install Terraform and the {{site.data.keyword.Bluemix_notm}} provider. See, Automating cloud resource provisioning with Terraform Getting started tutorial.
Take special notice of the Configuring the IBM Cloud Provider plug-in. The values for the following are required for this tutorial.
softlayer_username = "<infrastructure_username>"
softlayer_api_key = "<infrastructure_apikey>"
ibmcloud_api_key = "<platform_api_key>"
Note the values for the SoftLayer username and api key, and the {{site.data.keyword.Bluemix_notm}} API Key. These will be used in the next section.
{: #terraformconfig}
In this section, you will learn the basics of a terraform configuration by using a sample Terraform configuration provided by {{site.data.keyword.Bluemix_notm}}.
- Clone the sample configuration:
{:pre}
git clone https://github.com/IBM-Cloud/LAMP-terraform-ibm
- Inspect the configuration files
- install.yml - contains server installation configurations. Use this file to perform software installation or to create files on the server.
- provider.tf - contains the configuration of the provider such as the credentials to access {{site.data.keyword.Bluemix_notm}}.
- vm.tf - contains the server configurations to deploy the VM with specified variables.
- terraform.tfvars.template - variables that terraform will prompt you to enter.
- Copy the
terraform.tfvars.template
file toterraform.tfvars
. Open theterraform.tfvars
with your IDE, modify the file by adding the values mentioned in the previous section. Any variable defaults in the.tf
files can be specified in this file.
Note on SSH key - The public key from a private/public key pair is required to create a virtual machine instance. If you already have a key pair it is likely in this file: ~/.ssh/id_rsa.pub
(the private key does not have the .pub file extension and should not be used). SSH Keys will help you create the pair if needed.
{: #Createserver} In this section, you will learn how to create a LAMP stack server from the terraform configuration sample. The configuration is used to provision a virtual machine instance and install Apache, MySQL (MariaDB), and PHP onto that instance.
- Navigate to the folder of the repo you cloned.
{: pre}
cd LAMP-terraform-ibm
- Initialize the terraform configuration. This will also install
terraform-provider-ibm_VERSION
plugin.{: pre}terraform init
- Apply the terraform configuration. This will create the resources defined in the configuration.
{: pre} You should see an output similar to below.
terraform apply
- Next, head over to your infrastructure device list to verify that the server was created.
Note: after running the terraform apply command successfully, you should see a new terraform.tfstate
file added to your directory. This file contains the full deployment configuration and is used by terraform as a baseline to compare against future modifications to your configuration. If this file is removed or lost then you will lose your terraform deployment configurations.
{: tip}
{: #modify}
In this section, you are going to look at how to scale the virtual server resource and add an {{site.data.keyword.cos_full_notm}} service to your infrastructure environment.
-
Add the following lines to the
terraform.tfvars
file to increase the number of cores and memory and save the file:cores = 2 memory = 2048
{: codeblock}
-
Next, add a new {{site.data.keyword.cos_full_notm}} service. To do that create a new file and name it ibm-cloud-object-storage.tf. Add the code snippet below to the newly created file. This code defines a resource group and an instance of {{site.data.keyword.cos_full_notm}} named
lamp_objectstorage
. It uses thestandard
paid plan for the service. You can also use thelite
plan, but note that thelite
plan is limited to only one service instance per account.# a cloud object storage variable "resource_group" { default = "default" description = "resource group" } data "ibm_resource_group" "group" { name = "${var.resource_group}" } resource "ibm_resource_instance" "objectstorage" { name = "lamp_objectstorage" service = "cloud-object-storage" plan = "standard" location = "global" resource_group_id = "${data.ibm_resource_group.group.id}" parameters = { "HMAC" = true } }
{: codeblock} Note: we will later look for
lamp_objectstorage
in the logs to make sure {{site.data.keyword.cos_full_notm}} created successfully. -
Apply the terraform changes by running:
terraform apply
{: pre}
{: #verifyvm}
In this section, you are going to verify the VM and {{site.data.keyword.cos_short}} to make sure it has been created successfully.
Verify VM
- On the left side menu, click on Infrastructure to view the list of virtual server devices.
- Click Devices -> Device List to find the server created. You should see your server device listed.
- Click on the server to view more information on the server configuration. Looking at the screenshot below, we can see that the server is successfully created.
- Next, let's test the server in the web browser. Open the server public IP address in the web browser. You should see the server default installation page like below.
Verify {{site.data.keyword.cos_full_notm}}
-
From the Resource list of the {{site.data.keyword.Bluemix_notm}} Dashboard, you should see an instance of the {{site.data.keyword.cos_full_notm}} service been created for you and ready for use.
More info on {{site.data.keyword.cos_full_notm}} can be found here.
{: #deleteresources}
Delete resources using the following command:
terraform destroy
{: pre}
Note: To delete resources, you would need Infrastructure admin permissions. If you don't have an admin superuser account, then please request to cancel the resources using the infrastructure dashboard. You can request to cancel a device from the infrastructure dashboard under the devices.