1. Terraform이란?
1.1. IaC (Infrastructure as Code)
수작업으로 수행하던 인프라 구축을 코드(정의 파일, 명령어 등)를 사용하여 수행하는 방법
IaC를 이용하면 작업 공수의 절감, 작업 품질의 향상 효과를 가져올 수 있다.
1.2. Terraform
IaC 도구의 한 종류로, 아래의 구조로 동작한다.
2. Terraform 실습
2.1. Terraform 설치 (Windows 기준)
Install | Terraform | HashiCorp Developer
Install | Terraform | HashiCorp Developer
Explore Terraform product documentation, tutorials, and examples.
developer.hashicorp.com
2.2. Visual Studio Code Terraform 설정
Extension에서 HashiCorp Terraform 다운로드
2.3. AWS 설정
2.3.1. IAM Access Key 생성
2.3.2. EC2 AMI ID 확인
2.4. Terraform 파일 작성
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
Terraform Registry
registry.terraform.io
2.4.1. Terraform 기본 명령어
- terraform init
- .tf 파일에 대한 문제점 체크 (배포 전 환경 초기화 작업)
- 접속 테스트 및 구문 오류 확인
- terraform plan
- 작성한 .tf 구문에 설정한 변수 내용 출력
- terraform apply
- 배포 및 구축 작업 수행
- terraform destroy
- 배포된 인프라 삭제
- terraform state
- terraform state list
- terraform state show aws_route_table.terra.rt
- terraform state list
2.4.2. Init
# terra.tf
provider "aws" {
region = "ap-southeast-1"
access_key = "{{IAM_ACCESS_KEY_ID}}"
secret_key = "{{IAM_SECRET_ACCESS_KEY}}"
}
2.4.2. EC2
(1) EC2 인스턴스 생성
# terra.tf
provider "aws" {
region = "ap-southeast-1"
access_key = "{{IAM_ACCESS_KEY_ID}}"
secret_key = "{{IAM_SECRET_ACCESS_KEY}}"
}
resource "aws_instance" "terra_web" {
ami = {{EC2_AMI_ID}}
instance_type = "t2.micro"
tags = {
Name = "terra-web"
}
}
terraform plan
terraform apply
(2) EC2 인스턴스 삭제
terraform destroy
2.4.3. VPC
[참고] .tf 작성 시 큰 것 → 작은 것 순으로 코드를 배열하는 것이 좋음
provider "aws" {
region = "ap-southeast-1"
access_key = "{{IAM_ACCESS_KEY_ID}}"
secret_key = "{{IAM_SECRET_ACCESS_KEY}}"
}
resource "aws_instance" "terra_web2" {
ami = {{EC2_AMI_ID}}
instance_type = "t2.micro"
tags = {
Name = "terra_web2"
}
}
resource "aws_vpc" "terra_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terra_vpc"
}
}
resource "aws_subnet" "terra_subnet" {
vpc_id = aws_vpc.terra_vpc.id
cidr_block = "10.0.10.0/24"
availability_zone = "ap-southeast-1a" # 생략 가능
tags = {
Name = "terra_subnet"
}
}
2.4.4. Internet Gateway, Routing Table
(1) IGW 생성
(2) Routing Table 생성
(3) Routing Table과 Subnet 연동
(4) Security Group 설정
(5) ENI 설정
(6) EIP 설정
(7) 인스턴스 연결
2.5. httpd 띄워 보기
provider "aws" {
region = "ap-southeast-1"
access_key = "{ACCESS_KEY}"
secret_key = "{SECRET_KEY}"
}
resource "aws_vpc" "terra_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terra_vpc"
}
}
resource "aws_subnet" "terra_subnet" {
vpc_id = aws_vpc.terra_vpc.id
cidr_block = "10.0.10.0/24"
availability_zone = "ap-southeast-1a" # 생략 가능
tags = {
Name = "terra_subnet"
}
}
resource "aws_internet_gateway" "terra_gw" {
vpc_id = aws_vpc.terra_vpc.id
tags = {
Name = "terra_gw"
}
}
resource "aws_route_table" "terra_rt" {
vpc_id = aws_vpc.terra_vpc.id
route {
cidr_block = "0.0.0.0/0" # 모든 IP에 대하여 통신 허용용
gateway_id = aws_internet_gateway.terra_gw.id
}
tags = {
Name = "terra_rt"
}
}
resource "aws_route_table_association" "terra_rta" {
subnet_id = aws_subnet.terra_subnet.id
route_table_id = aws_route_table.terra_rt.id
}
resource "aws_security_group" "terra_sg" {
name = "terra_sg"
description = "Allow SSH(22), HTTP(80), HTTPS(443)"
vpc_id = aws_vpc.terra_vpc.id
tags = {
Name = "terra_sg"
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1" # All protocols
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_network_interface" "terra_eni" {
subnet_id = aws_subnet.terra_subnet.id
private_ips = ["10.0.10.50"]
security_groups = [aws_security_group.terra_sg.id]
}
resource "aws_eip" "terra_eip" {
domain = "vpc"
network_interface = aws_network_interface.terra_eni.id
associate_with_private_ip = "10.0.10.50"
depends_on = [ aws_instance.terra_web2 ]
}
resource "aws_instance" "terra_web2" {
ami = "ami-0995922d49dc9a17d" # Amazon Linux
instance_type = "t2.micro"
network_interface {
network_interface_id = aws_network_interface.terra_eni.id
device_index = 0
}
user_data = <<-EOF
#!/bin/bash
sudo yum install httpd -y
sudo systemctl start httpd
sudo bash -c `echo Terraform HTML HELLO_WORLD > /var/www/html/index.html`
EOF
tags = {
Name = "terra_web2"
}
}
'Security > SK 쉴더스 루키즈 23기' 카테고리의 다른 글
[클라우드 기반 시스템 운영 & 구축 실무] 로그 수집 및 분석 (0) | 2025.04.11 |
---|---|
[어플리케이션 보안] SQL Injection과 SQLMAP (0) | 2025.04.11 |
[인프라 활용을 위한 파이썬] 함수 (0) | 2025.01.09 |
[취약점 진단 실무] 보안 가이드 업데이트 실습 (0) | 2024.12.18 |
[모듈 프로젝트] 웹 서비스를 위한 AWS 인프라 구축 (0) | 2024.12.09 |