$ terraform init # Initializes the Terraform
$ terraform plan # Plans the current state and changes.
$ terraform apply # Applies changes to create or update infrastructure.
$ terraform destroy # Destroys the currently configured infrastructure.
주로 init → plan → apply 순으로 실행한다.
terraform init
.tf파일을 읽고필요한 Provider Plugin을 다운로드 설치한다.
✨ 새로운프로젝트를 시작하거나종속성이 변경된 경우에 주로 사용된다.
위 명령어를 실행하면 실행한 디렉토리에.terraform디렉토리를 생성하여 지정한 Provider에 해당하는 파일을 다운로드한다.
provider "aws" {
access_key = "자신의 ACCESS_KEY"
secret_key = "자신의 SECRET_KEY"
region = "ap-northeast-1" #자신의 지역
}
# 새로운 VPC생성
resource "aws_vpc" "jibin-example-vpc" {
cidr_block = "10.0.0.0/16"
}
실행 결과
PS > terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.12.0...
- Installed hashicorp/aws v5.12.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 plan
인프라스트럭처변경 사항을미리 확인하는 데 사용된다.
.tf파일로 작성한 인프라에 대한 생성 계획을 보여주며, 이를 통해 미리 변경 사항을 확인 할 수 있다.
terraform apply를 실행하기 전에 어떤 리소스가 생성, 변경 또는 삭제될지에 대한 정보가 포함된다.
PS> 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_vpc.myvpc will be created
+ resource "aws_vpc" "myvpc" {
+ arn = (known after apply)
+ cidr_block = "10.0.0.0/16"
+ default_network_acl_id = (known after apply)
+ default_route_table_id = (known after apply)
+ default_security_group_id = (known after apply)
+ dhcp_options_id = (known after apply)
+ enable_dns_hostnames = (known after apply)
+ enable_dns_support = true
+ enable_network_address_usage_metrics = (known after apply)
+ id = (known after apply)
+ instance_tenancy = "default"
+ ipv6_association_id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_block_network_border_group = (known after apply)
+ main_route_table_id = (known after apply)
+ owner_id = (known after apply)
+ tags_all = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
PS > 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_vpc.jibin-example-vpc will be created
+ resource "aws_vpc" "myvpc" {
+ arn = (known after apply)
+ cidr_block = "10.0.0.0/16"
+ default_network_acl_id = (known after apply)
+ default_route_table_id = (known after apply)
+ default_security_group_id = (known after apply)
+ dhcp_options_id = (known after apply)
+ enable_dns_hostnames = (known after apply)
+ enable_dns_support = true
+ enable_network_address_usage_metrics = (known after apply)
+ id = (known after apply)
+ instance_tenancy = "default"
+ ipv6_association_id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_block_network_border_group = (known after apply)
+ main_route_table_id = (known after apply)
+ owner_id = (known after apply)
+ tags_all = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:
terraform destroy
만들어진 인프라를 삭제한다
terraform apply와 반대로 삭제되는 리소스들을 조회한 후, yes를 입력하게 되면 삭제를 진행한다.