마지막 단계에서는 Terraform 구성을 실행하고 변수에 값을 제공합니다.
먼저, Terraform 작업 디렉터리를 초기화해야 합니다. terraform init 명령어는 구성에 정의된 프로바이더 (이 경우 local 프로바이더) 를 다운로드하고 설치합니다.
다음 명령어를 실행합니다.
terraform init
Terraform 이 성공적으로 초기화되었음을 나타내는 출력을 보게 될 것입니다.
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/local versions matching "2.4.0"...
- Installing hashicorp/local v2.4.0...
- Installed hashicorp/local v2.4.0 (signed by HashiCorp)
Terraform has been successfully initialized!
...
이제 구성을 적용할 수 있습니다. 변수에 값을 전달하려면 -var 명령줄 플래그를 사용할 수 있습니다. 또한 대화형 확인 프롬프트를 건너뛰기 위해 -auto-approve 플래그도 사용할 것입니다.
filename과 content에 대한 값을 사용하여 terraform apply 명령어를 실행합니다.
terraform apply -var="filename=hello.txt" -var="content=Hello, Terraform Variables." -auto-approve
Terraform 은 이제 계획을 실행하고 파일을 생성합니다. 출력은 다음과 유사할 것입니다.
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
## local_file.my_file will be created
+ resource "local_file" "my_file" {
+ content = "Hello, Terraform Variables."
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "hello.txt"
+ id = "..."
}
Plan: 1 to add, 0 to change, 0 to destroy.
local_file.my_file: Creating...
local_file.my_file: Creation complete after 0s [id=...]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
모든 것이 제대로 작동했는지 확인하려면 cat 명령어를 사용하여 새로 생성된 파일의 내용을 표시합니다.
cat hello.txt
변수에 제공한 내용을 보게 될 것입니다.
Hello, Terraform Variables.
축하합니다! Terraform 변수를 성공적으로 사용하여 파일을 생성했습니다.