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 demonstrate how to use Terraform variables and outputs in the sample web application configuration shown earlier in the course to make it more flexible and generalizable.
locals {
example_variable = "example_value"
}
2. **Input Variables:** Define input variables in a separate `variables.tf` file or within your `main.tf` file. These variables allow you to configure and change values at runtime.
For example, we can define the following variables associated with our EC2 instacnes:
```json
variable "ami" {
description = "Amazon machine image to use for ec2 instance"
type = string
default = "ami-011899242bb902164" # Ubuntu 20.04 LTS // us-east-1
}
variable "instance_type" {
description = "ec2 instance type"
type = string
default = "t2.micro"
}
We can do the same for many other values used throughout our configuration to make it more flexible. See https://github.com/sidpalas/devops-directive-terraform-course/blob/main/04-variables-and-outputs/web-app/variables.tf for examples.
After defining the variables, we then reference them within the resource configuration using var.VARIABLE_NAME
:
resource "aws_instance" "instance_1" {
ami = var.ami
instance_type = var.instance_type
security_groups = [aws_security_group.instances.name]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World 1" > index.html
python3 -m http.server 8080 &
EOF
}
bucket_prefix = "devops-directive-web-app-data"
domain = "devopsdeployed.com"
db_name = "mydb"
db_user = "foo"
# For sensitive variables, pass them at runtime instead of storing them in the .tfvars file.
# db_pass = "foobarbaz"
output "instance_1_ip_addr" {
value = aws_instance.instance_1.public_ip
}
terraform init
, terraform plan
, and terraform apply
steps to provision the infrastructure.terraform apply -var "db_user=my_user" -var "db_pass=something_super_secure"
By using variables in this way, you can deploy multiple copies of a similar but slightly different web application.
Additionally, you can create staging and production environments simply by configuring different variable values within your terraform.tfvars
file.
Remember not to store sensitive values like passwords in your .tfvars
file; instead, pass them at runtime or use an external secrets manager.