Every developer should automate their infrastructure—here’s why.
Imagine you want to start writing an application on Kubernetes. You go to your favorite cloud, spin up a cluster, choose all the config options you need. Over the next few weeks, you iteratively develop your application and add more pieces to the puzzle, like third-party service integrations and a database. After a while, you decide it’s time to move this into production.
You know the best approach is to create a new production environment and keep “dev” separate. Although your code has changed drastically, its history is neatly kept in GitHub—no issues there. But what about your Kubernetes cluster, attached databases, service integrations, and volume claims? Are you certain that you can recreate the state of your dev environment’s infrastructure?
Here’s where Infrastructure as Code (IaC) comes in—it allows you to codify your cloud environments so you can automate provisioning of your infrastructure. To learn more about IaC, check out my video below, and if you want to get hands-on with, keep scrolling down.
IBM Cloud Schematics (Terraform-as-a-Service)
IBM Cloud Schematics is IBM’s cloud automation tool. The best thing about it? It’s based entirely on open source Terraform! See the blog post "IBM Cloud Schematics: Enabling Infrastructure as Code" for more info on the offering.
Today, let’s deploy a simple Kubernetes cluster on IBM Cloud using Schematics.
Navigate to the Schematics Dashboard and click Create a workspace.
Choose a name for your workspace, and input the GitHub repository URL: https://github.com/svennam92/ibmcloud-terraform
Note: You could also fork this if you want your own repo.
When you hit Retrieve input variables, it’ll pull the Terraform files from GitHub and you’ll see a list of configurable variables. As you input these variables, let’s break down the
main.tf Terraform file that Schematics is using to automate the infrastructure.
Using the IBM Cloud provider for Terraform
The first thing in the
main.tf Terraform file is the provider setting for using IBM Cloud:
As part of this, you’ll need to configure the API key that Terraform will use to authenticate with IBM Cloud. Create an API key using the API Keys dashboard and copy it into your Schematics workspace creation.
Next, you’ll need to configure the Kubernetes cluster resource you’re creating. The following is the basic required resource config:
Defaults are available for most fields, but you will need to manually configure the VLANs.
Note: If you’ve never created a cluster before, these VLANs are created for you, and you can simply input
Once completed, it should look something like this:
To customize most of these fields, you may want help from the CLI. Download the IBM Cloud CLI here.
Finally, hit Create to provision the Schematics workspace.
Navigating IBM Cloud Schematics
Schematics allows you to fully manage your Terraform-based infrastructure automation. This includes making changes to the configuration, getting the latest Terraform config from GitHub, tracking provisioned resources, and finding resources to get started. Joining our Slack is a great way to have your questions answered or raise any issues:
With Terraform, there’s a “planning” stage before you start provisioning your infrastructure. This lets you ensure all the knobs are in the right position before launching. To do so, hit Generate Plan:
Verify the logs:
Once you’ve verified the infrastructure that will be provisioned, hit Apply Plan. It generally takes around 20 minutes while your cluster is being provisioned and set-up:
That’s it! What’s next?
Once completed, you can access your Kubernetes cluster directly from your Schematics workspace or by navigating to your IBM Cloud Kubernetes Dashboard. You’ve successfully “codified” and automated the creation of a simple Kubernetes cluster.