0% found this document useful (0 votes)
914 views

Terraform GCP

The document discusses Terraform and infrastructure as code (IAC) concepts. It provides an overview of traditional infrastructure provisioning approaches and challenges. It then introduces Terraform as a tool for defining and provisioning infrastructure using code. Key Terraform concepts like init, plan, apply, variables and multiple providers are explained.

Uploaded by

Laurentiu Grama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
914 views

Terraform GCP

The document discusses Terraform and infrastructure as code (IAC) concepts. It provides an overview of traditional infrastructure provisioning approaches and challenges. It then introduces Terraform as a tool for defining and provisioning infrastructure using code. Key Terraform concepts like init, plan, apply, variables and multiple providers are explained.

Uploaded by

Laurentiu Grama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

TERRAFORM

Google Cloud

© ANKIT MISTRY – TERRAFORM


Terraform Using
Google Cloud Platform

© ANKIT MISTRY – TERRAFORM


Course Introduction

© ANKIT MISTRY – TERRAFORM


Udemy Tips

© ANKIT MISTRY – TERRAFORM


IAC, Terraform &
Installation

© ANKIT MISTRY – TERRAFORM


Traditional IT
 How Application Dev Lifecycle works
 Business create requirement
 BA – Convert Requirement into Technical detail
 Cloud Architect/ Tech lead : Infrastructure design
 If more hardware require – contact procurement
 Buying new hardware in Datacenter may take weeks to months
 Infrastructure team – Provision hardware
 Dev Team start working on Application
 This flow has very slow App Deployment
 Expensive, Scaling is issue
 Lots of different team involved will lead to error.
 Need to overcome above issue – Public Cloud Provider is the solution : AWS, GCP
 In Cloud, Resource provisioning is very fast. From month to weeks
 Public cloud provider will manage everything for you.
© ANKIT MISTRY – TERRAFORM
Interaction with Cloud
 Cloud Console/Portal
 Compared to Traditional Flow this is better
 With few clicks, can provision VM in Cloud
 But good enough if managing limited resource

 With Programmatic way – API


 Python, java, Shell
 Different Team write different script for resource provisioning
 There is no unique approach
 Different Organization try to solve same problem, but different way
 There comes Some common unifying approach, language, tool for infrastructure creation inside Cloud
 Docker, Puppet, Ansible, terraform, packer

© ANKIT MISTRY – TERRAFORM


IAC
 Resource provisioning using Code
resource "google_compute_instance"
 Create Shell/Python script for creating VM "first-instance"{
name = "hello-1"
 But writing/maintaining such code is tedious task zone = "us-central1-a"
machine_type = "n1-standard-1"
boot_disk {
initialize_params {
Create N/W
image = "debian-
Wait for above step to finish
cloud/debian-9"
Provision Subnet
}
Create Firewall rule
}
Wait for above step to finish
network_interface {
Compute engine instance with all parameter
network = "default"
}
}

© ANKIT MISTRY – TERRAFORM


Terraform
 Terraform is the one of the most popular tool for Infrastructure provisioning
 Free – Open source
 Developed by HashiCorp
 Quick & easy to get started with single binary file
 Master HCL – terraform in short span of time
 Terraform has multiple provider are available.
 Apart from Public cloud, lots pf different other provider are available for
network, DNS, Firewall, database
 Write configuration in HCL/JSON.
 HCL is preferred.
 Terraform is agentless tool
 It is not configuration tool. Work well with Ansible.
© ANKIT MISTRY – TERRAFORM
Terraform is idempotent
Python/Shell
script
To create VM
run 3 times
3 resource will be created
It will cost 3 times.

Terraform To create VM
HCL script
run 3 times
Only 1 resource will be created
It will not cost 3 times.
© ANKIT MISTRY – TERRAFORM
Native tool
 Cloud Native tool available for infrastructure provisioning
 Azure – Template
 Google – Deployment manager
 AWS - Cloud Formation
 JSON/Yaml
 Terraform is cloud agnostic.
 With Multiple provider, resource can be provisioned for multiple cloud.

© ANKIT MISTRY – TERRAFORM


