127
Deployment & Orchestration with Terraform Find the latest, print-friendly version of this presentation at https://christopherdemarco.com/terraform

Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Deployment & Orchestration with

TerraformFind the latest, print-friendly version of this presentation at

https://christopherdemarco.com/terraform

Page 2: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Copyright © 2017 Christopher DeMarco. All Rights Reserved.

The opinions and mistakes that follow are my own and do not represent my employer, Hashicorp, USENIX, or anyone else.

All code samples were believed correct at runtime. Your mileage may vary.

To my grandfather, who taught me how to write.To my father, who taught me why.

Page 3: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Who has used Terraform before?

3

Page 4: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Infrastructure Orchestration* Not configuration management!

All your vendors’ web interfaces are different, and they all suck.

How do you document mouse-and-keyboard? Playback?!

How can creating infrastructure become repeatable and reliable?

4

Page 5: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Infrastructure as CodeBecause programming makes things easier!

Automation!

Scale / test / rollback

Version

Collaborate / audit

Have you tried turning it off & back on again?

Interface consistency

Make devs help!

5

Page 6: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

“Infrastructure” is broad:

• AWS • Google Compute Engine

• Azure • Oracle Public Cloud

• 1&1 • Digital Ocean • Scaleway

• VMWare • Docker • Heroku

• OpenStack • Kubernetes

• Rancher • Nomad

• CloudFlare • Dyn • DDNS

• Chef • Cobbler • Rundeck

• MySQL • PostgreSQL • RabbitMQ

• DataDog • Grafana • New Relic

• Icinga2 • Librato • StatusCake

• Mailgun • OpsGenie • PagerDuty

6

Page 7: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Stop making toolsI don’t want a drill bit, I want a hole!

Stateful

Intelligent dependency graphing

Lightweight DSL

Incremental

Golang makes it fast and portable.

Commercial support available

7

Page 8: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Stop talkingshow us something already

Page 9: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Providers connect to a service that you’ll be managing.

Resources are the things you want to manage. They are declared with a type and an identifier.

A resource has arguments. They can be strings, lists, or maps.

Whitespace is flexible.

HCL can be converted to/from JSON.

9

Page 10: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set
Page 11: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf init

Initialize the package directory. Copy in needed binaries.

Initialize empty state if none exists.

`tf init` is idempotent.

Terraform will prompt you to run `terraform init` if it can’t find what it needs.

11

Page 12: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

12

Page 13: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf plan

Interrogate the provider[s].

Compare with local state.

Calculate dependency graph.

Print what will be done.

The plan is not saved (unless expressly requested).

13

Page 14: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

14

Page 15: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf applyPlan.

Apply the dependency graph.

Update local state.

15

Page 16: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

16

Page 17: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

17

Page 18: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Let’s change something.

18

Page 19: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

19

Page 20: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

20

Page 21: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

What if it’s a destructive change?

21

Page 22: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

22

Page 23: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

23

Page 24: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

What if we remove the resource?

24

Page 25: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

25

Page 26: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

26

Page 27: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf destroy

Teardown *all* resources.

Prompt for confirmation (unless `-force`).

There is no undo!

Calculate dependency graph so deletion is never* blocked.

27

Page 28: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

28

Page 29: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

In a demo, no-one can see your creds.

Use the provider’s default setup? (e.g. `~/.aws`)

Use env vars?

Set expressly?

Read a file?

29

Page 30: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Let’s build a simple webserver.

30

Page 31: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Terraform will load and evaluate everything in the current directory named like `*.tf` .

Configuration is declarative, order does not matter.

Dependencies will be discovered and graphed.

31

Page 32: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Dependency graphing means that order does not matter!

Once a resource is instantiated, it exports attributes. Dereference them like `${type.name.attribute}` .

Outputs will become very useful later on.

32

Page 33: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Dependencies can span files . . . this can become difficult to manage. Use common sense.

`name` attribute is *not* Terraform’s identifier!

33

Page 34: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

34

Page 35: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf output

35

Page 36: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Sensitive outputs

36

Page 37: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Variables

37

Page 38: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Variables must be declared. Type may be declared, or inferred from the default. If neither type nor default is given, a string is assumed.

