mirror of
https://github.com/sidpalas/devops-directive-terraform-course.git
synced 2025-12-11 13:21:14 +00:00
initial commit
This commit is contained in:
71
04-variables-and-outputs/examples/README.md
Normal file
71
04-variables-and-outputs/examples/README.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Variables
|
||||
|
||||
## Variable block
|
||||
|
||||
must define variable block
|
||||
|
||||
```
|
||||
variable "var_name" {
|
||||
type = string
|
||||
}
|
||||
```
|
||||
|
||||
## Variable types
|
||||
- string
|
||||
- number
|
||||
- bool
|
||||
- list(<TYPE>)
|
||||
- set(<TYPE>)
|
||||
- map(<TYPE>)
|
||||
- object({<ATTR NAME> = <TYPE>, ... })
|
||||
- tuple([<TYPE>, ...])
|
||||
|
||||
## Variable files
|
||||
`variables.tfvars` (or `<FILENAME>.auto.tfvars`) automatically applied
|
||||
|
||||
## Apply default
|
||||
`terraform apply`
|
||||
|
||||
## Apply a different variable file
|
||||
`terraform apply -var-file=another-variable-file.tfvars`
|
||||
|
||||
## Passing Variable via Prompt
|
||||
If value not specified, Terraform will prompt for value. (this is okay for testing... but don't depend on it since you should be automating things!)
|
||||
```
|
||||
var.db_pass
|
||||
password for database
|
||||
|
||||
Enter a value:
|
||||
```
|
||||
|
||||
## Passing Variables via CLI
|
||||
`terraform apply -var="db_pass=$DB_PASS_ENV_VAR"`
|
||||
|
||||
# Local Variables
|
||||
|
||||
Allows you to store the value of expression for reuse but doesn't allow for passing in values
|
||||
```
|
||||
locals {
|
||||
extra_tag = "extra-tag"
|
||||
}
|
||||
```
|
||||
|
||||
# Output Variables
|
||||
|
||||
Allows you to output some value (which might not be known ahead of time).
|
||||
|
||||
For example it might be useful to know the IP address of a VM that was created:
|
||||
|
||||
```
|
||||
output "instance_ip_addr" {
|
||||
value = aws_instance.instance.private_ip
|
||||
}
|
||||
```
|
||||
|
||||
Sample output:
|
||||
```
|
||||
db_instance_addr = "terraform-20210504182745335900000001.cr2ub9wmsmpg.us-east-1.rds.amazonaws.com"
|
||||
instance_ip_addr = "172.31.24.95"
|
||||
```
|
||||
|
||||
Will be output after `terraform apply` or `terraform output`
|
||||
@@ -0,0 +1 @@
|
||||
instance_name = "hello-world-2"
|
||||
47
04-variables-and-outputs/examples/main.tf
Normal file
47
04-variables-and-outputs/examples/main.tf
Normal file
@@ -0,0 +1,47 @@
|
||||
terraform {
|
||||
backend "s3" {
|
||||
bucket = "devops-directive-tf-state"
|
||||
key = "04-variables-and-outputs/examples/terraform.tfstate"
|
||||
region = "us-east-1"
|
||||
dynamodb_table = "terraform-state-locking"
|
||||
encrypt = true
|
||||
}
|
||||
|
||||
required_providers {
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = "~> 3.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
locals {
|
||||
extra_tag = "extra-tag"
|
||||
}
|
||||
|
||||
resource "aws_instance" "instance" {
|
||||
ami = var.ami
|
||||
instance_type = var.instance_type
|
||||
|
||||
tags = {
|
||||
Name = var.instance_name
|
||||
ExtraTag = local.extra_tag
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_db_instance" "db_instance" {
|
||||
allocated_storage = 20
|
||||
storage_type = "gp2"
|
||||
engine = "postgres"
|
||||
engine_version = "12.4"
|
||||
instance_class = "db.t2.micro"
|
||||
name = "mydb"
|
||||
username = var.db_user
|
||||
password = var.db_pass
|
||||
skip_final_snapshot = true
|
||||
}
|
||||
|
||||
7
04-variables-and-outputs/examples/outputs.tf
Normal file
7
04-variables-and-outputs/examples/outputs.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
output "instance_ip_addr" {
|
||||
value = aws_instance.instance.private_ip
|
||||
}
|
||||
|
||||
output "db_instance_addr" {
|
||||
value = aws_db_instance.db_instance.address
|
||||
}
|
||||
3
04-variables-and-outputs/examples/terraform.tfvars
Normal file
3
04-variables-and-outputs/examples/terraform.tfvars
Normal file
@@ -0,0 +1,3 @@
|
||||
instance_name = "hello-world"
|
||||
ami = "ami-011899242bb902164" # Ubuntu 20.04 LTS // us-east-1
|
||||
instance_type = "t2.micro"
|
||||
30
04-variables-and-outputs/examples/variables.tf
Normal file
30
04-variables-and-outputs/examples/variables.tf
Normal file
@@ -0,0 +1,30 @@
|
||||
# should specify optional vs required
|
||||
|
||||
variable "instance_name" {
|
||||
description = "Name of ec2 instance"
|
||||
type = string
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
variable "db_user" {
|
||||
description = "username for database"
|
||||
type = string
|
||||
default = "foo"
|
||||
}
|
||||
|
||||
variable "db_pass" {
|
||||
description = "password for database"
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
Reference in New Issue
Block a user