Caption: 테라폼 인프라 배포 동작 순서
① 프로젝트 범위를 식별하고 HCL 언어로 생성할 인프라를 선언합니다.
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "example" {
ami = "ami-0c76973fbe0ee100c"
instance_type = "t2.micro"
}
...
.tf
파일 생성② terraform init
명령어로 테라폼을 시작하기 위한 준비를 진행합니다. (backend 구성 초기화, 플러그인 설치 등)
$ **terraform init**
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v4.38.0...
- Installed hashicorp/aws v4.38.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
.terraform
폴더가 생성되며 관련한 플러그인을 가져옵니다..terraform.lock.hcl
파일을 생성합니다.③ terraform plan
명령어로 변경될 인프라 내역을 보여줍니다.
$ **terraform plan**
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:
# aws_instance.example will be created
+ resource "aws_instance" "example" {
+ ami = "ami-0c76973fbe0ee100c"
+ arn = (known after apply)
+ associate_public_ip_address = (known after apply)
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ disable_api_stop = (known after apply)
+ disable_api_termination = (known after apply)
+ ebs_optimized = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ host_resource_group_arn = (known after apply)
+ id = (known after apply)
+ instance_initiated_shutdown_behavior = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro"
+ ipv6_address_count = (known after apply)
...
terraform plan -out [FILE_NAME]
형태로 저장이 가능하며, terraform apply [FILE_NAME]
형태로 지정하여 배포할 수 있습니다.④ terraform apply
명령어로 인프라를 배포합니다.
$ **terraform apply**
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:
# aws_instance.example will be created
+ resource "aws_instance" "example" {
+ ami = "ami-0c76973fbe0ee100c"
...
Plan: 1 to add, 0 to change, 0 to destroy.
aws_instance.example: Creating...
aws_instance.example: Still creating... [10s elapsed]
aws_instance.example: Still creating... [20s elapsed]
aws_instance.example: Still creating... [30s elapsed]
aws_instance.example: Still creating... [40s elapsed]
aws_instance.example: Creation complete after 41s [id=i-0932856b42695f556]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
terraform plan
에서 계획된 행위를 수행합니다..tfstate
파일로 저장합니다.