Examine the evolution of virtualization technologies from bare metal, virtual machines, and containers and the tradeoffs between them.
Install terraform and configure it to work with AWS
Learn the common terraform commands and how to use them
•Terraform Plan, Apply, Destroy
Use Terraform variables and outputs to improve make our configurations more flexible
Explore HCL language features in Terraform to create more expressive and modular infrastructure code.
Learn to break your code into modules to make it flexible and reuseable
Overview of two primary methods for managing multiple Terraform environments
Techniques for testing and validating Terraform code
Covers how teams generally work with Terraform, including automated deployment with CI/CD
In this lesson, we will explore some advanced features of the HashiCorp Configuration Language (HCL) used in Terraform. These features can help make your Terraform code more expressive and modular.
Terraform provides various expressions, operators, and functions to build dynamic strings, perform arithmetic operations, and use built-in functions. You can refer to the Terraform documentation for a complete list and usage examples.
Some examples include:
for
loops can be used to loop over a list of configurations.Terraform provides various meta arguments to control the behavior of resources, such as depends_on
, count
, for_each
, and lifecycle
.
You can find more information in the Terraform documentation.
This meta argument is used when one resource implicitly depends on another, but there's no direct connection within the config. For example, if an instance needs access to an S3 bucket, you need to provision the role policy first.
resource "aws_instance" "example" {
# ...
depends_on = [aws_iam_role_policy.example]
}
Use count
to create multiple copies of a resource. This is useful when you have nearly identical resources.
resource "aws_instance" "example" {
# ...
count = 4
tags = {
Name = "server-${count.index + 1}"
}
}
### for_each
`for_each` is similar to count, but it provides more control over each resource. It allows you to loop through an iterable and create resources based on its values.
```json
locals {
subnets = ["subnet-abc123", "subnet-def456"]
}
resource "aws_instance" "example" {
# ...
for_each = toset(local.subnets)
subnet_id = each.value
}
The lifecycle meta argument is used to specify the order in which Terraform takes actions, like creating resources before destroying them, or ignoring changes.
resource "aws_instance" "example" {
# ...
lifecycle {
create_before_destroy = true
ignore_changes = [tags]
prevent_destroy = true
}
}
Provisioners allow you to perform actions locally or on remote machines. You can use file
, local-exec
, or remote-exec
provisioners, or use vendor-specific provisioners like the Ansible provisioner.
For more information, refer to the Terraform documentation on provisioners.
Throughout the course, we will continue to use some of these advanced language features as we develop our web application infrastructure. To get the most up-to-date and detailed reference, make sure to consult the Terraform documentation.