Use variables just like you use resource attributes.

Variables are scoped to the package in which they are declared.

38

Page 39: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

If `terraform.tfvars` exists, it will be evaluated.

Specify tfvars files, or set variables directly, on the command line.

Or set them via environment variables.

Terraform will prompt for values for any variables that haven’t been set otherwise.

39

Page 40: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Precedence:

The last file or variable specified on the command line wins.

Otherwise, `terraform.tfvars` wins.

Otherwise, an env var wins.

Otherwise, the default is used.

Otherwise, you’re prompted.

*Maps are merged!

40

Page 41: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Variable syntax summary

41

Page 42: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Modules

42

Page 43: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Modules are basically functions.

Recall that variables are scoped to a package. Therefore you must explicitly pass data into and out of modules.

43

Page 44: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Declare the variables that will be passed in.

Output the variables that will be returned.

44

Page 45: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

When passing variables into a module, make sure the names match!

45

Page 46: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

46

Page 47: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Copy the necessary modules into the working directory.

Local paths can be relative or absolute.

Load remote modules from Git, Mercurial, HTTP URIs; S3; Terraform Registry.

Read and evaluate any third-party modules before using them!

47

tf get

Page 48: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

48

Page 49: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Provisioners

49

Page 50: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Provisioners get resources ready for the next step.

Maybe we use Ansible and need to ensure we have Python.

The provisioner will eventually timeout if, for example, you can’t SSH there from here.

50

Page 51: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Idempotent?

Multiple provisioners will be executed in the order they’re declared.

Terraform does not “grok” provisioners. A failed provisioner run will cause the entire resource to fail.

OTOH: a successful provisioner will never be re-run. What if you *want* to re-run a provisioner?

51

Page 52: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

`tf taint` marks a resource as “tainted”—it is to be destroyed & recreated.

`tf untaint`

52

tf taint

Page 53: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

53

Page 54: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Is there a better way?

54

Page 55: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

A `null_resource` is a “virtual resource” created in the project’s state.

To re-run the provisioner, `tf taint null_resource.provision_me`!

55

Page 56: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Other ways to use remote-exec

`inline` takes a list of strings.

`script` will upload a file and execute it.

`scripts` will upload a directory.

56

Page 57: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

More provisioners* Not configuration management!

Chef

File

Local-exec

Salt-masterless

57

Page 58: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

What if I want n of a thing? How do I loop?

58

Page 59: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Terraform iterates over `count`, setting `count.index` each time.

If `count` is > 3, we will get duplicate Env tags.

`*` means “all resources”.

Select into a list with the `element()` function.

59

Page 60: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

OK, how about conditionals?

60

Page 61: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

This almost works . . .

61

Page 62: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

But the ternary operator is more flexible.

62

Page 63: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Use functions to modify variables.https://www.terraform.io/docs/configuration/interpolation.html

Page 64: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf console

64

Page 65: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Templates

65

Page 66: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Use templates where string interpolation would be unwieldy.

66

Page 67: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Data sources are dynamic, read-only ways to get at data.

Escape interpolation with `$$`.

Like modules, variables must be passed into a template expressly.

Unlike modules, template variables don’t get declared.

Also unlike modules, template variables are not accessed like `var.name`.

67

Page 68: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

68

Page 69: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Well, that was anticlimactic . . .

69

Page 70: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

AWS lets you pass “user data”: scripts that hosts will run on first boot.

Load a template from a file with the `file()` function.

Render the template locally to assist debugging.

70

Page 71: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

71

Page 72: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

One-to-many is easy; what if we want many-to-one?

72

Page 73: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

73

Page 74: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Here-doc syntax is available throughout Terraform.

The `local_file` resource will create directories as needed.

74

Page 75: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

You can use functions in templates, too.

75

Page 76: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

76

Page 77: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

breakreturn at 1100

Page 78: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

What happens if I lose state?

78

Page 79: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

79

Page 80: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Use remote state!

Backup

Collaboration

Pipe data among tf projects

Pipe data among other tools

Update centralized SSOT

80

Page 81: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

State backends

Artifactory

AWS S3

Azure

Consul

etcd

Google Cloud Storage

HTTP REST

OpenStack

81

