.

 

테라폼 windows 64bit 버전 다운로드 받아 사용

https://www.terraform.io/downloads.html

 

 

D:\Util\terraform>terraform -version
Terraform v0.12.27

 

같은 폴더에 <mytest.tf> 파일 생성

provider "aws" {
  access_key = "ACCESS KEY"
  secret_key = "SECRET KEY"
  region     = "ap-northeast-2"
}

resource "aws_instance" "example" {
  ami           = "ami-f293459c"
  instance_type = "t2.micro"
}

 

D:\util\terraform> terraform init

tf파일을 확인하여 필요한 provider를 다운받는다. 여기서는 aws provider가 필요 함.

- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.67.0...

 

 

D:\util\terraform> terraform plan

tf 파일이 수행되면 어떤 변경들이 발생하는지 보여준다.

명령 실행 후 할당되는 정보들은 (known after apply)라고 표시되는것을 알 수 있다.

Refreshing Terraform state in-memory prior to plan... 
The refreshed state will be used to calculate this plan, but will not be 
persisted to local or remote state storage. 

------------------------------------------------------------------------ 

An execution plan has been generated and is shown below. 
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-f293459c" 
      + arn                          = (known after apply) 
      + associate_public_ip_address  = (known after apply) 
      + availability_zone            = (known after apply) 

~~~~ 생략 ~~~~ 

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

------------------------------------------------------------------------ 

Note: You didn't specify an "-out" parameter to save this plan, so Terraform 
can't guarantee that exactly these actions will be performed if 
"terraform apply" is subsequently run.

 

 

D:\util\terraform> terraform apply

tf 파일을 적용한다. 기본적으로 "yes"라고 직접 타이핑 해야만 진행되도록 안전장치가 되어 있다.

An execution plan has been generated and is shown below.
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-f293459c"
      + arn                          = (known after apply)
      + associate_public_ip_address  = (known after apply)
      + availability_zone            = (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: yes

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: Creation complete after 31s [id=i-0ad240b918ae149aa]

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

 

이제 AWS 콘솔에 가서 보면 t2.micro 타입의 EC2가 생성된 것을 확인할 수 있다.

 

명령을 수행하고 나면 <terraform.tfstate> 파일이 자동으로 생성되는데 방금 생성한 EC2 정보를 여기서 확인할 수 있다.

이 상태 파일은 테라폼이 어떤 리소스를 관리하는지 알 수 있도록 하므로 여러 사람과 작업할 때 함께 공유되어야 한다.

이 정보는 다음 명령을 통해서도 확인 가능하다.

 

D:\util\terraform> terraform show

# aws_instance.example:
resource "aws_instance" "example" {
    ami                          = "ami-f293459c"
    arn                          = "arn:aws:ec2:ap-northeast-2:277773220743:instance/i-0ad240b918ae149aa"
    associate_public_ip_address  = true
    availability_zone            = "ap-northeast-2c"
    cpu_core_count               = 1
    cpu_threads_per_core         = 1
    disable_api_termination      = false
    ebs_optimized                = false
    get_password_data            = false
    hibernation                  = false
    id                           = "i-0ad240b918ae149aa"
    instance_state               = "running"
    instance_type                = "t2.micro"
    ipv6_address_count           = 0
    ipv6_addresses               = []
    monitoring                   = false
    primary_network_interface_id = "eni-03fe16619d143d4e6"
    private_dns                  = "ip-172-31-46-213.ap-northeast-2.compute.internal"
    private_ip                   = "172.31.46.213"
    public_dns                   = "ec2-15-164-211-149.ap-northeast-2.compute.amazonaws.com"
    public_ip                    = "15.164.211.149"
    security_groups              = [
        "default",
    ]
    source_dest_check            = true
    subnet_id                    = "subnet-220a546e"
    tenancy                      = "default"
    volume_tags                  = {}
    vpc_security_group_ids       = [
        "sg-0c01d362",
    ]

~~~~ 이하생략 ~~~~

 

생성했던 인스턴스를 제거하기 위해서는 다음 명령을 수행한다. 생성할때와 마찬가지로 "yes"라고 직접 타이핑 해야만 진행되도록 안전장치가 되어 있다.

D:\util\terraform> terraform destroy

aws_instance.example: Refreshing state... [id=i-0ad240b918ae149aa]
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

 

 

 

== 참고

terraform : https://www.terraform.io/

테라폼 기초 튜토리얼 : https://www.44bits.io/ko/post/terraform_introduction_infrastrucute_as_code

테라폼101 : https://mooyoul.github.io/2016/12/19/Terraform-101/

 

AND