Referencing the Five Basic Types of Terraform Config Files
The aim of this pageπ is to explain the five basic types of terraform config files. Config_files represent one of the four major component of terraform itself, besides 1. single executable (terraform) 2. plugins (e.g. aws/gcp provider) and 3. state_files
2 min readAug 17, 2023
A common approach is to
- put your resources in a
main.tf
file - inputs in
variables.tf
- outputs in
outputs.tf
.
Additionally, you can also create a modules
sub-folder for any private sub-modules you make (private means they are not intended for use in other modules). Also, you can create an examples
folder that contains examples in subfolders, and a README
that describes the module's use. Here's an example folder structure for a module in the git repo terraform-aws-pavol
:
terraform-aws-pavol
βββ examples
β βββ simple
β βββ main.tf
βββ modules
β βββ lambda_function
β β βββ main.tf
β β βββ variables.tf
β β βββ outputs.tf
β βββ ecs
β βββ main.tf
β βββ variables.tf
β βββ outputs.tf
βββ main.tf
βββ variables.tf
βββ outputs.tf
βββ README.md
1. variables to create necessary abstractions, usually places in variables.tf
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_region" {
default = "us-east-1"
}
- create necessary abstractions for storing data, for example to hold aws access keys, region to be used, etc.
- usually the top of the file
2. providers to deploy an environment with its specific requirements such are region or secrets
provider "aws" {
access_key = "var.access_key"
secret_key = "var.secret_key"
region = "var.aws_region"
}
- deploy environment
- you pass the variables
- connect to AWS by defining a provider
- AWS is a provider, a platform you are building your
- provider properties are in docs
3. datasources obtaining data about services such as available AMIs or information about AZs
data "aws_ami" "alx" {
most_recent = true
owners = ["amazon"]
filters {}
}
- query the cloud for the datasource that may be used later as a deploy input
- you pull a DATA SOURCE β querying an informatino about that provider
- if getting all amazon linux ami
- or, you could be pulling information about availability zone
4. resources that are created within the public cloud, usually put in main.tf
resource "aws_instance" "ex" {
ami = "data.ams_ami.alx.id"
instance_type = "t2.micro"
}
- deployment INPUT
- you create a resource or deploy a resource
- weβre going to be creating a server
- this is what is needed to be created
- resource takes several parameters to be created or passed as a variable or as a data source
5. output β data produced by the deployment that may be usefulel, usually in output.tf
- for example public IP address
output "aws_public_ip" {
value = "aws_instance.ex.public.dns"
}