Terraform Installation
 Available for all major OS:
 Visit : https://www.terraform.io/downloads.html
 Download Binary
 Unzip it.
 Export Path variable
 Windows – will see
 export PATH=$PATH:path to terraform binary
 verify with terraform version
 Editor – Free to use any of your favorite editor
 Let’s see in action

© ANKIT MISTRY – TERRAFORM


Terraform Basics - I

© ANKIT MISTRY – TERRAFORM


Terraform Workflow

Scope Author Initialize Plan Apply


• Identify the • Write the configuration • Install the plugins • Preview the changes • Make the planned
infrastructure for your for your infrastructure. Terraform needs to Terraform will make to changes.
project. manage the match your
infrastructure. configuration.

© ANKIT MISTRY – TERRAFORM


Scope & Author
 Identify what resource need to provision
 Create Local File – sample.txt with some content .tf Extension
 Write configuration file for it in HCL language
Resource Resource
Type - Local Name
Block

Arguments

© ANKIT MISTRY – TERRAFORM


Create first Terraform
File

© ANKIT MISTRY – TERRAFORM


Terraform
init, Plan & apply

© ANKIT MISTRY – TERRAFORM


init, Plan & apply
 init
 first command after writing configuration files
 initialize a working directory
 Download plugin
 local_file
 random

 plan
 Creates execution plan
 Doesn’t change any infrastructure
 apply
 execute all changes & provision resource specified in configuration files

© ANKIT MISTRY – TERRAFORM


local_file argument

© ANKIT MISTRY – TERRAFORM


Multiple Resource
resource local_file cat_res {
Main.tf
filename = "cat.txt"
content = "I love cat"
} cat.tf

resource local_file dog_res {


filename = “dog.txt"
content = "I love dogs"
} dog.tf

© ANKIT MISTRY – TERRAFORM


Random Provider
 The "random" provider allows the use of randomness within Terraform configurations.
 This is a logical provider, which means that it works entirely within Terraform’s logic, and doesn't
interact with any other services.
 Let’s see in action

© ANKIT MISTRY – TERRAFORM


Variables
Main.tf variables.tf
resource local_file sample_res {
variable filename {
filename = “sample.txt"
type = string
content = "I love Terraform"
default = "sample.txt"
}
}

variable content {
type = string
resource local_file sample_res { default = "I Love Terraform"
filename = var.filename }
content = var.content
}

© ANKIT MISTRY – TERRAFORM


Types of variables
 string – “cat”
 number – 234, 6.5
 bool - true/false
 list – sequence of value
 list(string) =>[“red”, “green”, “blue”]
 Tuple – group non homogeneous data type
 tuple([string, number, bool]) => [“dog”, 23, true]
 map – like key value : Dictionary
 {name = “Ankit", age = 32}
 set – only unique values
 object – complex data type

© ANKIT MISTRY – TERRAFORM


Use Variables
variable filename {
type = string var.filename
default = "sample.txt"
}

variable filename {} Terraform apply will ask

terraform apply -var "filename=sample.txt"

export TF_VAR_filename=sample.txt"

© ANKIT MISTRY – TERRAFORM


Variable Definition File
 terraform.tfvars
 terraform.tfvars.json
 *.auto.tfvars
 *.auto.tfvars.json

© ANKIT MISTRY – TERRAFORM


Which Variable will load first
1. export TF_VAR_filename=sample.txt“
2. terraform.tfvars file
3. variable.auto.tfvars file
4. terraform apply -var "filename=sample.txt”

© ANKIT MISTRY – TERRAFORM


Multiple Provider
main.tf

resource "local_file" "rand_res" {


filename = “sample.txt"
content = "I love terraform”
}

resource "random_string" "rand_name" {


length = 20
}

© ANKIT MISTRY – TERRAFORM


Implicit Dependency
resource "local_file" "rand_res" {
filename = "implicit.txt"
content = "I love terraform "
}

resource "random_string" "rand_name" {


length = 20
}

resource "local_file" "rand_res" {


filename = "implicit.txt"
content = "I love random text ${random_string.rand_name.id}"
}

© ANKIT MISTRY – TERRAFORM


Explicit Dependency
resource "local_file" "rand_res" {
filename = “explicit.txt"
content = "I love terraform "
}

resource "random_string" "rand_name" {


length = 20
}

resource "local_file" "rand_res" {


filename = "implicit.txt"
content = "I love random text ${random_string.rand_name.id}“
depend_on = [random_string.rand_name]
}

© ANKIT MISTRY – TERRAFORM


Output
resource "random_string" "rand_name" {
length = 20
}

output name {
value = random_string.rand_name.id
}

output.tf

© ANKIT MISTRY – TERRAFORM


Lifecycle Rules
 lifecycle – resource attributes
 create_before_destroy - Create the resource first and then destroy older
 prevent_destroy - Prevents destroy of a resource
 ignore_changes - Ignore Changes to Resource – Specific tag or all

© ANKIT MISTRY – TERRAFORM


Provider version
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "2.1.0"
}
}
}

