Question:
How to create a GCP Cloud Composer V2 instance via Terraform?

To work with Google Cloud Composer first we have to set up a Cloud Composer environment. Afterward, we can manage the Apache Airflow service that simplifies the deployment and operation of workflows in the cloud. In this tutorial, we'll walk you through the process to create a Google Cloud Composer V2 instance using Terraform. But before this we have to >Set up a Cloud Composer environment that we have already leared in our previous blog. 


What is Apache Airflow?

Apache Airflow is a popular open-source platform for workflow automation and orchestration. Google Cloud Composer takes Airflow to the next level by providing a fully managed service that allows you to create, schedule, and monitor workflows with ease. 


Prerequisites

Before you begin, make sure you have the following:


  1. Google Cloud Platform Account: You need a GCP account with a project where you'll create the Cloud Composer instance.


  1. Terraform Installed: Install Terraform on your local machine. You can download it from the Terraform website.


  1. Service Account Key: Create a service account in your GCP project and download the JSON key file. This service account will be used by Terraform to provision resources.


Step 1: Set Up Authentication

After installing the Google Cloud SDK, authenticate your account by running the following command and following the prompts:


gcloud auth login


Step 2. Initialize a New Terraform Configuration

Create a new directory for your Terraform configuration and navigate to it in your terminal.


mkdir gcp-composer-terraform

cd gcp-composer-terraform


Initialize a new Terraform configuration by creating a file named main.tf and add the following content:


provider "google" {

  credentials = file("path/to/your/service-account-key.json")

  project     = "your-gcp-project-id"

  region      = "us-central1"

}


resource "google_composer_environment" "composer" {

  name   = "my-composer-env"

  region = "us-central1"


  config {

    node_count           = 3

    node_config {

      zone     = "us-central1-a"

      machine_type = "n1-standard-2"

    }

    software_config {

      airflow_config_overrides = {

        "core" = "airflow.contrib.kubernetes.pod_runtime_config.KubePodRuntimeConfig"

      }

    }

  }

}



Replace "path/to/your/service-account-key.json" with the actual path to your downloaded service account key file, and "your-gcp-project-id" with your GCP project ID.


Step 3. Initialize and Apply Terraform Configuration

Run the following commands in your terminal to initialize and apply the Terraform configuration:


terraform init

terraform apply


Terraform will show you a plan of the resources it will create. Type yes to confirm and proceed with the resource creation.


Step 4. Accessing Cloud Composer Environment

Once Terraform completes provisioning the resources, it will output the details of the Cloud Composer environment, including the Web Server URL. You can access the Airflow Web UI using this URL.


Step 5. Clean Up

To avoid incurring charges, make sure to destroy the Terraform-managed resources when you're done:


terraform destroy


Final Thoughts

In this tutorial, you learned how to create a Google Cloud Composer V2 instance using Terraform. This approach allows you to define and manage your Cloud Composer environment as code, making it easier to reproduce and maintain your workflows. By integrating Terraform with GCP, you can automate the deployment and management of your infrastructure, leading to more efficient and scalable cloud-based workflows.


Ritu Singh

Ritu Singh

Submit
0 Answers