Page 82: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Backends have specific configuration details.

Whichever backend you use, ensure that it is not publicly visible.

Turn on versioning if available.

82

Page 83: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

83

If the backend is already configured, `tf init` will proceed.

Existing state will be migrated to the new backend.

Page 84: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

If the backend is not configured, use `tf init` to fill in the details.

This is useful for automated configuration.

84

Page 85: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

85

Now that state is stored remotely, we can easily use it in other parts of our infrastructure.

`tf state pull` will fetch remote state.

Page 86: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Reference state outside of the package.

86

Page 87: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Security groups and an app

87

Page 88: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Reference remote state just like any other resource.

If the remote state changes, any references will become stale.

Re-apply any referring packages.

88

Page 89: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Happily, `terraform_remote_state` is a read-only reference. `destroy` does not touch it.

89

Page 90: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Inspect state.

90

Page 91: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

91

tf showDump the state or planfile.

This is useful for figuring out how to reference a module we want to taint.

(But we can do better!)

Page 92: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

92

Page 93: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf state Select and pretty-print items from the state.

93

Page 94: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

94

Page 95: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf state pull | jq`pull` will dump the state as JSON.

Now you can use the AWESOME `jq` JSON parser!

95

Page 96: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Use workspaces to namespace state.

96

Page 97: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

tf workspace

Use distinct directories for state.

`new` to create, `list` to list, `select` to switch. `show` if you can’t remember where you are.

Use with the Consul and S3 backends.

Use the variable `${terraform.workspace}` for e.g. naming things.

97

Page 98: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Enough AWS already!What else can Terraform orchestrate?

98

Page 99: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Let’s take a tour.Beware the bright light!

99

Page 100: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Google Cloud

100

Page 101: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

PostgreSQL

101

Page 102: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

And of course, you can mix-and-match.

102

Page 103: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

103

Page 104: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Recap: providers, resources & data sources

104

Page 105: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Provider arguments

Providers vary as to authentication.

Creds may be provided inline, via environment variables, and/or filesystem paths.

Providers may require regions, cloud types, and/or projects to be specified.

Store your creds safely!

105

Page 106: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Resource arguments and attributes

Arguments to resources may be required or optional.

Some arguments can be changed without re-creating the resource.

An attribute’s value always reflects its current state. Refresh via `plan` or `apply`.

106

Page 107: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Data sourcesData sources are *read-only*.

Arguments provide filters to restrict the kind / quantity of results.

Attributes are typically numerous and contain nested data structure.

107

Page 108: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotchas

Page 109: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Module param mismatch

109

Page 110: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Silent failure on missing output

110

Page 111: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Silent failure on missing output(cont.)

111

Page 112: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Useless var interpolation!

112

Page 113: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Use `local` variables.

113

Page 114: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Multiple accounts

114

Page 115: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Bizarre errors

115

Page 116: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!State changed from under me!

116

Page 117: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Destroy is scary!Insurance against co-workers?

117

Page 118: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Timeout!

118

Page 119: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!Increase verbosity

`TF_LOG=<TRACE, DEBUG, WARN, INFO, ERROR>`

You get to drink from the firehose!

It’s mainly useful for proving you’ve found a bug.

119

Page 120: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha! A bug in the provider!

Use the provider’s `version` argument to pin it.

Providers are in a separate GitHub organization from Terraform Core: https://github.com/terraform-providers . Look at the relevant CHANGELOG.

120

Page 121: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha!tfstate is huge!

It’s just JSON. Use `jq` or your favorite programming language.

Go install `jq` and rejoice.

121

Page 122: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Gotcha! Dependencies hurt my brain!

122

Page 123: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

123

Page 124: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

124

Page 125: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

. . . Getcha?Golang niceities

Terraform is a single binary. Download from http://terraform.io .

`tf fmt` will apply the standard Go formatter to canonicalize style—spacing, quoting, etc.

`tf validate` will check for syntax and dependency graph issues.

125

Page 126: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Q & A

Page 127: Orchestration with Deployment & Terraformchristopherdemarco.com/terraform/M4-Deployment-and... · If `terraform.tfvars` exists, it will be evaluated. Specify tfvars files, or set

Thank you.Please fill out your surveys.