Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

IaC101 Workshop Google Cloud Terraform

This terraform example creates a new Google Cloud Instance using a Google's Container-Optimized OS image. Container-Optimized OS is an operating system image for Compute Engine VMs that is optimized for running Docker containers. With Container-Optimized OS, you can bring up your Docker containers on Google Cloud Platform quickly, efficiently, and securely.

This tutorial compliments the Intro CI/CD Tutorial and demonstrates how use terraform to create a new Google Cloud Instance and deploy the application using the CI/CD Tutorial Docker image. The image will be pulled from Docker hub and run on the instance created from terraform.

Create a Google Cloud Account

You will need to create a new free Google Cloud account

Create a Google Cloud Platform Project

Use these instructions to create a new Google Cloud project

Create and get Google Cloud Project Credentials

You will need to create Google Cloud Credentials in order to perform administrative actions using terraform. Go to the create service account key page. Select the default service account or create a new one, select JSON as the key type and hit Create. Save this json file in the root of terraform/google_cloud/.

Important Security Note: Rename the file to cicd_demo_gcp_creds.json in order to protect your Google Cloud credentials from being published and exposed in a public GitHub repository. You can also protect the credentials json file from being released by simply adding the credentials json filename in this project's .gitignore file. You must be very cautious with the data in this json file because if exposed anyone wih this information can hack into your account and create resources and run up charges on your Google Cloud account.

Install Terraform locally

First install Terraform locally

Setup Terraform

In a terminal run:

  • cd terraform/google_cloud/
  • terraform init this installs the Google Terraform Plugins

Now you'll have to change some values in the main.tf file. Next you'll change some values in the terraform variable values to match your information.

variable "project_name" {
  type = "string"
  default = "cicd-workshops"
}

Change the default value to the project name you created earlier

variable "docker_declaration" {
  type = "string"
  # Change the image: string to match the Docker image you want to use
  default = "spec:\n  containers:\n    - name: test-docker\n      image: 'ariv3ra/cicd-workshop'\n      stdin: false\n      tty: false\n  restartPolicy: Always\n"
}

Change the value of: image: 'ariv3ra/cicd-workshop' to the Docker image that you built and pushed to Docker hub in the CI/CD tutorial.

Terraform plan

The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.

This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state.

In a terminal run:

  • terraform plan -out=plan.txt

This will show you a nice graph that lists what terraform will create or change.

Terraform apply

The terraform apply command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.

In a terminal run:

  • terraform apply plan.txt

This executes the terraform plan and attempts to build out a new Google Compute Instance based on the terraform and the Docker image defined.

Google Compute Instance IP Address

When terraform completes building the Google assets you should see the instance's Public IP Address and should look similar to the output below

NameCheap DNS Hostname = test.punkdata.org      NOTE: The NameCheap data will only be displayed if you implemented the terraform-provider-namecheap steps )

Public IP Address = 104.196.11.156

Copy the IP Address or DNS listed and paste into a web browser with port 5000 appended to the end of the IP Address. The complete address should look like this:

https://35.237.090.42:5000

The new application should render a welcome message and an image. The application is a Docker container spawned from the CI/CD Intro tutorial Docker image we build and pushed to CircleCI

Terraform destroy

Now that we proved your Google Compute instance and Docker container work you really should run the terraform destroy command to destroy the assets that you created in this tutorial. You can also leave it up and running but be aware that there is a cost associated with any assets running in the Google Cloud Platform and you could be liable for those costs. Google give a generous $300 credit for it's free trial sign-up but you could easily eat through that if your leave assets running. It's your call but run terraform destroy to close out any running assets.

Summary

This tutorial will show you how to deploy a new server to Google Cloud Platform and a Docker container running our CI/CD Intro application using terraform. Use the resource urls to learn more about Google Cloud and Terraform.

Resource URLs: