Terraform EC2(인스턴스) 생성

[Tool]Terraform EC2생성

Terraform으로 EC2 Instance생성

  • terraform은 유저 디렉토리 이하의 [*.tf]파일을 읽어 들여 EC2를 생성한다.
  • terraform으로 생성한EC2(인스턴스)는 terraform.tfstate파일 내에 저장된다.
  • terraform.tfstate(Default)는 Terraform쪽에서 AWS의 설정을 파악하기 위한 파일이므로 중요하다.

EC2생성 예시(CentOS 2016.2.15기준)

  • Group으로 관리하기 위해 유저 디렉토리를 생성(terraform의 설치 디렉토리 이하)

$ mkdir aws-test
$ cd aws-test

  • aws-test.tf 파일 생성

$ vi aws-test.tf

# Create EC2 Instance

provider "aws" {
    access_key = "[IAM Access_key]"
    secret_key = "[IAM Secret_key]"
    region = "ap-northeast-1"
}

resource "aws_instance" "example" {
  ami = "[amiID]"
  instance_type = "t2.micro"
}
:wq
  • 작성한 파일 확인(테라폼에서 에러 발생 여부를 확인하는 과정)

$ terraform plan

Refreshing Terraform state prior to plan...


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_instance.example
    ami:                      "" => "[ami-id]"
    availability_zone:        "" => "<computed>"
    ebs_block_device.#:       "" => "<computed>"
    ephemeral_block_device.#: "" => "<computed>"
    instance_state:           "" => "<computed>"
    instance_type:            "" => "t2.micro"
    key_name:                 "" => "<computed>"
    placement_group:          "" => "<computed>"
    private_dns:              "" => "<computed>"
    private_ip:               "" => "<computed>"
    public_dns:               "" => "<computed>"
    public_ip:                "" => "<computed>"
    root_block_device.#:      "" => "<computed>"
    security_groups.#:        "" => "<computed>"
    source_dest_check:        "" => "1"
    subnet_id:                "" => "<computed>"
    tenancy:                  "" => "<computed>"
    vpc_security_group_ids.#: "" => "<computed>"


Plan: 1 to add, 0 to change, 0 to destroy.
  • 작성한 내용 적용

$ terraform apply

aws_instance.example: Creating...
  ami:                      "" => "[ami-id]"  availability_zone:        "" => "<computed>"
  ebs_block_device.#:       "" => "<computed>"
  ephemeral_block_device.#: "" => "<computed>"
  instance_state:           "" => "<computed>"
  instance_type:            "" => "t2.micro"
  key_name:                 "" => "<computed>"
  placement_group:          "" => "<computed>"
  private_dns:              "" => "<computed>"
  private_ip:               "" => "<computed>"
  public_dns:               "" => "<computed>"
  public_ip:                "" => "<computed>"
  root_block_device.#:      "" => "<computed>"
  security_groups.#:        "" => "<computed>"
  source_dest_check:        "" => "1"
  subnet_id:                "" => "<computed>"
  tenancy:                  "" => "<computed>"
  vpc_security_group_ids.#: "" => "<computed>"
aws_instance.example: Creation complete

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

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate
  • AWS Console에서 EC2생성을 확인

댓글