provider "local" {
# Configuration options
}

https://www.terraform.io/docs/language/expressions/version-constraints.html

© ANKIT MISTRY – TERRAFORM


Data source
 local_file reads a file from the local filesystem.

data "local_file" "foo" {


filename = "sample1.txt"
}

output name1 {
value = data.local_file.foo.content
}

© ANKIT MISTRY – TERRAFORM


Terraform + GCP

© ANKIT MISTRY – TERRAFORM


Setup GCP Project
SERVICE ACCOUNT

© ANKIT MISTRY – TERRAFORM


Google Provider
 Terraform has multiple provider to intract with different public cloud
 Infrastructure provision inside GCP from Terraform
 https://registry.terraform.io/providers/hashicorp/google/latest/docs

terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.84.0"
}
}
}
provider "google" {
project, region, zone
}
© ANKIT MISTRY – TERRAFORM
Connect with GCP
 Google Provider Configuration
 Projectid, zone, region

 Multiple ways to authenticate with GCP


1. Username/Password - gcloud auth application-default login
2. Google Cloud VM
3. Service Account – Keys : Preferred in Production
 Create Google Cloud Storage Bucket.

resource "google_storage_bucket" "gcs1"{


name = "bucketname"
}

© ANKIT MISTRY – TERRAFORM


Approach to Provision resource
1. What this resource do
2. Cloud Console resource provisioning
3. Terraform script with minimum attributes (all required)
4. Add more arguments

© ANKIT MISTRY – TERRAFORM


Google Cloud Storage
 Object storage solution in GCP
 Unstructured Data storage
 Image
 Video
 Binary File, etc…
 Cloud storage can be used for long term archival storage
 Can be access object over http, Rest API
 Let’s see in action

© ANKIT MISTRY – TERRAFORM


GCS + Terraform

© ANKIT MISTRY – TERRAFORM


Google Cloud Network
 No Network – No Cloud
 To create any resource, Network is must
 VPC – Virtual Private Network
 VPC contains subnets – Logical grouping of IP in single region
 3 Types of VPC
 Default VPC
 Auto Mode
 Custom Mode

 Let’s see in action –


 How to create VPC
 Create Subnet
 Create firewall Policy

© ANKIT MISTRY – TERRAFORM


Network + Terraform

© ANKIT MISTRY – TERRAFORM


GCE + Terraform

© ANKIT MISTRY – TERRAFORM


Cloud Run + Terraform

© ANKIT MISTRY – TERRAFORM


Cloud Function + Terraform

© ANKIT MISTRY – TERRAFORM


BigQuery + Terraform

© ANKIT MISTRY – TERRAFORM


PubSub + Terraform

© ANKIT MISTRY – TERRAFORM


Spanner + Terraform

© ANKIT MISTRY – TERRAFORM


Cloud SQL + Terraform

© ANKIT MISTRY – TERRAFORM


BigTable + Terraform

© ANKIT MISTRY – TERRAFORM


MemoryStore + Terraform

© ANKIT MISTRY – TERRAFORM


THANK YOU

© ANKIT MISTRY – TERRAFORM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy