TW presents: Getting started with Terraform Pipelines (by Michael Lihs)
- Shared screen with speaker view

40:28
Find the whole Code of Conduct from ThoughtWorks here:https://www.thoughtworks.com/code-of-conduct-germany

44:30
Just ping your questions to the chat and we will gather them to make sure we don’t miss anything. :-)

51:24
Question: Will you be doomed if state gets somehow lost? Can you recover from a state loss?

51:48
you should store your state in a high durability place, such as S3

51:56
not exactly but close enough

52:06
Terraform gives the possibility to import ressources.

52:07
you can import existing infrastructure into a state

52:08
terraform import can help

52:38
You should backup your state file aswell :-) maybe work with snapshots bevor/after apply

53:09
Does terraform give a graphical representation of the infra getting created like for e.g cloud formation designer provides?

53:13
But there is no import all yet … it is per resource (at least last I checked)

53:15
Question: what happens if resorces were changed outside terraform?

53:15
Followup question: What if your state loss happens because a state apply failed and you have an intermediate state?

53:23
not every resource can be imported

53:35
You should take care of import. If the provider is implemented badly it won't help you very much

53:36
Do you persist the state then?

54:23
When a state apply fails because AWS rejects the request?

54:45
But most of the time they just get destroyed and recreated

55:05
the state needs to be persisted in any case. Because if you don’t two different people applying terraform code would recreate resources

55:06
On which service does terraform run on?

55:33
terraform usually stores backup file while apply

55:39
Is there a possibility to snapshot tf-state in a storage-account?

55:51
How do changes to resources, which were triggered manually, affect the terraform state?

55:52
All major cloud providers, you can even order Dominoes Pizza with Terraform @Rajan

55:54
yes

56:03
in CI

56:05
:-)

56:29
Important to mention it doesn’t run all the time … it’s not a service or something … it’s run in a pipeline, makes changes and exits

57:10
Recommendation for state version is to enable it on the remote storage eg S3 supports versioning of files

57:45
These are the supported systems that Terraform can configure: https://www.terraform.io/docs/providers/index.html

57:47
the state in the end is a JSON File. If everything else fails, things can fixed by editing the file and reuploading it, although this is a recipe for pain

57:52
terraform cloud is free for state storage as well

58:00
My team works with AWS, Github and K8S providers

58:46
Indeed there is also Terraform cloud, we can’t use that as our clients have restrictions in place about where sensitive data eg state files can be sotred

59:31
tdd is kinda impossible with terraform

59:45
terratest is ok to use

59:59
but doesn't help much in tdd

01:00:25
Terraform vs. Pulumi…. Which is gaining more traction these days?

01:00:31
terraform

01:00:53
terraform has a bigger offering of providers, pulumi is in very active development as well

01:00:57
pulumi is gaining traction

01:00:58
So how big was the blast radius on your projects outage?

01:01:01
:)

01:01:11
if you are starting from scratch I would encourage to check both and see which one fits your use case better

01:01:24
It depends on how you consider TDD, my team have written tests and then written the terraform to deploy working code to make the tests go green - this is out of scope of this talk but we have run talks on infra testing before and will again

01:02:11
My team have focused on Terraform as it’s a common language and tool set to work with Github, K8S and AWS

01:02:19
interesting

01:03:03
any links Tim?

01:05:08
What does “Go for immutable infra” mean exactly? Other than a DB

01:05:09
No links to hand sorry

01:05:28
@Tim, what does it mean to split state? Is this an outcome of infra modularity?

01:06:16
should we split state for each environment?

01:06:25
YES

01:06:28
immutable infra would be deploying infrastructure that is not changed until the next deployment. For instance, all the networking is based on the TF code reflecting the end state.

01:07:13
Thanks @Mario

01:08:05
I have a question, where do you run terraform code, in azure devops, azure container or azure shell, or local laptop?

01:08:45
Is the concept of pipelines and stages applicable with other tools than Terraforn? E.g. with Ansible?

01:09:12
yes, though the declarative nature of terraform tends to make describing the end state a bit easier

01:09:54
For my team we run all terraform code in pipeline only, we do not run terraform locally at all and have not creds to do so. My team make an active choice to do this.

01:10:17
Quick question: It looks like your terraform code for many services is all in a single “infra” repo - was there any intentional decision to do this instead of putting the terraform scripts alongside the service code inside each services repository? What drove that decision?

01:10:21
Our CI is GoCD running in K8S containers

01:10:47
tim do you use atlantis?

01:11:10
We’ve just started using Atlantis … it’s nice :D

01:11:14
Not yet, we are considering it in the future

01:11:49
Tim, thanks a lot, do you use HachiCorp Vault for secrets or some parts of GoCD for secrets?

