AWS Community Day CPH 2024 - Three problems of Terraform

AndreyDevyatkin 160 views 85 slides May 07, 2024
Slide 1
Slide 1 of 85
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85

About This Presentation

Terragrunt, Terraspace, Terramate, terra... whatever. What is wrong with Terraform so people keep on creating wrappers and solutions around it? How OpenTofu will affect this dynamic? In this presentation, we will look into the fundamental driving forces behind a zoo of wrappers. Moreover, we are goi...


Slide Content

NORDICS
DGI Byen’s CPH Conference
2024

NORDICS
The three problems of Terraform
Andrey Devyatkin | 2024-05-07

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

https://www.grc.com/sn/sn-923-notes.pdf

www.fivexl.io | hello@fivexl.io

Do it.
Do it better.
Do it right.
Alex Lindsay

www.fivexl.io | hello@fivexl.io

Andrey Devyatkin
Co-Host @ DevSecOps Talks
podcast
Principal AWS Consultant
AWS Community Builder
Security and Identity
Co-Founder @ FivexL
AWS User Group Leader
UG Las Palmas de GC

www.fivexl.io | hello@fivexl.io

https://www.istockphoto.com/es/foto/mano-de-gato-levantada-gm914509428-251700990

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

three
conceptual
problems
Dynamic state location
Deploying the same configuration to multiple
environments
Environment specific parameters
A way to address differences between environments
Cross-state resources lookup
A need to reference resources from different states

www.fivexl.io | hello@fivexl.io

Assumptions
AWS

www.fivexl.io | hello@fivexl.io

https://www.primevideo.com/detail/Silicon-Valley/0PHZ6LOP10TB423SCOM0BMCFMM

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

$ ls
README.md
.terraform
main.tf
terraform.tfstate

www.fivexl.io | hello@fivexl.io

terraform {
backend "s3" {
bucket = "my-cool-startup-infra-state"
key = "terraform/main.tfstate"
region = "us-east-1"
}
}

www.fivexl.io | hello@fivexl.io

Assumptions
AWS
S3 backend

www.fivexl.io | hello@fivexl.io

So far so good
No need for the wrapper

www.fivexl.io | hello@fivexl.io

https://www.amazon.co.uk/Silicon-Valley-Season-2-DVD/dp/B018I8RFZS

www.fivexl.io | hello@fivexl.io

We need to deploy the app to
the second environment

www.fivexl.io | hello@fivexl.io

Assumptions
AWS
dev/production
S3 backend

www.fivexl.io | hello@fivexl.io

We need to change
backend configuration
depending on env

www.fivexl.io | hello@fivexl.io

terraform {
backend "s3" {
bucket = "my-cool-startup-infra-state"
key = "terraform/main.tfstate"
region = "us-east-1"
}
}

www.fivexl.io | hello@fivexl.io

https://github.com/hashicorp/terraform/issues/17288
https://github.com/opentofu/opentofu/issues/1042

www.fivexl.io | hello@fivexl.io

Can we use Terraform
workspaces?

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/cli/workspaces
As of 2023-05-18

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/cli/workspaces#when-not-to-use-multiple-workspaces
As of 2023-05-18
Okay, what is the real life use case then?
??????

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/cli/workspaces#alternatives-to-workspaces
As of 2023-05-18

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

https://terragrunt.gruntwork.io/docs/features/keep-your-terragrunt-architecture-dry/

www.fivexl.io | hello@fivexl.io

But why so many
directories? Can’t we just
use the same directory
somehow? ??????

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/language/settings/backends/configuration#partial-configuration
As of 2023-05-18

www.fivexl.io | hello@fivexl.io

terraform {
backend "s3" {
bucket = "my-cool-startup-infra-state"
key = "terraform/main.tfstate"
region = "us-east-1"
}
}

www.fivexl.io | hello@fivexl.io

terraform {
backend "s3" {}
}
terraform init \
-backend-config "bucket=my-cool-startup-infra-state" \
-backend-config "key=terraform/main.tfstate" \
-backend-config "region=us-east-1"

www.fivexl.io | hello@fivexl.io

Do we share S3 bucket
between environments?

www.fivexl.io | hello@fivexl.io

Assumptions
AWS
dev/production
S3 backend
bucket/state per env with
predefined name

www.fivexl.io | hello@fivexl.io

terraform {
backend "s3" {}
}
terraform init \
-backend-config "infra-state-7YYYYYYYYY62" \
-backend-config "key=terraform/main.tfstate" \
-backend-config "region=us-east-1"

www.fivexl.io | hello@fivexl.io

Why did we name s3
bucket this way? ??????

www.fivexl.io | hello@fivexl.io


format("infra-state-%s",
data.aws_caller_identity.current.account_id)

www.fivexl.io | hello@fivexl.io

