A Terraform block specifies the required providers that terraform needs in order to execute the script. This block also contains the source block that specifies from where terraform should download the provider and also the required version.
A provider block specifies the cloud provider and the API credentials required to connect to the provider’s services. It includes the provider name, version, access key, and secret key.
1 2 3 4
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs provider "aws" { region = "us-east-1" }
A resource block represents a particular resource in the cloud provider’s services. It includes the resource type, name, and configuration details. This is the main block that specifies the type of resource we are trying to deploy.
A data block is used to fetch data from the provider’s services, which can be used in resource blocks. It includes the data type and configuration details. This is used in scenarios where the resource is already deployed, and you would like to fetch the details of that resource.
1 2 3 4 5 6 7 8 9 10
# https://developer.hashicorp.com/terraform/language/data-sources data "aws_ami" "example" { most_recent = true
A variable block is used to define input variables that are used in the Terraform configuration. It includes the variable name, type, and default value.
1 2 3 4 5 6
# https://developer.hashicorp.com/terraform/tutorials/aws-get-started/aws-variables variable "instance_name" { description = "Value of the Name tag for the EC2 instance" type = string default = "ExampleAppServerInstance" }
Modules are containers for multiple resources that are used together. A module consists of .tf and/or .tf.json files stored in a directory. It is the primary way to package and reuse resources in Terraform.
Every Terraform configuration has at least one model (root module) which contains resources defined in the .tf files. Test configuration we created in the third part of these series is a module.
Modules are a great way to compartmentalize reusable collections of resources in multiple configurations.
Often called local variables block, this block is used to keep frequently referenced values or expressions to keep the code clean and tidy.
Locals block can hold many variables inside. Expressions in local values are not limited to literal constants. They can also reference other values in the module to transform or combine them. These variables can be accessed using local.var_name notation, note that it is called local. when used to access values inside.
1 2 3 4 5 6 7 8 9 10 11 12 13
# https://developer.hashicorp.com/terraform/language/values/locals locals { # Ids for multiple sets of EC2 instances, merged together instance_ids = concat(aws_instance.blue.*.id, aws_instance.green.*.id) }
locals { # Common tags to be assigned to all resources common_tags = { Service = local.service_name Owner = local.owner } }
Provisioners allows us to specify actions to be performed on local or remote machines to prepare resources for service. There are two types of Terraform provisioners:
local-exec invokes local executable after a resource is created. It runs the process on the machine running Terraform, meaning the machine where you run terraform apply. This is most likely your own computer.
remote-exec invokes remote executable, something like an EC2 instance on AWS.
This is an example of a provisioner for an EC2 instance. This example contains both local-exec and a remote-exec: