devops:terraform
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
devops:terraform [2021/05/26 18:36] – skipidar | devops:terraform [2024/04/03 19:53] (current) – skipidar | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Terraform ===== | ===== Terraform ===== | ||
+ | |||
+ | ===Debugging=== | ||
+ | |||
+ | |||
+ | <sxh shell> | ||
+ | # https:// | ||
+ | # investigage errors | ||
+ | export TF_LOG=" | ||
+ | export TF_LOG_PATH=" | ||
+ | </ | ||
+ | |||
+ | ===Glossary=== | ||
+ | |||
+ | | Terraform Module |A Terraform module is a set of Terraform configuration files in a single directory.| | ||
+ | |||
+ | |||
Installation | Installation | ||
Line 7: | Line 23: | ||
https:// | https:// | ||
+ | Skeleton project | ||
+ | https:// | ||
- | + | == Apply terraform bash script | |
- | Apply terraform bash script | + | |
<sxh bash> | <sxh bash> | ||
Line 24: | Line 41: | ||
#terraform apply | #terraform apply | ||
+ | </ | ||
+ | == Upgrade terraform provider == | ||
+ | https:// | ||
+ | |||
+ | <sxh bash> | ||
+ | |||
+ | terraform init -upgrade | ||
+ | |||
+ | Initializing the backend... | ||
+ | |||
+ | Initializing provider plugins... | ||
+ | - Finding hashicorp/ | ||
+ | - Installing hashicorp/ | ||
+ | - Installed hashicorp/ | ||
+ | |||
+ | Terraform has made some changes to the provider dependency selections recorded | ||
+ | in the .terraform.lock.hcl file. Review those changes and commit them to your | ||
+ | version control system if they represent changes you intended to make. | ||
+ | |||
+ | Terraform has been successfully initialized! | ||
</ | </ | ||
+ | === Main === | ||
+ | Create " | ||
+ | <sxh json> | ||
+ | terraform { | ||
+ | required_providers { | ||
+ | aws = { | ||
+ | source | ||
+ | version = "~> 3.0" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | provider " | ||
+ | profile = " | ||
+ | region | ||
+ | } | ||
+ | |||
+ | </ | ||
=== Variables === | === Variables === | ||
- | Create " | + | Create "**variables.tf**" |
<sxh json> | <sxh json> | ||
- | variable "region" { | + | variable "aws_region" { |
- | description = "Value of the Name tag for the EC2 instance" | + | description = "The AWS region to deploy |
type = string | type = string | ||
- | default = "eu-west-1" | + | default = "eu-central-1" |
} | } | ||
+ | |||
+ | variable " | ||
+ | description = "The AWS account identifier of the project" | ||
+ | type = string | ||
+ | default = " | ||
+ | } | ||
+ | |||
+ | variable " | ||
+ | description = "The resource prefix" | ||
+ | type = string | ||
+ | default = " | ||
+ | } | ||
+ | |||
+ | |||
+ | locals { | ||
+ | iot_policy = " | ||
+ | } | ||
+ | |||
</ | </ | ||
+ | |||
+ | **locals** | ||
+ | Only here one can combine other variables | ||
Use the variable | Use the variable | ||
Line 53: | Line 129: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | |||
+ | === Data === | ||
+ | |||
+ | When you define '' | ||
+ | |||
+ | <sxh json> | ||
+ | |||
+ | </ | ||
+ | |||
+ | You can apply filters. | ||
+ | |||
+ | <sxh json> | ||
+ | |||
+ | # Find the latest available AMI that is tagged with Component = web | ||
+ | data " | ||
+ | filter { | ||
+ | name = " | ||
+ | values = [" | ||
+ | } | ||
+ | |||
+ | filter { | ||
+ | name = " | ||
+ | values = [" | ||
+ | } | ||
+ | |||
+ | most_recent = true | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Create **templates.tf** | ||
+ | |||
+ | <sxh json> | ||
+ | |||
+ | data " | ||
+ | vars = { | ||
+ | aws_region = " | ||
+ | aws_account_id = " | ||
+ | } | ||
+ | template = <<EOF | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | EOF | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Usage | ||
+ | <sxh json> | ||
+ | resource " | ||
+ | name = " | ||
+ | policy = " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Dynamic block === | ||
+ | |||
+ | See https:// | ||
+ | |||
+ | To replace the repetitive code as here in a module: | ||
+ | <sxh json> | ||
+ | resource " | ||
+ | name = " | ||
+ | resource_group_name = azurerm_resource_group.dynamic_block.name | ||
+ | location | ||
+ | address_space | ||
+ | |||
+ | subnet { | ||
+ | name = " | ||
+ | address_prefix = " | ||
+ | } | ||
+ | |||
+ | subnet { | ||
+ | name = " | ||
+ | address_prefix = " | ||
+ | } | ||
+ | |||
+ | subnet { | ||
+ | name = " | ||
+ | address_prefix = " | ||
+ | } | ||
+ | |||
+ | subnet { | ||
+ | name = " | ||
+ | address_prefix = " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Use the " | ||
+ | |||
+ | <sxh json> | ||
+ | resource " | ||
+ | name = " | ||
+ | resource_group_name = azurerm_resource_group.dynamic_block.name | ||
+ | location | ||
+ | address_space | ||
+ | |||
+ | dynamic " | ||
+ | for_each = var.subnets | ||
+ | iterator = item # | ||
+ | content { | ||
+ | name = item.value.name | ||
+ | address_prefix = item.value.address_prefix | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | Declare a variable in your module | ||
+ | <sxh json> | ||
+ | variable " | ||
+ | description = "list of values to assign to subnets" | ||
+ | type = list(object({ | ||
+ | name = string | ||
+ | address_prefix = string | ||
+ | })) | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | **USAGE of your module** | ||
+ | |||
+ | Assigning values to the variable " | ||
+ | |||
+ | <sxh json> | ||
+ | subnets = [ | ||
+ | { name = " | ||
+ | { name = " | ||
+ | { name = " | ||
+ | { name = " | ||
+ | ] | ||
+ | </ | ||
+ |
devops/terraform.1622054185.txt.gz · Last modified: by skipidar