atsikham / terraform-gcp

Repository from Github https://github.comatsikham/terraform-gcpRepository from Github https://github.comatsikham/terraform-gcp

GCP sample project with Terraform and Ansible

This project is used to set up a simple infrastructure configuration with Google cloud Terraform provider and Ansible

Terraform provisioning

Requirements

terraform v0.12.0+
google_provider 3.0.0+

Preparation

As GCP service accounts cannot be used to create a GCP project without parents (single projects that are not attached to any organization), Terraform admin project will contain only a GCS bucket to store Terraform state.

  1. Set up the environment

    export TF_VAR_billing_account=YOUR_BILLING_ACCOUNT_ID
    export TF_ADMIN=${USER}-terraform-admin
    Note
    To see available billing accounts use gcloud beta billing accounts list command
  2. Authenticate with your personal account

    gcloud auth application-default login
  3. Set default compute zone (Frankfurt in this case)

    gcloud config set compute/zone europe-west3
  4. Create the Terraform Admin Project

    gcloud projects create ${TF_ADMIN} --set-as-default
    gcloud beta billing projects link ${TF_ADMIN} --billing-account ${TF_VAR_billing_account}
  5. Set up remote state in Cloud Storage

    gsutil mb -p ${TF_ADMIN} -l EU gs://${TF_ADMIN}
    gsutil versioning set on gs://${TF_ADMIN}
  6. Configure your environment for the Google Cloud Terraform provider

    export GOOGLE_PROJECT=${TF_ADMIN}

Apply configuration

  1. Configure ssh keys: place id_rsa.pub to terraform/files directory and specify username environment variable

    export TF_VAR_ssh_username=<username>
  2. Initialize the backend: terraform init

  3. Preview the Terraform changes: terraform plan

  4. Apply the Terraform changes: terraform apply

Clean up changes

Use following command: terraform destroy

Ansible configuration management

Terraform creates inventory hosts file with gcp-website hosts group configured

  1. Ping target VMs

    export ANSIBLE_HOST_KEY_CHECKING=False
    ansible -i ansible/hosts -m ping gcp-website
  2. Print kernel version

    ansible -i ansible/hosts -m command -a 'uname -r' gcp-website
  3. Execute playbook

    ansible-playbook -i hosts apache.yaml

Checks

  1. Set gcloud project to created by Terraform

    gcloud config set project <project-id>
  2. Get lb ip using command

    gcloud compute forwarding-rules describe website-forwarding-rule
  3. Connect to test-lb-instance using ssh

  4. Request site index.html a few times via curl

About


Languages

Language:HCL 99.4%Language:Smarty 0.6%