• Gavin In The Cloud
  • Posts
  • Automating App Engine Deployment for a Hello World App Using Terraform and GitHub Actions

Automating App Engine Deployment for a Hello World App Using Terraform and GitHub Actions

Effortless Cloud Deployment: Streamlining 'Hello World' App Launch on Google App Engine with Terraform and GitHub Actions

Automating App Engine Deployment for a Hello World App Using Terraform and GitHub Actions

Introduction: As a cloud engineer experienced in implementing solutions on GCP, you're likely no stranger to the importance of automation in cloud workflows. In this blog post, we will guide you through the process of automating the deployment of a basic "Hello World" application on Google App Engine using Terraform and GitHub Actions. This detailed tutorial will cover every step from setting up the necessary resources to configuring the CI/CD pipeline, ensuring a seamless and efficient deployment process.

Prerequisites: Before we dive into the technical details, ensure you have the following prerequisites in place:

  1. A Google Cloud Platform (GCP) account with sufficient permissions to create resources such as Google App Engine services.

  2. A GitHub account with a repository set up to manage your Terraform code and GitHub Actions workflows.

Project Structure: To maintain a well-organized project structure, follow this directory layout within your GitHub repository: GitHub-Repo

You can simply clone my public repository: GitHub-Repo

Terraform Configuration: Now let's explore the components of your Terraform configuration that will provision the Google App Engine service, storage bucket, and related resources.

main.tf: The main.tf file defines the Terraform configuration for creating a Google App Engine service. It specifies the version, runtime, entry point, deployment details, and other configurations for your app.

resource "google_app_engine_application" "app" {
  project     = "project-id" // Replace with your project ID
  location_id = "us-central1" // Replace with your desired region
}

resource "google_app_engine_standard_app_version" "myapp_v1" {
  version_id = "v1"
  service    = "myapp"
  runtime    = "nodejs10"

  entrypoint {
    shell = "node ./app.js"
  }

  deployment {
    zip {
      source_url = "https://storage.googleapis.com/${google_storage_bucket.bucket.name}/${google_storage_bucket_object.object.name}"
    }
  }

  env_variables = {
    port = "8080"
  }
 

  automatic_scaling {
    max_concurrent_requests = 10
    min_idle_instances = 1
    max_idle_instances = 3
    min_pending_latency = "1s"
    max_pending_latency = "5s"
    standard_scheduler_settings {
      target_cpu_utilization = 0.5
      target_throughput_utilization = 0.75
      min_instances = 2
      max_instances = 10
    }
  }

  delete_service_on_destroy = true
  service_account = "your-service-account"
}


resource "google_storage_bucket" "bucket" {
  name     = "appengine-github"
  location = "US"
}

resource "google_storage_bucket_object" "object" {
  name   = "hello-world.zip"
  bucket = google_storage_bucket.bucket.name
  source = "./hello-world.zip"
}

provider.tf: The provider.tf file configures the Terraform provider for Google Cloud. It specifies the necessary details such as the required provider version, project ID, region, and zone.

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "4.58.0"
    }
  }
  backend "gcs" {
    bucket  = "your-backend-bucket" // Replace with your backend bucket name
    prefix  = "terraform/state"
  }
}

provider "google" {
  project = "project-id" // Replace with your project ID
  region  = "us-central1" // Replace with your desired region
  zone    = "us-central1-c" // Replace with your desired zone
}

hello-world.zip: This archive contains your "Hello World" app files - main.py and app.yaml.

  1. main.py: This file holds the code for your "Hello World" web app. It uses Flask to create a simple web server that says "Hello World!" when you visit it.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello World!"

if __name__ == '__main__':
    app.run()
  1. app.yaml: This file configures how your app should run on Google App Engine. It specifies the runtime and how to handle incoming requests.

runtime: python37

service: default

handlers:
- url: /.*
  script: auto

GitHub Actions Configuration: Your GitHub Actions workflow configuration, defined in .github/workflows/terraform.yml, is responsible for automating the Terraform workflow and deploying your app to App Engine.

name: "Deploy to App Engine"

on:
  push:
    branches:
      - main

jobs:
  terraform:
    name: "Terraform"
    runs-on: ubuntu-latest
    env:
      GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
    defaults:
      run:
        working-directory: terraform
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1
        with:
          terraform_version: 1.0.1
          terraform_wrapper: false

      - name: Terraform Init
        id: init
        run: terraform init

      - name: Terraform Format
        id: fmt
        run: terraform fmt 
        
      - name: Terraform Plan
        id: plan
        run: terraform plan 

      - name: Terraform Apply
        id: apply
        run: terraform apply -auto-approve

        #- name: Terraform Destroy
        #id: destroy
        #run: terraform destroy -auto-approve

Implementation Steps: Here's a step-by-step guide to implementing the deployment process:

  1. Set up GitHub Repository: Create a new repository on GitHub or utilize an existing one to host your Terraform code. You can also clone this repository if needed: GitHub-Repo

  2. Configure GCP Provider: In the provider.tf file, set up the GCP provider by specifying details such as the backend bucket, project ID, region, and zone.

  3. Create Google Cloud Service Account: Generate a Google Cloud service account and grant it the necessary permissions to manage App Engine and storage resources. Remember to download the service account key file.

  4. Configure GitHub Secrets: Navigate to your GitHub repository's Settings > Secrets. Add a new secret named GOOGLE_CREDENTIALS and paste the contents of the service account key file into the value field. This step ensures secure authentication for GitHub Actions.

Note: Make sure to remove any white spaces in your token content before pasting it.

  1. Commit and Push Code: Commit and push your Terraform code to the GitHub repository. When you push changes to the main branch, GitHub Actions will automatically trigger the defined workflow.

  2. Automated Workflow: The workflow you've set up will initiate Terraform, perform a plan of the changes, and then apply those changes to your GCP project. This process deploys your "Hello World" app to Google App Engine seamlessly.

Conclusion: In this comprehensive blog post, you've successfully learned how to automate the deployment of a "Hello World" app on Google App Engine using Terraform and GitHub Actions. By following the provided steps and configurations, you can streamline your deployment process, reduce manual intervention, and ensure consistent and reliable results. As a cloud engineer with experience in AWS and GCP, this approach aligns with your expertise in implementing solutions on cloud platforms. Happy automating!

References: