Terraform 설치 및 명령어

Louis·2022년 11월 10일
0

Terraform

목록 보기
2/6

테라폼 설치 및 구성

AWS CLI설치

MacOS

$ which aws
/usr/local/bin/aws

Terraform 설치

MacOS

  • Homebrew로 설치
$ brew install terraform
$ terraform --version
Terraform v1.3.4
on darwin_arm64

테라폼 기본 명령어

Terraform init

  • 리소스 생성에 앞서, 현재 티렉토리을 Terraform 프로젝트용으로 만들어 준다
  • 'terraform init' 명령어 설정한 provider 플러그인을 다운로드 한다.```
$ 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 plan

  • 실제 리소스 생성에 앞서 terraform 파일에 문법 등의 문제가 없는지 검토 할 필요가 있다.
  • 'terraform plan' 명령어를 시용하면 아래와 같이 어떤 리소스가 추가되고, 어떤 값을 갖는지 예측 할 수 있다.
$ terraform plan
var.server_port
  The port the server will use for HTTP requests

  Enter a value: 8080


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:
-------
-------
  + resource "aws_security_group" "instance" {
      + arn                    = (known after apply)
      + description            = "Managed by Terraform"
      + egress                 = (known after apply)
      + id                     = (known after apply)
      + ingress                = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = ""
              + from_port        = 8080
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "tcp"
              + security_groups  = []
              + self             = false
              + to_port          = 8080
            },
        ]
      + name                   = "terraform-my-instance"
      + name_prefix            = (known after apply)
      + owner_id               = (known after apply)
      + revoke_rules_on_delete = false
      + tags_all               = (known after apply)
      + vpc_id                 = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + public_ip = (known after apply)

Terraform apply

  • 'terraform plan' 명령어에서 에러가 없었다면, 다음은 실제로 리소스를 생성한다.
  • 'terraform apply'명령어를 사용하여 tf 파일을 클라우드에 반영을 한다.
  • 변경 & 추가 & 삭제되는 리소스를 확인하고 'yes'를 입력하여 반영한다.
$ terraform apply
var.server_port
  The port the server will use for HTTP requests

  Enter a value: 8080


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_security_group.instance will be created
  + resource "aws_security_group" "instance" {
      + arn                    = (known after apply)
      + description            = "Managed by Terraform"
      + egress                 = (known after apply)
      + id                     = (known after apply)
      + ingress                = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = ""
              + from_port        = 8080
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "tcp"
              + security_groups  = []
              + self             = false
              + to_port          = 8080
            },
        ]
      + name                   = "terraform-my-instance"
      + name_prefix            = (known after apply)
      + owner_id               = (known after apply)
      + revoke_rules_on_delete = false
      + tags_all               = (known after apply)
      + vpc_id                 = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + public_ip = (known after apply)

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_security_group.instance: Creating...
aws_security_group.instance: Creation complete after 2s [id=sed48]
aws_instance.example: Creating...
aws_instance.example: Still creating... [10s elapsed]
aws_instance.example: Still creating... [20s elapsed]
aws_instance.example: Creation complete after 21s [id=34]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Outputs:

Terraform destroy

  • 'terraform destroy' 명령어를 사용하여, 모든 리소스를 제거할 수 있다.
  • 'terraform apply'와 마찬가지로 사용자의 의사를 확인하는 프롬프트에서 'yes'를 입력한다.
$ terraform destroy
var.server_port
  The port the server will use for HTTP requests

  Enter a value: 8080

aws_security_group.instance: Refreshing state... [id=ed48]
aws_instance.example: Refreshing state... [id=i-e34]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  - destroy

Terraform will perform the following actions:
------
------
# aws_security_group.instance will be destroyed
  - resource "aws_security_group" "instance" {
      - arn                    = "arurity-group/sg-0" -> null
      - description            = "Managed by Terraform" -> null
      - egress                 = [] -> null
      - id                     = "sg-48" -> null
      - ingress                = [
          - {
              - cidr_blocks      = [
                  - "0.0.0.0/0",
                ]
              - description      = ""
              - from_port        = 8080
              - ipv6_cidr_blocks = []
              - prefix_list_ids  = []
              - protocol         = "tcp"
              - security_groups  = []
              - self             = false
              - to_port          = 8080
            },
        ] -> null
      - name                   = "terraform-my-instance" -> null
      - owner_id               = "" -> null
      - revoke_rules_on_delete = false -> null
      - tags                   = {} -> null
      - tags_all               = {} -> null
      - vpc_id                 = "v721" -> null
    }

Plan: 0 to add, 0 to change, 2 to destroy.

Changes to Outputs:
  - public_ip = "1" -> null

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.example: Destroying... [id=iae34]
aws_instance.example: Still destroying... [id=i-0b34, 10s elapsed]
aws_instance.example: Still destroying... [id=i-0b34, 20s elapsed]
aws_instance.example: Still destroying... [id=i-0b34, 30s elapsed]
aws_instance.example: Still destroying... [id=i-0bae34, 40s elapsed]
aws_instance.example: Destruction complete after 50s
aws_security_group.instance: Destroying... [id=sg-0d48]
aws_security_group.instance: Destruction complete after 0s

Destroy complete! Resources: 2 destroyed.
profile
인프라 운영하는 개발자

0개의 댓글