3.3 Infrastructure Automation
3.3 Infrastructure Automation
Infrastructure
Automation
Now that we’ve covered several of Google Cloud’s services and features, it makes
sense to talk about how to automate the deployment of Google Cloud infrastructure.
Calling the cloud API from code is a powerful way to generate infrastructure. But
writing code to create infrastructure also has some challenges. One issue is that the
maintainability of the infrastructure depends directly on the quality of the software. For
example, a program could have a dozen locations that call the cloud API to create
VMs. Fixing a problem with the definition of one VM would require first identifying
which of the dozen calls actually created it. Standard software development best
practices will apply, and it’s important to note that applications undergo changes
rapidly, requiring maintenance on your code.
Agenda
01 Terraform
Lab: Automating the Deployment of Infrastructure
Using Terraform
You will use Terraform to deploy a VPC network, a firewall rule, and VM instances in
the lab of this module.
Cloud Console
console.cloud.google.com
So far, you have been creating Google Cloud resources using the Google Cloud
Console and Cloud Shell. We recommend the Google Cloud Console when you are
new to using a service or if you prefer a UI. Cloud Shell works best when you are
comfortable using a specific service and you want to quickly create resources using
the command line. Terraform takes this one step further.
Proprietary + Confidential
Terraform is one of the tools used for Infrastructure as Code or IaC. Before we dive
into understanding Terraform, let’s look at what Infrastructure as Code is. In essence,
infrastructure as code allows for the quick provisioning and removing of
infrastructures.
Several tools can be used for IaC. Google Cloud supports Terraform, where
deployments are described in a file known as a configuration. This details all the
resources that should be provisioned. Configurations can be modularized using
templates which allow the abstraction of resources into reusable components across
deployments.
In addition to Terraform, Google Cloud also provides support for other IaC tools,
including:
● Chef
● Puppet
● Ansible
● Packer
Terraform is an infrastructure
automation tool
● Repeatable deployment process
● Declarative language
● Focus on the application
● Parallel deployment
Compute Cloud Firewall Cloud VPN
● Template-driven Engine Rules
Terraform is an open source tool that lets you provision Google Cloud resources.
This deployment can be repeated over and over with consistent results, and you can
delete a whole deployment with one command or click. The benefit of a declarative
approach is that it allows you to specify what the configuration should be and let the
system figure out the steps to take.
Instead of deploying each resource separately, you specify the set of resources which
compose the application or service, allowing you to focus on the application. Unlike
Cloud Shell, Terraform will deploy resources in parallel.
Terraform uses the underlying APIs of each Google Cloud service to deploy your
resources. This enables you to deploy almost everything we have seen so far, from
instances, instance templates, and groups, to VPC networks, firewall rules, VPN
tunnels, Cloud Routers, and load balancers. For a full list of supported resource
types, a link to the Using Terraform with Google Cloud documentation page is
included in the Course Resources.
[https://cloud.google.com/docs/terraform?hl=en]
Proprietary + Confidential
Terraform language
● The configuration file guides the <BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
management of the resource. # Block body
<IDENTIFIER> = <EXPRESSION> # Argument
}
The Terraform language is the user interface to declare resources. Resources are
infrastructure objects such as Compute Engine virtual machines, storage buckets,
containers, or networks. A Terraform configuration is a complete document in the
Terraform language that tells Terraform how to manage a given collection of
infrastructure. A configuration can consist of multiple files and directories.
● Blocks that represent objects and can have zero or more labels. A block has a
body that enables you to declare arguments and nested blocks.
● Arguments are used to assign a value to a name.
● An expression represents a value that can be assigned to an identifier.
Proprietary + Confidential
disk {
image = “image to build instance”
}
}
output “instance_ip” {
value = “${google_compute.ip_address}”
}
Terraform can be used on multiple public and private clouds. Terraform is already
installed in Cloud Shell.
The example Terraform configuration file shown starts with a provider block that
indicates that Google Cloud is the provider. The region for the deployment is specified
inside the provider block.
The resource block specifies a Google Cloud Compute Engine instance, or virtual
machine. The details of the instance to be created are specified inside the resource
block.
The output block specifies an output variable for the Terraform module. In this case, a
value will be assigned to the output variable "instance_ip."
Proprietary + Confidential
Before you get into the lab, let me walk you through how Terraform can be used to set
up an auto mode network with an HTTP firewall rule.
For this example we are going to define our infrastructure in a single file, main.tf.
Let’s start with the main.tf file. The main.tf file is where we specify the infrastructure
we wish to create. It is like a blueprint for our desired state.
Next we define our network, setting the auto create subnetworks flag to true which will
automatically create a subnetwork in each region.
# [START vpc_firewall_create]
resource "google_compute_firewall" "rules" {
project = var.project_id # Replace this with your project ID in quotes
name = "my-firewall-rule"
network = "vpc_network"
allow {
protocol = "tcp"
ports = ["80", "8080"]
}
}
Next, we define our firewall. Here we are allowing TCP access to port 80 and 8080.
Terraform takes this main.tf file and uses it as the specification for what to create.
Proprietary + Confidential
terraform init
terraform plan
terraform apply
Once we have completed the main.tf file, we can deploy the defined infrastructure in
Cloud Shell.
We use the command terraform init to initialize the new Terraform configuration.
We run this command in the same folder as the main.tf file.
● The terraform init command makes sure that the Google provider plugin
is downloaded and installed in a subdirectory of the current working directory,
along with various other bookkeeping files. You will see an "Initializing provider
plugins" message.
Terraform knows that you're running from a Google project, and it is getting
Google resources.
Lab Intro
Automating the Deployment of
Infrastructure Using Terraform
Let’s apply what you’ve just learned in a hands-on lab, where you will automate the
deployment of VPC networks, firewall rules, and VM instances.
Proprietary + Confidential
VMs VMs
SubNetwork: 10.128.0.2 SubNetwork: 10.132.0.2
10.128.0.0/20 10.128.0.1 Compute Engine 10.132.0.0/20 Compute Engine
(Auto-mode) (Auto-mode)
mynet-us-vm mynet-eu-vm
You deploy an auto mode network called mynetwork with a firewall rule to allow
HTTP, SSH, RDP, and ICMP traffic. You also deploy the VM instances shown in this
network diagram.
Lab review
Automating the Deployment
of Infrastructure Using
Terraform
Philipp Maier
In this lab, you created a Terraform configuration with a module to automate the
deployment of GCP infrastructure. As your configuration changes, Terraform can
create incremental execution plans, which allows you to build your overall
configuration step by step.
The instance module allowed you to re-use the same resource configuration for
multiple resources while providing properties as input variables. You can leverage the
configuration and module that you created as a starting point for future deployments.
You can stay for a lab walkthrough, but remember that GCP's user interface can
change, so your environment might look slightly different.
Proprietary + Confidential
Google Cloud
02
Marketplace
Google Cloud Marketplace lets you quickly deploy functional software packages that
run on Google Cloud. Essentially, Cloud Marketplace offers production-grade
solutions from third-party vendors who have already created their own deployment
configurations based on Terraform. These solutions are billed together with all of your
project’s Google Cloud services. If you already have a license for a third-party service,
you might be able to use a Bring Your Own License solution.
You can deploy a software package now and scale that deployment later when your
applications require additional capacity. Google Cloud even updates the images of
these software packages to fix critical issues and vulnerabilities, but doesn't update
software that you have already deployed. You even get direct access to partner
support.
Proprietary + Confidential
Review:
Infrastructure Automation
Now, you might say that going through all the effort to deploy a network, a firewall
rule, and two VM instances doesn’t convince you to use Terraform. That’s true, if you
only need to create these resources once, and don’t foresee ever creating them
again. However, for those of us who manage several resources and need to deploy,
update, and destroy them in a repeatable way, an infrastructure automation tool like
Terraform becomes essential.