본문 바로가기

IaC/Terraform

[Terraform]AWS Provider로 Ubuntu AWS EC2 Instance 만들기

개요

  • Terraform은 각 provider가 제공하는 많은 리소스와 상호작용할 수 있다.
  • Terraform Registry에서 원하는 provider를 찾은 뒤 사용 가능한 리소스를 정의하고 해당 리소스를 관리할 수 있다.
  • 여기서는 aws provider의 resource(aws_instanace)와 data source(aws_ami)를 이용하여 최신버전의 ec2 instanace를 기동시킨다.

Resource: aws_instance

ubuntu instance 만들기

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "ap-northeast-2"
}

# instanace 
resource "aws_instance" "jeff_tf_ubuntu" {
  ami           = "ami-0f0646a5f59758444"
  instance_type = "t2.micro"
  subnet_id = "subnet-004f0791a0c0c4e57"
  security_groups = [ "sg-07a2fa9eb741e42d6" ]
  tags = {
    Name = "jeff_tf_ubuntu"
  }
}

ami 찾기

EC2 → 인스턴스 → 인스턴스 시작 → AMI

  ami           = "ami-0f0646a5f59758444"

vpc, sg 설정

여기서는 subnet과 security groups는 콘솔에서 만든 후 코드에 입력하였다.

  subnet_id = "subnet-004f0791a0c0c4e57"
  security_groups = [ "sg-07a2fa9eb741e42d6" ]

terraform init && terraform apply

  • resource "aws_instance" "jeff_tf_ubuntu" 에서 설정한
    • instance type, vpc, sg를 가진 ubuntu ec2 instance가 생성된다.

Data Source: aws_ami

ubuntu ami 최신버전으로 가져오기

  • data source의 most_recent = true 옵션을 통해 ubuntu ec2 instance를 최신 버전으로 변경한다.

example

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "ap-northeast-2"
}

# resource - instanace 
resource "aws_instance" "jeff_tf_ubuntu" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"
  subnet_id = "subnet-004f0791a0c0c4e57"
  security_groups = [ "sg-07a2fa9eb741e42d6" ]
  tags = {
    Name = "jeff_tf_ubuntu_2"
  }
}

# data - aws_ami
data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}
  • most_recent = true
    • 가장 최신 ami

Attributes Reference 참고

  • image_id - ID of the AMI. Should be the same as the resource id.

진행 결과

  • 기존 ubunut를 삭제하고 최신의 ami를 받아와서 ec2를 띄운다
 terraform apply                                                                              main
data.aws_ami.ubuntu: Reading...
data.aws_ami.ubuntu: Read complete after 1s [id=ami-003bb1772f36a39a3]
aws_instance.jeff_tf_ubuntu: Refreshing state... [id=i-06a93d177fbc8fbf7]

Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # aws_instance.jeff_tf_ubuntu must be replaced
-/+ resource "aws_instance" "jeff_tf_ubuntu" {
      ~ ami                                  = "ami-0f0646a5f59758444" -> "ami-003bb1772f36a39a3" # forces replacement

# 중략

Plan: 1 to add, 0 to change, 1 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.jeff_tf_ubuntu: Destroying... [id=i-06a93d177fbc8fbf7]
aws_instance.jeff_tf_ubuntu: Still destroying... [id=i-06a93d177fbc8fbf7, 2m20s elapsed]
aws_instance.jeff_tf_ubuntu: Destruction complete after 2m21s
aws_instance.jeff_tf_ubuntu: Creating...
aws_instance.jeff_tf_ubuntu: Still creating... [10s elapsed]
aws_instance.jeff_tf_ubuntu: Still creating... [20s elapsed]
aws_instance.jeff_tf_ubuntu: Still creating... [30s elapsed]
aws_instance.jeff_tf_ubuntu: Still creating... [40s elapsed]
aws_instance.jeff_tf_ubuntu: Creation complete after 41s [id=i-03f387e4ea85fe036]