Note:

Install and Configure Pulumi Infrastructure as Code tool on Oracle Cloud Infrastructure

Introduction

Let us simplify your Oracle Cloud infrastructure (OCI) management with the Pulumi Infrastructure as Code (IaC) tool. This tutorial will discuss how to install and set up Pulumi, ensuring it seamlessly fits into your workflow. Whether you are experienced or getting started with IaC, this tutorial has step-by-step instructions. Get ready to upgrade your infrastructure management with Pulumi on OCI.

Once Pulumi is running on your OCI set up, you will see a noticeable improvement in efficiency. The straightforward interface makes resource deployment and management easy and simplifies your OCI infrastructure management.

Objective

Prerequisites

Task 1: Download and Install Pulumi

Download and install Pulumi on your system or virtual machine (VM). For more information, see Download & install Pulumi.

For example, macOS users can install Pulumi by running the following command.

brew install pulumi/tap/pulumi

Task 2: Install the OCI Provider Package for Pulumi

OCI providers act as the connection between Pulumi code and the OCI services you aim to oversee. OCI providers enable Pulumi to create, manage, and configure resources within your OCI environment through code.

To install the OCI providers for Pulumi, go to the Command Line Interface (CLI) and run the installation command. For this tutorial, we will use Python as the Pulumi language for deploying OCI services.

python3 -m pip install pulumi_oci

To install OCI providers using other languages, see OCI provider for Pulumi.

Task 3: Configure Environment Variables

Set up the necessary environment variables to connect Pulumi with your OCI environment. These variables include your tenancy Oracle Cloud Identifier (OCID), user OCID, fingerprint, region, and the path to your OCI API key.

Modify the values as per your tenancy config file and run the following commands.

export TF_VAR_tenancy_ocid="ocid1.tenancy.oc1..<unique_ID>"

export TF_VAR_user_ocid="ocid1.user.oc1..<unique_ID>"

export TF_VAR_fingerprint="<key_fingerprint>"

export TF_VAR_region="us-ashburn-1"

export TF_VAR_private_key_path="/path/to/oci_api_key.pem"

Task 4: Create a Pulumi Stack

Organize your project by creating a directory for your Pulumi stack. Initialize it with your preferred programming language. For this tutorial, we are using Python stack.

  1. Create a Pulumi stack directory. To create a new directory for your Pulumi stack, run the following command.

    mkdir OCI_Pulumi_Stack && cd OCI_Pulumi_Stack
    
  2. Initialize the Pulumi project project with an OCI Python template: pulumi new oci-python.

  3. Enter the project details like project name, description, and stack name (for example, dev, staging, prod). This step starts the set up process, including adding necessary dependencies and loading sample code into the main.py file.

  4. Verify the code set up by checking the contents of the OCI_Pulumi_Stack directory. This is where you will find the sample code and any additional files created during the initialization process.

Task 5: Customize a Pulumi Stack

Customize a stack according to your specific requirements. Open the main deployment file main.py and add the necessary code to define the OCI resources you want to deploy. In this tutorial, we will show you how to retrieve the availability domains within your OCI tenancy.

"""A OCI Python Pulumi program"""

import pulumi

import pulumi_oci as oci

test_availability_domains = oci.identity.get_availability_domains(compartment_id="ocid1.tenancy.oc1..xxxxx")

# Output availability domains

pulumi.export("availability_domain_1", test_availability_domains.availability_domains[0].name)

pulumi.export("availability_domain_2", test_availability_domains.availability_domains[1].name)

pulumi.export("availability_domains_3", test_availability_domains.availability_domains[2].name)

This code demonstrates how to fetch the availability domains from your OCI tenancy and export their names as output. When you execute this code using Pulumi, it will interact with OCI to retrieve the availability domains and display their names as output. This allows you to ensure that your code accurately retrieves the necessary information from OCI.

Task 6: Deploy the Pulumi Code to OCI

Run the following command to deploy resources to OCI.

pulumi up

Task 7: Verify the Command Output

After deployment, command will run the Pulumi program and retrieve the availability domains within your OCI tenancy, as defined in the code.

Pulumi_OCI_Output

Acknowledgments

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.