Terraform 이란?
코드형 인프라(IaC) 접근 방식을 통해 선언적 구성 파일(가상 머신, 컨테이너, 스토리지, 네트워킹 등의 리소스)로 Google Cloud 리소스를 프로비저닝할 수 있는 오픈소스 도구
Google Cloud
## public IP
- 54.x.x.x
- 2.xx
- 34.xx
## private IP
- 10.0.x.x
- 192.168.x.x
- 172.16~32.x.x
session에 key를 붙이는 명령어
ssh-add <key_name>
# server password
sudo -i
sudo passwd ec2-user
sudo yum install util-linux-user.x86_64 # 아마존 리눅스만
# zsh 설치
sudo yum install zsh
chsh -s /bin/zsh * # 기본쉘은 zsh 로 변경 몇몇 os는 util-linux-user.x86_64 를 설치필요*
# Linux x86 (64-bit)
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
# Linux ARM
curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
curl -sO https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_darwin_amd64.zip
unzip terraform_0.12.24_darwin_amd64.zip
mv terraform /usr/local/bin
aws configure 세팅
aws configure
설정된 사용자 및 권한 확인
aws sts get-caller-identity
terraform init
terraform plan
terraform apply
terraform import
Amazon에서 제공하는 Private한 네크워크 망입니다. 다음은 VPC의 핵심 구성요소입니다.
- Virtual Private Cloud(VPC)
- 사용자의 AWS 계정 전용 가상 네트워크입니다.
- 서브넷
- VPC의 IP 주소 범위입니다.
- 라우팅 테이블
- 네트워크 트래픽을 전달할 위치를 결정하는 데 사용되는 라우팅이라는 규칙 집합입니다.
- 인터넷 게이트웨이
- VPC의 리소스와 인터넷 간의 통신을 활성화하기 위해 VPC에 연결하는 게이트웨이입니다.
- NAT 게이트웨이
- 네트워크 주소 변환을 통해 프라이빗 서브넷에서 인터넷 또는 기타 AWS 서비스에 연결하는 게이트웨이입니다.
- 씨큐리티 그룹
- 보안 그룹은 인스턴스에 대한 인바운드 및 아웃바운드 트래픽을 제어하는 가상 방화벽 역할을 하는 규칙 집합입니다.
- VPC 엔드포인트 — 인터넷 게이트웨이, NAT 디바이스, VPN 연결 또는 AWS Direct Connect 연결을 필요로 하지 않고 PrivateLink 구동 지원 AWS 서비스 및 VPC 엔드포인트 서비스에 VPC를 비공개로 연결할 수 있습니다. VPC의 인스턴스는 서비스의 리소스와 통신하는 데 퍼블릭 IP 주소를 필요로 하지 않습니다. VPC와 기타 서비스 간의 트래픽은 Amazon 네트워크를 벗어나지 않습니다.
vpc생성 시 사설 대역망 사용 (CIDR block)
10.0.0.0/8
172.168.0.0/12
192.168.0.0/16
nat gateway
일 경우nat gateway
는 퍼블릭 서브넷에 존재한다internet gateway
일 경우따라서 서브넷은 라우트 테이블에 따라 프라이빗, 퍼블릭 서브넷이 될 수 있다.
provider.tf
# provider
provider "aws" {
region = "ap-northeast-2"
}
vpc.tf
# VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terraform-101"
}
}
# Subnets
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-northeast-2a"
tags = {
Name = "terraform-101-public-subnet"
}
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "ap-northeast-2b"
tags = {
Name = "terraform-101-private-subnet"
}
}
# IGW
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "terraform-101-igw"
}
}
# elastic ip for NAT
resource "aws_eip" "nat" {
vpc = true
lifecycle {
create_before_destroy = true
}
}
# NAT Gateway
resource "aws_nat_gateway" "nat_gateway" {
allocation_id = aws_eip.nat_1.id
# Private subnet이 아니라 public subnet을 연결해야한다.
subnet_id = aws_subnet.public.id
tags = {
Name = "terraform-101-NAT-GW-1"
}
}
# Route Table
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
# inner rule (IGW와 RT 연결)
route = {
cidr_block = "0.0.0.0/0"
gateway_id = "aws_internet_gateway.igw.id"
}
tags = {
Name = "terraform-101-rt-public"
}
}
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
tags = {
Name = "terraform-101-rt-private"
}
}
# Subnet - Route Table 연결
resource "aws_route_table_association" "route_table_association_public" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.puvlic.id
}
resource "aws_route_table_association" "route_table_association_private" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.private.id
}
# NAT와 RT 연결
resource "aws_route" "private_nat" {
route_table_id = aws_route_table.private.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gateway.id
}
++ 추가로 VPC endpoint 연결
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_iam_user" "gildong_hong" {
name = "gildong.hong"
}