01:12:36
I don't like workspaces

01:13:00
Currently we are using K8S “secrets” we will be moving to AWS secrets store “soon” and then in a while we will be standing up vault

01:13:57
What’s the best place to version control infrastructure code, in repo with application code or in separate repo?

01:14:03
My team is building a platform in AWS for a large Thoughtworks client. We are currently getting our MVP up and running for our first team to move to our platform

01:14:09
Plus Locks can only be removed if you have the right permissions.

01:14:19
Did you discuss splitting state even more, e.g. one statefile per service? Why did you decide against it?

01:14:41
monorepo vs mico repos with code?

01:15:12
and last thing, do you use tools like test terratest, kitchen-terraform, validate? or some part of GoCD can help with testing?

01:15:19
I think version control answer is “it depends” and it also depends how tightly linked your code and infra are

01:16:43
Is there anyone who experienced with Jenkins for CI pipeline? How good is Jenkins about Terraform CI pipelining?

01:16:44
My team currently tests with tflint and terraform validate, we have additional in pipeline tests to ensure resources are available and configured correctly.

01:17:00
We are also looking at awsspec for testing

01:17:20
works good

01:17:21
I have 0 Jenkins experience I think that @Mario has much more

01:17:37
I see a lot of azure specific configuration? I was expecting something more platform independent so you can describe the state independent of where it is applied (Azure, AWS, etc)?

01:17:40
Have you looked at terragrunt at all? It can handle splitting and templating of environments. Well worth a look if it fits your needs

01:17:46
any CI tool can be used to be honest, though I’ve seen many centralized jenkins instances that are exceedingly hard to use

01:17:47
why not to split states down to each independent servece?

01:18:08
+1 to terragrunt, can be used effectively to address some terraform pain points

01:18:36
Terragrunt is something that we are looking at next week to fix some of the pain of copy pasted variables

01:18:44
Why not split to each service: There are tons of interdependencies you can create between services and passing variables back and fourth becomes a PITA

01:20:08
I think terragrunt will be obsolete in some time as tf adds those teatures

01:22:18
My team uses both terraform data calls and terraform remote_state as data sources to pass information between small pieces of terraform code

01:22:47
we use remote state too

01:22:52
how "cloud specific" is the resulting terraform code? I'm asking since my team might have to e.g. provision k8s and other services on multiple cloud service providers.

01:23:15
very specific

01:23:33
since resources change by clouds

01:25:59
The k8s terraform will apply to most versions of k8s, but different cloud providers provide different resources as Ashish says

01:26:42
The concepts however are always the same, resources / data objects same templating engine etc

01:26:42
thanks

01:27:17
If you want to create a kubernetes cluster, just take a look at the differences between the terraform resource “azurerm_kubenretes_cluster” and “google_container_cluster” … you have to handle ALL those differences yourself

01:30:02
did you. setup pipeline for terraform right away or after your infra reached a certain state

01:30:06
?

01:30:22
My team went straight to pipeline never ran it locally

01:30:24
State described well here. Also the graph tool https://thorsten-hans.com/terraform-state-demystified

01:31:15
But we where starting with a Greenfield setup and we had the opportunity to make a clear choice of pipelines only

01:32:46
We have sometimes needed to do interactive terraform but only a few times in the last 6 months. We do this from the CI/CDs

01:33:01
agents*

01:33:13
gotcha

01:33:55
We actually tag all our infra with the unique pipeline ID as well so we can trace AWS -> pipeline -> code

01:34:51
Where/how do you publish the various terraform packages? Is it just pushing a docker image to a registry every time?

01:35:50
You can run a terraform registry or just use git but we are not at this stage yet

01:35:57
How do you build the pipeline itself? Also with terraform?

01:36:28
Can we elaborate more on why “the download terraform package” is needed in every stage?

01:36:39
We actually use yaml to define them in git and then have good sutopull them

01:36:39
In Gitlab CI/CD you can use “artifacts” to store the package…

01:37:06
Thanks @Matthias

01:37:07
There is a GoCD terraform provider but it’s a little trickier to automate for us

01:37:10
What if the "test" stage in prod fails?

01:37:38
We've actually created AWS CodePipelines with Terraform, which then later runs Terraform.. works

01:37:52
That’s an interesting question Steffen, and your team should decide if it’s an alert etc

01:37:54
we build pipelines with terraform using aws codepipeline and codebuild

01:38:08
When automating terraform plan/apply in a pipeline (no manual approval), do you put any restrictions on? (i.e. don’t automatically apply if there are any destroy’s in the plan)

01:38:21
@TIm that then looks like dev diverges from prod, so you probably can't sooo easily push a change through

01:38:52
kk, thanks

