devops:terraform
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
devops:terraform [2023/11/01 07:15] – removed - external edit (Unknown date) 127.0.0.1 | devops:terraform [2024/04/03 19:53] (current) – skipidar | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== 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 | ||
+ | https:// | ||
+ | |||
+ | Introduction: | ||
+ | https:// | ||
+ | |||
+ | Skeleton project | ||
+ | https:// | ||
+ | |||
+ | == Apply terraform bash script == | ||
+ | <sxh bash> | ||
+ | |||
+ | #!/bin/bash | ||
+ | set -eo pipefail | ||
+ | |||
+ | if [[ ! -d " | ||
+ | then | ||
+ | terraform init | ||
+ | fi | ||
+ | |||
+ | terraform validate | ||
+ | terraform plan | ||
+ | |||
+ | #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 === | ||
+ | |||
+ | Create " | ||
+ | <sxh json> | ||
+ | |||
+ | variable " | ||
+ | description = "The AWS region to deploy the resources into" | ||
+ | type = string | ||
+ | default = " | ||
+ | } | ||
+ | |||
+ | 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 | ||
+ | <sxh json> | ||
+ | |||
+ | provider " | ||
+ | profile = " | ||
+ | region | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | === 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 = " | ||
+ | ] | ||
+ | </ | ||