Last active
May 8, 2024 07:27
-
-
Save huynhbaoan/1cc29bc8c0426e4cdf89e6654e34fcf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## I. Important references | |
https://developer.hashicorp.com/terraform/language/ | |
https://developer.hashicorp.com/terraform/cloud-docs | |
https://registry.terraform.io/providers/hashicorp/aws/latest/docs | |
### 1. Some points | |
Terraform HCL is similar to C syntax in some ways | |
- Comment: #, //, /* ... */ | |
- Block: <type> <label> { ... } | |
- As I understand: | |
- Block: <primary/terraform type> <secondary/provider type> <label> { ... } | |
resource "terraform_data" "replacement" { | |
input = var.revision | |
} | |
### 2. Useful tips | |
#### a. Remove a resource from your Terraform configuration without destroying the real infrastructure object it manages | |
removed { | |
from = aws_instance.example | |
lifecycle { | |
destroy = false | |
} | |
} | |
### 2. Example: | |
#### Resource block | |
resource "aws_instance" "web" { | |
ami = "ami-a1b2c3d4" | |
instance_type = "t2.micro" | |
} | |
#### Accessing Resource Attributes | |
<RESOURCE TYPE>.<NAME>.<ATTRIBUTE> | |
### Meta-arguments | |
#### Lifecycle | |
https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle | |
Within Resource block | |
#### create_before_destroy (bool) | |
resource "azurerm_resource_group" "example" { | |
# ... | |
lifecycle { | |
create_before_destroy = true | |
} | |
} | |
#### prevent_destroy (bool) | |
#### ignore_changes (list of attribute names) | |
resource "aws_instance" "example" { | |
# ... | |
lifecycle { | |
ignore_changes = [ | |
# Ignore changes to tags, e.g. because a management agent | |
# updates these based on some ruleset managed elsewhere. | |
tags, | |
] | |
} | |
} | |
#### replace_triggered_by (list of resource or attribute references) | |
Useful when we need constrains between resources | |
resource "aws_appautoscaling_target" "ecs_target" { | |
# ... | |
lifecycle { | |
replace_triggered_by = [ | |
# Replace `aws_appautoscaling_target` each time this instance of | |
# the `aws_ecs_service` is replaced. | |
aws_ecs_service.svc.id | |
] | |
} | |
} | |
#### Custom Condition Checks: precondition & postcondition | |
resource "aws_instance" "example" { | |
instance_type = "t2.micro" | |
ami = "ami-abc123" | |
lifecycle { | |
# The AMI ID must refer to an AMI that contains an operating system | |
# for the `x86_64` architecture. | |
precondition { | |
condition = data.aws_ami.example.architecture == "x86_64" | |
error_message = "The selected AMI must be for the x86_64 architecture." | |
} | |
} | |
} | |
data "aws_ami" "example" { | |
id = var.aws_ami_id | |
lifecycle { | |
# The AMI ID must refer to an existing AMI that has the tag "nomad-server". | |
postcondition { | |
condition = self.tags["Component"] == "nomad-server" | |
error_message = "tags[\"Component\"] must be \"nomad-server\"." | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment