TOPIC/Code

[Terraform] 전체 코드 중 특정 resource/module만 apply하기

H-Y-E-N 2023. 12. 15. 18:00

안녕하세요. HYEN입니다.

이번에는 Terraform 공부 중 알게된 전체 코드 중 특정 리소스만 apply하는 방법에 대해 알아보도록 하겠습니다. 😝


Contents

    1. terraform state list 

    1-1 terraform state list [options] [address...] 명령어 설명

    • address가 있는 경우, 주어진 조건과 일치하는 상태 파일의 모든 리소스를 나열합니다.
    • address가 없다면 상태 파일의 모든 리소스가 나열됩니다.
    • 모듈 depth에 따라 알파벳 순으로 정렬됩니다.
    • 코드가 복잡한 경우 수천 개의 결과가 출력될 수 있기 때문에 하나 이상의 pattern을 사용하는 것을 권장합니다.
    • resource address 형식은 다음과 같습니다. 
      • module
        • [module path][resource spec]
        • module.module_name[module index]
      • resource
        • resource_type.resource_name[instance index]

    1-2 terraform state list 명령어 실행 결과

    • 해당 명령어를 입력할 경우 전체 resource address list가 출력됩니다. 

    1-3 terraform state list module.fw 명령어 실행 결과

    2. terraform apply -target [resource address] 

    2-1 terraform apply -target [resource address] 명령어 설명 

    • [resource address]에 해당하는 target에 대해서만 apply 명령어를 실행할 수 있습니다. 

    2-2 terraform apply -target [resource address] 명령어 실행 결과

    • 예시로 terraform apply -target module.fw.azurerm_firewall_policy_rule_collection_group.rule-collection-group 명령어 실행를 실행해 보았습니다. (Azure Firewall 정책 중 NAT Rule의 Source IP 변경)

    • "Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols ... Terraform will perform the following actions ~"에서 알 수 있듯이 해당 명령어를 입력하면 -target flag 뒤에 명시한 resource address에 대해서만 apply가 실행됩니다.
    • 해당 명령어는 Warning 문구를 확인하여 예외적인 경우에만 사용할 것을 권장하고 있으며 경고 문구 상세는 하기와 같습니다.

    Warning: Resource targeting is in effect
    You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current configuration.

    The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.


    이상으로 Terraform 코드 중 일부 resource에 대해서만 apply하는 방법에 대해 아주 간단히 알아보았습니다! 

    728x90
    320x100
    SMALL

    'TOPIC > Code' 카테고리의 다른 글

    #01 REST API - 개념과 보안  (0) 2024.04.23