8.terraform Modules
8.terraform Modules
TERRAFORM
MODULES
Your project is code complete
and it’s time to deploy it!
I know, I’ll use AWS!
You login to the AWS console
(Spend hours reading docs)
OK, I have a server running!
What else do I need?
Well, you probably want more than
one server for high availability
And a load balancer to distribute
traffic across those servers
And EBS Volumes and RDS databases
to store all of your data
You’ll need an S3 bucket for files
CloudWatch for monitoring
Don’t forget the VPC, subnets, route
tables, NAT gateways
Route 53 for DNS
ACM for SSL/TLS certs and KMS to
encrypt / decrypt secrets
stage prod
And also…
And you have to maintain it all.
Forever.
AWS: 1,000 new releases in 2021
Terraform: release every ~2 weeks
Security vulnerabilities: daily
There’s a better way to deploy
and manage infrastructure:
TERRAFORM
MODULES
TERRAFORM
MODULES
Reusable, composable, battle-tested
infrastructure code
I’ll show you
How Terraform Modules work?
stage prod
Code reuse
Remote or local source
Terraform evaluation
Mini-Terraform configuration
Multiple instances (no count)
Module Components
add(3, 5 )
add(10, 35)
add(- 45, 6)
assert add(3, 5 ) == 8
assert add(10, 35) == 45
assert add(-45, 6 ) == -39
def sub( x, y) :
return x - y
sub(add( 5, 3) , add( 4, 7) )
v a r i a b l e "image_id" {
d e s c r i p t i o n = "The ID o f the AMI t o run"
}
variable "port" {
description = "The p o r t t o l i s t e n on f o r HTTP requests"
}
* foo
* bar
* baz
Documentation is in README.md
A more complicated module:
> t r e e compl e t e - mod u le
.
├── m a i n . t f
├── o u t p u t s . t f
├── v a r i a b l e s . t f
├── README.MD
├── modules
├── examples
└── t e s t
amiId : = b u i l d Va u l t A m i ( t )
defer c l eanupAmi( t , amiId)
terratest.Apply(options)
defer t e r r a t e s t . D e s t r o y ( o p t i o n s )
assertCanInitializeAndUnsealVault(t, o p tio n s )
}
amiId : = b u i l d Va u l t A m i ( t )
defer c l eanupAmi( t , amiId)
terratest.Apply(options)
defer t e r r a t e s t . D e s t r o y ( o p t i o n s )
assertCanInitializeAndUnsealVault(t, o p tio n s )
}
amiId : = b u i l d Va u l t A m i ( t )
defer c l eanupAmi( t , amiId)
terratest.Apply(options)
defer t e r r a t e s t . D e s t r o y ( o p t i o n s )
assertCanInitializeAndUnsealVault(t, o p tio n s )
}
amiId : = b u i l d Va u l t A m i ( t )
defer c l eanupAmi( t , amiId)
terratest.Apply(options)
defer t e r r a t e s t . D e s t r o y ( o p t i o n s )
assertCanInitializeAndUnsealVault(t, o p tio n s )
}
amiId : = b u i l d Va u l t A m i ( t )
defer c l eanupAmi( t , amiId)
terratest.Apply(options)
defer t e r r a t e s t . D e s t r o y ( o p t i o n s )
assertCanInitializeAndUnsealVault(t, o p tio n s )
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
name = "Foo"
image_id = "ami-123asd1"
port = 8080
}
stage
prod
gruntwork.io
Many companies, all
running on the same
infrastructure code
gruntwork.io
stage prod
… into this
With some help from this
Questions?
Simple workflow
Workflow:
Modules
● Code reuse
● Apply versioning
● Use version constraints
● Store code remotely
● Easier testing
● Encapsulation
● Use and contribute to Module Registry
Workflow: Modules
What is the good terraform module?
variable “name” {}
output “bucket_id” {
value = “${aws_s3_bucket.bucket.id}”
}
Terraform Module
#Create module bucket
module “bucket” {
name = “MahBucket”
source = “.\\Modules\\s3”
}
#Use MahBucket
resource “aws_s3_bucket_object” {
bucket = “${module.bucket.bucket_id}”
key = “/walrus/bucket.txt”
source = “./mahbucket.txt”
}
Use a Module
Module Parameter