Would exposing the account
id get us into trouble? ??????

www.fivexl.io | hello@fivexl.io

As of 2024-05-07
https://medium.com/@maciej.pocwierz/how-an-empty-s3-bucket-can-make-your-aws-bill-explode-934a383cb8b1

www.fivexl.io | hello@fivexl.io

https://www.lastweekinaws.com/blog/are-aws-account-ids-sensitive-information/

www.fivexl.io | hello@fivexl.io

Control Tower naming convention

www.fivexl.io | hello@fivexl.io

# debatable
format("infra-state-%s-%s",
data.aws_caller_identity.current.account_id,
data.aws_region.r.name)

# paranoid edition
format("infra-state-%s", sha1(
format(
"%s-%s",data.aws_caller_identity.current.account_id
data.aws_region.r.name)))

www.fivexl.io | hello@fivexl.io

Why not just use env
suffix like -prod or -dev?
??????

www.fivexl.io | hello@fivexl.io

If we are using the same dir
then how to be with .terraform?
??????

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_data_dir
As of 2023-05-18

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

AWS_DEFAULT_REGION
env variable?
Use aws-vault for env setup

www.fivexl.io | hello@fivexl.io

$ ls
README.md
.terraform.7YYYYYYYYY62
.terraform.8XXXXXXXXX28
main.tf

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

How do we specify different
parameters for different
environments?

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/language/values/variables#assigning-values-to-root-module-variables
As of 2023-05-18

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

$ ls
README.md
7YYYYYYYYY62.tfvars
8XXXXXXXXX28.tfvars
.terraform.7YYYYYYYYY62
.terraform.8XXXXXXXXX28
main.tf

$ cat 7YYYYYYYYY62.tfvars
# dev
instance_type = "t4g.micro"

$ cat 8XXXXXXXXX28.tfvars
# prod
instance_type = "t4g.large"

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

Do we really need a
wrapper for this?

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

What if we add more
applications?

www.fivexl.io | hello@fivexl.io

https://infrastructure-as-code.com/book/2018/03/28/defining-stacks.html

www.fivexl.io | hello@fivexl.io

https://www.youtube.com/watch?v=wgzgVm7Sqlk

www.fivexl.io | hello@fivexl.io

How do I get VPC id from network
stack to my application stack?

www.fivexl.io | hello@fivexl.io

https://developer.hashicorp.com/terraform/language/state/remote-state-data

www.fivexl.io | hello@fivexl.io

Terragrunt remote state resoltion

www.fivexl.io | hello@fivexl.io

AWS SSM
Parameters
AWS S3
Self-containing
modules
Are the other ways?
Tooling

www.fivexl.io | hello@fivexl.io

Self-contained
modules? ??????
Create resources
Look up resources
Provide policies

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

two
more
problems
Licensing
Are you competing with HashiCorp?
Team work
How do you work together with the same state?

www.fivexl.io | hello@fivexl.io

OpenTofu
Not a big gap at the
moment
Unclear longevity
Community
requested features
Has a momentum

www.fivexl.io | hello@fivexl.io

Terraform is a cli tool
That operates on a shared resources

www.fivexl.io | hello@fivexl.io

Conventional CI/CD
vs
TACOS
Terraform Automation and Collobaration Systems

www.fivexl.io | hello@fivexl.io

Commit
UnitTest
Lint
Build
Deploy Test Promote
CI/CD Server
TACOS
Server

www.fivexl.io | hello@fivexl.io

Benefits of using
TACOS
Lock down access to
the state
Better visibility
Less shoulders
bumping

Extra features
https://www.reddit.com/r/Terraform/comments/lkylzk/scalr_vs_spacelift_vs_atlantis_vs_env0_bake_off/

www.fivexl.io | hello@fivexl.io

Conclusion and
recap

www.fivexl.io | hello@fivexl.io

three
conceptual
problems
Dynamic state location
Deploying the same configuration to multiple
environments
Environment specific parameters
A way to address differences between environments
Cross-state resources lookup
A need to reference resources from different states

www.fivexl.io | hello@fivexl.io

two
more
problems
Licensing
Are you competing with HashiCorp?
Team work
How do you work together with the same state?

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

Tools like Terragrunt offer nice extra
features

Which are making going back harder

www.fivexl.io | hello@fivexl.io

conventions vs wrappers

www.fivexl.io | hello@fivexl.io

www.fivexl.io | hello@fivexl.io

https://www.youtube.com/channel/UCiJjTS8EiCKlbT85It_0e6g

Thank you
https://github.com/Andrey9kin/3-terraform-problems
https://twitter.com/andrey9kin
https://www.linkedin.com/in/andreydevyatkin/
andrey.devyatkin@fivexl.io
https://devsecops.fm/

NORDICS
Thank you!