The latest cloud infrastructure announcements, technical solutions, and enterprise cloud insights.

Automating Infrastructure Provisioning and Deployment of a Three-Tier App on Oracle Cloud Infrastructure with Terraform and Chef

I am Upendra Vellanki, Principal Technologist, Platform Technology Solutions group of Oracle Product development.

Oracle Cloud Infrastructure combines the elasticity and agility of public cloud with the granular control, security, and predictability of on-premises infrastructure to deliver high-performance, high availability and cost-effective infrastructure services.

Because cloud infrastructure encompasses so many areas of IT and cloud engineering, it's essential to have cloud infrastructure automation tools that can help cloud infrastructure engineers, IT professionals, and sysadmins in nearly every area of the field. This post describes several cloud infrastructure automation tools, both open source and enterprise, that perform tasks from automatically provisioning required infrastructure to deploying a three-tier application with a single click (command).

In this post, a three-tier application is used to show how an application can be deployed with chef cookbooks and how a data source can be configured dynamically using the cloud-init feature. Three tier applications are the most common type of application and are a good example to show a mix of infrastructure deployment, software deployment, and database connectivity.

The process outlined here that uses these automation tools consists of the following high-level tasks:

  1. Create a MySQL private image with required database schema.
  2. Provision the required number of VM instances on Oracle Cloud Infrastructure by using Terraform.
  3. Deploy a web application by using cloud-init and Chef cookbooks.
  4. Access the web app, which internally calls the MySQL database and displays dynamic content.


  • Install Terraform.
  • Install a Chef cluster and create the required cookbooks at the organizational central Chef server. The java, tomcat, and webapp cookbooks are used in this example.


  • Terraform provisions one instance with MySQL and two instances with Tomcat.
  • A custom image is used to create the MySQL instance.
  • The Terraform script has the user data attribute. The cloud-init script is passed in a YAML format to the user data, and this script installs Tomcat by using Chef cookbooks.

Step 1: Create a MySQL Private Image with Required Database Schema

  1. Log in to the Oracle Cloud Infrastructure Console.
  2. Create a CentOS VM instance. For instructions, see Launching an Instance.
  3. Install the MySQL database by using these instructions.
  4. Create the custom image by using the Create Custom Image option from the VM. For detailed instructions, see Managing Custom Images.

Step 2: Provision the Required Number of VM Instances by Using Terraform

  1. Ensure that your Oracle Cloud Infrastructure credentials are either set as environment variables or provided in the tfvars file in the Terraform scripts.
  2. Create the required script files to create network, storage, instances, and so on. Sample script files are provided for reference, and you can download the files here.

    The following sample Terraform script creates MySQL and Tomcat instances:

  3. Before you run the scripts, ensure that the image ID for creating the MySQL database instance is your custom image. It you want this image to be available in another tenant, copy the image across tenants by following these instructions.
  4. To validate your scripts, run the terraform plan command before running the terraform apply command.

    The following plan output shows that nine resources will be added and one will be updated.

  5. Run the terraform apply command.

    The script displays the public IP address of the MySQL instance. This IP address is dynamically added to create a data source in Tomcat.

    You can see the MySQL instance and two instances of Tomcat in the Oracle Cloud Infrastructure Console.

Step 3: Deploy a Web Application by Using cloud-init and Chef Cookbooks

The user data attribute that’s part of the Terraform scripts runs Chef cookbooks to install the Tomcat server and the sample application.

The following script performs these actions:

  • Installs the Tomcat server by using the Chef cookbook, and deploys the sample application, which is customized as part of the tomcat cookbook. This cookbook customization can be modified to deploy a WAR or EAR file, for any enterprise applications. These cookbooks are downloaded from the Chef server, which is configured in the cloud-init user data file.
  • Downloads the mysql-java driver, and copies the JAR file to the Tomcat server.
  • Creates a MySQL data source by capturing the IP address of the MySQL VM instance dynamically, using the metadata service.

Step 4: Access the Application

  1. From the Oracle Cloud Infrastructure Console, note the public IP address of the Tomcat app instance.

  2. Open a browser and access the application by using the following URL:


    A Hello World page is displayed. This page is static (no database interaction).

  3. Now enter the following URL:


    A product catalogue page is displayed. This page has dynamic content that is fetched from the MySQL database.


This post has shown how to provision the required compute instances and network related resources; and install the required software like tomcat, MySql database. It also covered how a data source can be dynamically configured using cloud-init.

Further scope

  • The MySQL data source in Tomcat can have more dynamic properties, such as credentials, number of database connections, and so on.
  • WAR or EAR files can be deployed for complex enterprises applications.

Join the discussion

Comments ( 3 )
  • Damith Kothalawala Wednesday, June 27, 2018
    Thank you for the nice write-up. Would you be able to share sample code on github ?

    Thank you in Advance
  • Christian Nguyen Tuesday, December 15, 2020
    Super helpful tutorial! Very detailed and easy to walkthrough.
  • Christian Nguyen Tuesday, December 15, 2020
    Super helpful! Very detailed and easy to walkthrough.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha