Iaroslav Kozlov Русский

Inside the infrastructure Infrastructure on GCP

Infrastructure on GCP

Infrastructure as code: Terraform compares a configuration with the state of what it manages, plans the changes and applies them through the provider’s API; Terragrunt cuts the configuration into units and runs them in the order of their dependencies.

My work

IAM, service accounts, networking, MySQL, ProxySQL and ScyllaDB on GCP are in Terraform and Terragrunt, including the installation of MySQL and ProxySQL.

How it is built

An example of the mechanism, not the configuration of the real project: a generalised layout from the documentation of Terraform, Terragrunt and the Google provider, with three units and one state bucket. How the units were really cut, and how MySQL and ProxySQL were installed on the machines, are not shown.

data control My part

After an error part way through an apply

  • Terraform logs the error, writes to the state what has already changed, unlocks the state and exits.
  • It does not roll anything back: what was created before the error stays, and the infrastructure may be in an invalid state.
  • Once the cause is resolved, the configuration is applied again to bring the infrastructure to the desired state.
  • If the run was cut off and its lock stayed, terraform force-unlock removes it; the documentation says to use it only on one’s own lock, where the automatic unlocking failed. 8 10

Components

Press a component to read what it does.

Terragrunt run --all

Runs a command over many units at once, in the order of their dependencies.

Network unit VPC, subnets

A directory with a terragrunt.hcl: the networking. Its outputs, the identifiers of what it created, are read by the units that depend on it.

IAM unit roles, accounts

A unit of IAM: service accounts, and the roles granted to them and to people on the project.

Database unit MySQL, ScyllaDB

A unit of the machines for the databases and ProxySQL. It depends on the network and on IAM, and runs after them.

Terraform plan, apply

Run in each unit: plans the changes that bring what is real to what is written, and applies them.

State GCS bucket

What Terraform knows of the resources it manages: it maps them to the configuration. Here kept in a Cloud Storage bucket, a state per unit, with a lock.

Google Cloud APIs

What is real: the resources of the project, created and changed through Google Cloud’s APIs by the provider.

  1. Units

    A unit is a directory with a terragrunt.hcl: the smallest piece of infrastructure deployed on its own. One command over many units runs them in the order of their dependencies.

    In detail

    A unit in Terragrunt is a directory containing a terragrunt.hcl file, the smallest deployable entity; units are designed to be contained and operated on independently, and their changes are meant to be atomic. include merges another configuration file into a unit’s, for example a root.hcl that every unit shares. run --all runs the command against all the units of the current stack, through a queue built from their dependencies: for plan and apply without -destroy a dependency runs before the units that depend on it; for destroy, plan -destroy and apply -destroy the order is reversed. 1 2314

    Where it breaks With --queue-ignore-errors the run goes on past a unit that failed: the documentation warns that this can leave the state inconsistent, when what depends on a failed unit keeps running.

  2. Dependencies

    A unit that needs what another created reads that unit’s outputs through a dependency block, and so runs after it. Before the first apply there are no outputs yet: a plan can run on stand-in values (mock_outputs).

    My part IAM, service accounts, networking, MySQL, ProxySQL and ScyllaDB on GCP, in Terraform and Terragrunt. MySQL and ProxySQL are installed through Terraform.

    In detail

    dependency reads another unit’s outputs and runs that unit first in a run queue; dependencies orders units without reading outputs. mock_outputs are the outputs to use when the dependency has none, for example before its first apply, so that this unit can run plan; mock_outputs_allowed_terraform_commands names the commands that may use them. 3 4

    Where it breaks With the commands limited to plan, an apply cannot run on stand-in values: it fails while the dependency has no outputs.

  3. Plan

    Before it changes anything, Terraform reads what is real, compares it with the configuration, and shows what it would create, change and destroy.

    In detail

    terraform plan creates an execution plan, a preview of the changes Terraform plans to make. By default it reads the current state of any already-existing remote objects, so that the state is up to date, compares the current configuration with the prior state and notes any differences, and proposes a set of actions that should make the remote objects match the configuration. 5

    Where it breaks With -refresh=false Terraform skips synchronising its state with the remote objects before it checks for changes: it ignores changes made outside it, and the plan can be incomplete or wrong.

  4. State

    Terraform keeps a state that maps the real resources to the configuration. Here it is in a Cloud Storage bucket, which supports locking: two runs cannot write one state at once.

    In detail

    Terraform uses its state to map real world resources to the configuration, keep track of metadata and improve performance, and to decide which changes to make. The gcs backend supports state locking, and enabling Object Versioning on its bucket is highly recommended. Locking happens automatically on every operation that could write the state; if it fails, Terraform does not continue (-lock=false turns it off for most commands, which the documentation does not recommend). For the gcs backend Terragrunt creates a missing bucket, with versioning enabled, when it is given the project and the location and has credentials; skip_bucket_creation and skip_bucket_versioning turn that off. 6 789

    Where it breaks A lock left behind by a run that was cut off stops the next operations that need the lock of that state. terraform force-unlock removes it; the documentation says to use it only on one’s own lock, where the automatic unlocking failed.

  5. Apply

    The apply makes the changes of the plan through Google Cloud’s APIs. On an error it records what has changed, unlocks the state and stops: what was done stays done.

    In detail

    When Terraform or a provider meets an error during an apply, Terraform logs the error, updates the state file with any changes to the resources, unlocks the state file and exits. The infrastructure may then be in an invalid state: Terraform does not roll back a partially completed apply. After the error is resolved, the configuration is applied again to bring the infrastructure to the desired state. 10

    Where it breaks An apply is not a transaction: between the error and the next apply the project may stay partly changed.

  6. IAM

    Roles can be described in three ways: the whole policy of a project, all members of one role, or one member of a role. The first two replace what was there, the third adds to it.

    In detail

    google_project_iam_policy is authoritative: it sets the IAM policy of the project and replaces any policy already attached. google_project_iam_binding is authoritative for a given role: it updates the policy to grant the role to a list of members, and other roles are preserved. google_project_iam_member is not authoritative: it grants a role to a new member, and other members of the role are preserved. 11

    Where it breaks A policy used together with bindings or members fights them over what the policy should be; a binding and members may be combined only for different roles. With the policy one can lock oneself out of the project.

  7. Credentials

    Terraform can act as a service account without holding its key: by impersonation. A key created by Terraform itself ends up in the state in plain text.

    In detail

    The provider can impersonate a service account for all its Google API calls, which needs the role roles/iam.serviceAccountTokenCreator on that account; the gcs backend can reach the state bucket through an impersonated account too. A static access token cannot be renewed by Terraform and expires, after an hour by default. google_service_account_key persists the credential it creates in plain text in the remote state. 7 1213

    Where it breaks Whoever can read the state can read a key kept in it: the documentation advises protecting the remote state accordingly.

The whole diagram

Infrastructure on GCP

Every component and step of the figure, drawn at once; the page lists them in text.

data control My part

Next case

Deployments to Kubernetes

GitOps: the desired state of the cluster is kept in a Git repository, and Argo CD’s controller compares what runs with it and brings the cluster back to it, resource by resource and wave by wave.

How a rollout goes Inside the infrastructure