CloudFormation: AWS 리소스를 모델링하고 설정하여 리소스 관리 시간을 줄이고 AWS에서 실행되는 애플리케이션에 더 많은 시간을 사용하도록 해 주는 서비스. AWS에만 적용 가능한 IaC 도구
AWS - CloudFormation 검색 - 스택 생성 - 준비된 템플릿 - EC2 템플릿 코드 조각 검색 - YAML 형식
AWSTemplateFormatVersion: 2010-09-09
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-068a0feb96796b48d // 원하는 이미지, 현재는 우분투
KeyName: cloudcamp // 자신의 키 이름
InstanceType: t2.micro
SecurityGroups:
- default
BlockDeviceMappings:
-
DeviceName: /dev/sda1
Ebs:
VolumeSize: 8
- 파일 업로드 - 스택생성 - 자동으로 인스턴스 생성됨 - 스택 삭제시 해당 스택으로 생성된 모든 리소스도 같이 삭제 됨!(설정으로 막을 수 있음)
Terraform:
Local 코드 : 현재 개발자가 작성/수정하고 있는 코드
AWS 실제 인프라 : 실제로 AWS에 배포되어 있는 인프라
Backend에 저장된 상태 : 가장 최근에 배포한 테라폼 코드 형상
테라폼은 실제 인프라와 Backend에 저장된 상태를 일치하도록 만드는 프로그램
[1] Terraform init
terraform init 명령어는 프로젝트를 terraform을 구동할 수 있는 환경으로 만들어준다.
보통 provider가 작성된 main.tf가 존재하는 경로에서 사용된다. terraform 최초 구동 시 반드시 필요한 명령어이다.
[2] terraform plan
현재 작성된 terraform 코드로 생성되고 변경될 내역을 보여준다.
실제 환경에 적용하기 전 검증할 수 있게 하는 매우 중요한 명령어이다.
또한 terraform 코드의 문법적 오류가 없는지도 검증할 수 있다.
[3] terraform apply
terraform apply는 실제로 인프라스트럭처를 구성한다.
[4] terraform destroy
구성했던 인프라스트럭처의 resource를 모두 회수한다.
provider "aws" {
region = "ap-northeast-2" // 서울 리전
}
provider는 작성 후, 밑에 리소스 및 변수 작성
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
resource "리소스타입" "리소스이름" {
속성이름 = "속성값"
}
[1]
variable "변수 이름" {
type = 변수 타입
default = "변수 값"
}
일반적으로 변수 코드를 저장하는 variables.tf를 따로 생성하여 관리
[2]
terraform apply -var "변수 이름=변수 값"로 변수 값 변경 가능
일반적으로 변수 코드를 저장하는 terraform.tfvars를 따로 생성하여 관리
output "이름" {
description = "설명"
value = 출력하고 싶은 값 // ex) aws_instance.app_server.public_ip
}
원하는 리소스를 미리 지정해 둔 다음, 모듈로 해당 리소스 모음을 실행
module "모듈이름" {
source = "템플릿이 될 리소스가 있는 파일의 경로"
변수이름 = "값"
}
1) 테라폼 설치 및 압축 풀기
https://www.terraform.io/downloads
2) 환경변수 설정
내PC 속성 - 고급 시스템 설정 - 환경변수 - 시스템 변수의 path 편집 - terraform 경로 추가
3) AWS CLI 설치 (windows)
https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/getting-started-install.html
1) AWS 인증 정보 설정
2) VScode 다운(메모장 대체)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_instance" "app_server" {
ami = "ami-068a0feb96796b48d"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
커밋은 실제 코드만!
(main.tf)
provider "aws" {
region = "ap-northeast-2"
}
[1] vpc 생성
resource "aws_vpc" "my-vpc2" {
cidr_block = "200.200.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = true // public ip 할당을 가능하게 함
tags = {
Name = "my-vpc2"
}
}
[2] 서브넷 생성
resource "aws_subnet" "my2-subnet-1" {
vpc_id = aws_vpc.my-vpc2.id
cidr_block = "200.200.10.0/24"
availability_zone = "ap-northeast-2a"
map_public_ip_on_launch = true // public ip를 자동으로 할당
tags = {
Name = "my2-subnet-1"
}
}
[3] 인터넷 게이트 웨이 생성
resource "aws_internet_gateway" "my-gw2" {
vpc_id = aws_vpc.my-vpc2.id
tags = {
Name = "my-gw2"
}
}
[4] 라우팅 테이블 수정
resource "aws_default_route_table" "my-rt2" {
default_route_table_id = aws_vpc.my-vpc2.default_route_table_id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my-gw2.id
}
tags = {
Name = "my-rt2"
}
}
[5] 보안 그룹 생성
resource "aws_security_group" "my-sg2" {
name = "my-sg2"
description = "Allow SSH inbound traffic"
vpc_id = aws_vpc.my-vpc2.id
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "my-sg2"
}
}
[6] EC2 인스턴스 생성
resource "aws_instance" "my-ec2" {
ami = "ami-068a0feb96796b48d"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.my-sg2.id]
subnet_id = aws_subnet.my2-subnet-1.id
key_name = "cloudcamp"
}
[7] public ip 출력
output "my_ec2_public_ip" {
value = aws_instance.my-ec2.public_ip
}