리소스는 Terraform 언어에서 가장 중요한 요소이다. 각 리소스 블록은 가상 네트워크, 컴퓨팅 인스턴스 또는 DNS 레코드와 같은 상위 수준 구성 요소와 같은 하나 이상의 인프라 개체를 설명한다.
Resource Syntax
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}
여기서 “aws_instance” 는 aws provider에 의해 정해진 EC2 resource type 이고 web은 인스턴스의 이름을 나타냅니다. Terraform 코드 내에서 인스턴스를 참조하는 데 사용됩니다. “ami” 와 “instance_type”은 Resource Argument 입니다.
Resource Argument
Resource Argument 는 선택한 resource type에 따라 다릅니다. 모든 Terraform 제공자는 리소스 유형과 해당 인수를 설명하는 자체 문서를 가지고 있습니다.
https://registry.terraform.io/browse/providers
Custom Condition Checks (사용자 조건 확인)
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."
}
}
}
사용자 지정 조건은 리소스가 제대로 구성되었는지 확인하여 구성의 문제를 쉽게 진단할 수 있도록 도와줍니다.
Operation Timeouts (작업 시간 초과)
resource "aws_db_instance" "example" {
# ...
timeouts {
create = "60m"
delete = "2h"
}
}
일부 리소스 유형은 특정 작업이 실패한 것으로 간주되기 전에 허용하는 시간을 사용자 지정할 수 있는 중첩 블록 인수를 제공합니다.