01:39:14
Currently the Azure DevOps Terraform provider is being developed. So for the moment it is not stable and a lot of resources within Azure DevOps unfortunately can't be terraformed.

01:39:20
Are you running the pipeline from non-master branches? If, what happens there?

01:39:39
We always run from master

01:39:49
haha … chip is implanted

01:40:03
do you run azure rm templates from teraform/terragrunt?

01:40:55
How do you know which version or git commit of the terraform scripts was used to build the current prod?

01:41:20
We tag things with the pipeline ID which is made up of the git short hash + pipeline run count

01:41:21
Like add labels in with the git commit to the resources ?

01:41:38
Thanks @tim

01:42:08
We also do some AWS tricks to have the pipeline ID embedded in the AWS role assume process that we use

01:42:35
thx

01:43:52
For “proper” unit testing with all dependencies in memory, it’s looking compelling on prem by combining Terraform with the golang vCenter vcsim tool where you can replicate a vCenter set of resources locally. Is anyone aware of such simulation tools for any of the public clouds?

01:44:19
There are some AWS stub tools, I’ve never looked at them

01:46:29
What exactly caused your complete infra outage?

01:46:53
👏👏👏

01:47:01
How are you guys managing state migrations? For example, I want to rename a module …

01:47:08
great question!

01:47:13
Gah … enter too early :D

01:48:02
Caused, not cost

01:48:06
I want to rename a module … module.x.something to module.y.something … we’ve always had to do this “by hand” before our pipeline runs with terraform state mv … any hints on how to get this in a pipeline in a "migrations”

01:48:07
tf state mv works well

01:48:13
@Hannes BBasically applying a new resource against a new (wrong) terraform state.

01:48:18
Personday, not Manday ;)

01:48:24
Yeah sure … corrupted state ...

01:48:28
:)

01:48:38
Thanks

01:48:45
so there is a way to provision resources via powershell modules/integration with UI via Azure resource templates (similar to cloudformation on AWS), when you develope infrastrcuture with teraform you are not always have chnages from Azure in modules

01:49:00
Do you have an opinion on terra grunt?

01:49:06
@Sean Mitchell, grep and sed possibly? :D

01:49:10
and I was interested how do you integrate azure rm?

01:49:20
I've done a complete nested module renames

01:49:36
For “proper” unit testing with all dependencies in memory, it’s looking compelling on prem by combining Terraform with the golang vCenter vcsim tool where you can replicate a vCenter set of resources locally. Is anyone aware of such simulation tools for any of the public clouds?

01:49:42
Grep and sed … no :D terraform state mv works fine, but its always outside of our pipeline. We are adding it to our PRs right now to say “Hey, before we merge this PR into environment, do these state moves”

01:49:57
But how to "terraform state.." when you don't have access to the state / infra, because only your CI has @Ashish

01:50:18
It also happened recently when azurerm renamed some resources from like azurerm_hub_dps to azurerm_iothub_dps … terraform wanted to erase and recreate

01:50:32
i did it manually

01:50:38
not in ci

01:50:57
that then, of course, breaks the concept of only CI has access

01:51:24
🙏

01:51:31
yeah

01:51:32
what about rollbacks with terraform?

01:51:32
Get Michaels slides here: https://github.com/michaellihs/meetup-2020-05-07-terraform-pipelines/blob/master/presentation.pdf

01:51:36
but I don't see a good alternative, except that you have a pipeline which allows to do such renaming

01:51:52
Big appreciation for the talk!

01:51:52
👍👍👍

01:51:53
Thanks, very good structured

01:51:54
thank you very much

01:51:57
Thank you

01:51:57
Thanks!!! :)

01:52:00
Thanks

01:52:03
thanks 😊

01:52:04
Awesome!

01:52:07
Thanks!

01:52:07
Thanks!

01:52:08
Thank you so much :)

01:52:09
many thanks

01:52:09
Thanks :)

01:52:11
bye

01:52:11
Thank you!

01:52:14
thank u :)

01:52:15
Great talk! thanks

01:52:16
thanks

01:52:16
Thanks :)

01:52:18
thank you!

01:52:19
thx

01:52:19
I would do the rename via a temp pipeline

01:52:19
Thank you!

01:52:21
Thanks.

01:52:22
Thank you

01:52:22
awesome!! thank a lot TW

01:52:23
Thanks :-)

01:52:24
Thanks for a great talk

01:52:29
Thanks for joining everyone! :)

01:52:40
Thank you guys!

01:52:44
Thank you very much!! I really enjoyed the presentation!

01:52:44
cheerio!

01:52:44
Thank you!

01:52:50
Have to go grab some Pizza now :)

01:52:56
And also thanks to Tim for answering questions in the chat!

01:53:01
3 dots -> save chat

01:53:03
Good question, because there are good questions and answers here :-)