Skip to main content

Terraform basics


Installation

AWS

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
  • AWS Access Key ID and Access Key: IAM -> 사용자 -> <user> -> 보안 자격 증명 -> 액세스 키 만들기
aws configure --profile <profile>

S3

  • ACL 비활성화됨
  • 모든 퍼블릭 액세스 차단
  • 버킷 버전 관리 활성화
  • 기본 암호화 활성화

Terraform

terraform -install-autocomplete

Tutorial

mkdir test && cd test
main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.8.0"
}
}

required_version = ">= 1.0.0" # `terraform version`

backend "s3" {
bucket = "<bucket>"
key = "<path>/terraform.tfstate"
region = "us-west-2"
dynamodb_table = "<terraform-table>-locks" # LockID: <bucket>/<key>-md5
encrypt = true
}
}

provider "aws" {
region = "us-west-2"
profile = "<profile>"
}
terraform init
ec2.tf
resource "aws_instance" "app_server" {
ami = "ami-090717c950a5c34d3"
instance_type = "t3.micro"
tags = {
"Name" = "app_server"
}
}

output "instance_id" {
value = aws_instance.app_server.id
}

output "instance_public_ip" {
value = aws_instance.app_server.public_ip
}
terraform refresh
terraform apply
terraform destroy

Reference