1. [Windows OS] WSL2로 Ubuntu 설치
실습 환경의 일원화를 위해 Windows OS 환경에서 WSL2를 통해 Linux 환경을 구성하고 진행합니다.
이 작업은 사용자 PC가 Windows OS일 경우 해당합니다. (Mac OS는 생략)
1.1. 최소 조건 및 확인
WSL2를 정상적으로 사용하기 위해 Windows 환경의 최소 조건을 확인합니다.
이후 WSL 명령어 동작 여부를 확인하고, 필요 시 설치를 진행합니다.
[Windows OS] Windows 버전 확인
winver
Bash
복사
# [CMD] Windows 버전 확인 창 출력
Windows 10 - 2004(2020년04월) 버전 이상 또는 Windows 11에서는 WSL2 사용이 지원됩니다.
WSL 명령어 동작 확인
wsl --install --no-distribution
Bash
복사
# [CMD] WSL 활성화
WSL 활성화 후 PC 재부팅을 수행하여 다시 접근합니다.
wsl --status
Bash
복사
# [CMD] WSL 상태 확인 (버전 2 확인)
wsl --list --online
Bash
복사
# [CMD] Microsoft Store를 통해 온라인으로 배포할 수 있는 목록
사용 중인 Windows 버전이 요구 조건을 만족하지 않을 경우, 아래 Microsoft 공식 문서를 참고하여 WSL 수동 설치를 진행하시기 바랍니다.
1.2. WSL2로 Ubuntu 설치
WSL2 기능이 정상적으로 활성화된 상태에서 Linux 배포판(Ubuntu)을 설치하고 초기 설정을 진행합니다.
[WSL2] Ubuntu 설치 및 초기 설정
wsl --install -d Ubuntu
Bash
복사
# [WSL] Ubuntu 기본 지정 버전으로 설치
Create a default Unix user account: <입력>
Bash
복사
# 사용자 계정 입력
New password: <입력>
Retype new password: <입력>
Bash
복사
# 사용자 비밀번호 입력
WSL - Ubuntu 최초 실행 시 사용자 계정과 비밀번호를 설정합니다.
초기 설정이 완료되면 Ubuntu에 정상적으로 접근됩니다.
[WSL2] Ubuntu CLI 접근
exit
Bash
복사
# [WSL] Ubuntu에서 빠져 나오기
wsl -l -v
Bash
복사
# [CMD] WSL2 - Ubuntu 설치 확인
wsl -d Ubuntu
Bash
복사
# [CMD] 배포한 Ubuntu에 접근
1.3. Ubuntu 구성 확인
Ubuntu에 접근한 상태에서 WSL 기반 Linux 환경이 정상적으로 구성되었는지 확인합니다.
시스템 정보 확인
uname -a
Bash
복사
# 리눅스 커널 정보 확인
cat /etc/os-release
Bash
복사
# OS 릴리즈 정보 확인
디스크 및 마운트 정보 확인
df -h
Bash
복사
# 루트 파일 시스템 및 용량 구조 확인
mount | grep /mnt
Bash
복사
# 마운트 정보 (/mnt) 확인
ls /mnt
Bash
복사
# Windows 드라이브(/mnt/c 등) 마운트 확인
ls /mnt/c
Bash
복사
패키지 도구 설치
sudo apt update
Bash
복사
# apt update와 편의성 도구 설치
sudo apt install -y tree jq htop unzip
Bash
복사
[Windows → WSL] 네트워크 공유 접근
<윈도우 키> + R
Bash
복사
# 실행 창 진입
\\wsl$\Ubuntu
Bash
복사
# [실행 창] Ubuntu에 네트워크 공유로 접근
생성한 Ubuntu에 최상위 경로(/)로 이동할 수 있습니다.
[Optional] 강의 종료 후 WSL(Ubuntu) 환경 정리
2. 기본 환경 구성
앞으로 실습 진행을 위한 기본 환경을 구성합니다.
2.1. SSH 키 페어 생성
실습 환경에서 생성하는 EC2 인스턴스 접속을 위한 SSH 키 페어를 생성합니다.
각자의 AWS 관리 콘솔로 접근한 다음 아래 생성 작업을 진행합니다.
[AWS 관리 콘솔] 키 페어 생성
[관리 콘솔 접근: EC2 서비스 → 네트워크 및 보안 → 키 페어]
•
우측 상단 키 페어 생성 (클릭)
◦
이름: <각자의 키 페어 이름 입력>
◦
키 페어 유형: RSA (유지)
◦
프라이빗 키 파일 형식: .pem (유지)
◦
키 페어 생성 (클릭)
⇒ 키 페어 생성으로 프라이빗 키를 다운로드합니다.
[Mac OS] 프라이빗 키 이동 및 권한 설정
아래 명령어들은 샘플 예시로 각자 환경에 맞는 파일명이나 경로를 지정해야 합니다.
mkdir -p ~/.ssh
chmod 700 ~/.ssh
Bash
복사
# [Mac OS] .ssh 디렉터리 생성
mv <키파일이름>.pem ~/.ssh
Bash
복사
# [Mac OS] 다운로드한 키 파일을 각자 원하는 경로에 이동
ls -al ~/.ssh
Bash
복사
# [Mac OS] 프라이빗 키의 권한 설정
chmod 400 ~/.ssh/<키파일이름>.pem
Bash
복사
[Windows OS] 프라이빗 키 이동 및 권한 설정
아래 명령어들은 샘플 예시로 각자 환경에 맞는 파일명이나 경로를 지정해야 합니다.
mkdir -p ~/.ssh
chmod 700 ~/.ssh
Bash
복사
# [WSL - Ubuntu] .ssh 디렉터리 생성
\\wsl$\Ubuntu\home\<사용자명>\.ssh
Bash
복사
# [Windows OS - 실행] 대상 경로의 탐색기를 열고 키 파일을 이동
ls -al ~/.ssh
Bash
복사
# [WSL - Ubuntu] 프라이빗 키의 권한 설정
chmod 400 ~/.ssh/<키파일이름>.pem
Bash
복사
각자 생성한 키 페어를 사용하는 EC2 인스턴스를 한 대 생성해서 SSH로 정상 접근되는지 확인합니다.
ssh -i ~/.ssh/<키파일이름>.pem ec2-user@<EC2 퍼블릭 IP>
Bash
복사
2.2. IAM 사용자 생성
이번 강의의 실습은 개인 AWS 계정 환경에서 진행하는 것을 권장합니다.
또한, IAM 사용자 계정을 통해 관리 콘솔에 접근하고 액세스 키를 활용해 awscli 도구를 사용합니다.
IAM 사용자 생성
•
•
사용자 생성 버튼을 클릭합니다.
•
사용자 이름은 admin으로 입력하고 [AWS Management Console에 대한 사용자 액세스 권한 제공]을 체크합니다.
•
사용자에게 콘솔 액세스 권한 제공은 [IAM 사용자를 생성하고 싶음]을 선택합니다.
•
콘솔 암호는 [사용자 지정 암호]를 선택하고 생성 기준에 맞춰 각자 암호를 지정합니다.
•
사용자는 다음 로그인 시 새 암호를 생성해야 합니다.를 체크 해제하고 다음 버튼을 클릭합니다.
•
권한 옵션은 [직접 정책 연결]을 선택하고 권한 정책에서 [AdministratorAccess]를 체크한 후 아래 다음 버튼을 클릭합니다.
•
검토 및 생성 페이지에서 사용자 생성 버튼을 클릭합니다.
•
암호 검색 페이지에서 .csv 파일 다운로드 버튼을 클릭하여 자신의 PC의 디렉터리에 저장합니다.
•
사용자 목록으로 돌아가기 버튼을 클릭하여 IAM 사용자 생성을 마무리합니다.
IAM 사용자로 관리 콘솔에 로그인 할때 계정 ID가 필요하니 잘 메모해 둡니다.
IAM 사용자 액세스 키 생성
•
IAM 사용자 페이지에서 생성한 사용자 이름을 클릭합니다.
•
보안 자격 증명 탭을 클릭하고 [액세스 키] 영역에서 액세스 키 만들기 버튼을 클릭합니다.
•
액세스 키 모범 사례 및 대안 페이지에서 [Command Line Interface(CLI)]를 선택하고 아래 체크 박스를 체크한 후 다음 버튼을 클릭합니다.
•
액세스 키 만들기 버튼을 클릭합니다.
•
액세스 키 검색 페이지에서 .csv 파일 다운로드 버튼을 클릭하여 자신의 PC의 디렉터리에 저장합니다.
•
완료 버튼을 클릭하여 IAM 사용자 액세스 키 생성을 마무리합니다.
IAM 사용자에 대한 액세스 키 ID와 시크릿 액세스 키는 모든 권한을 가진 프로그래밍 기반 보안 키로 외부에 노출되지 않도록 잘 보관하고 사용하기 바랍니다.
⇒ 현재 AWS 관리 콘솔에 접속한 루트 계정을 로그 아웃하고, IAM 사용자 기반으로 로그인합니다.
2.3. Slack 실습 환경 구성
Slack을 활용한 실습 환경을 구성하고, 이후 실습에서 사용할 알림 및 연동 기능을 준비합니다.
[Slack] 가입 및 데스크톱 앱 설치
⇒ 위 링크는 공식 사이트에서 제공하는 Slack 사용법: 빠른 시작 가이드로 무료 플랜으로 가입하고 설치까지 완료해 주세요.
혹시나 Slack 가이드 문서 링크 경로가 변경된 경우 검색을 통해 접근해 주세요.
[Slack] 실습용 워크스페이스 생성
1. Slack 데스크톱 접속
2. 좌측 상단에 + 아이콘 (클릭)
⇒ 새 워크스페이스 생성 (선택)
3. 이메일 주소 입력 (확인)
⇒ 이메일 주소로 전달된 인증 코드 6자리 입력
4. 워크스페이스 설정 작업
1단계) 워크스페이스 이름 설정
2단계) 당신의 이름 설정
3단계) 팀원 추가 (이 단계 건너뛰기) ⇒ 단계 건너뛰기
4단계) 제한된 무료 버전으로 시작하기 (클릭)
5. 좌측 상단에 + 아이콘 (클릭)
⇒ 다른 워크스페이스에 로그인 (선택)
6. 앞서 생성한 워크스페이스 (체크) ⇒ 열기
⇒ Slack 열기
3. Terraform으로 AWS 자원 배포
Terraform 구성으로 간단한 AWS 자원을 배포해 봅니다.
3.1. Terraform 환경 구성
이번 실습은 Terraform 도구로 AWS 자원을 배포하는 실습으로 사용자 PC에서 기본 환경 구성을 진행합니다.
이번 실습은 Terraform 1.11.x 버전을 기준으로 검증된 상태입니다.
최신 버전에서도 동작할 수 있으나, 버전에 따라 일부 동작 차이가 발생할 수 있으므로 가급적 동일한 버전 사용을 권장합니다.
[Mac OS] Terraform 설치
brew install tfenv
Bash
복사
# tfenv 설치
tfenv list-remote
Bash
복사
# 설치 가능 버전 리스트 확인
tfenv install 1.11.2
Bash
복사
# Terraform 1.11.2 버전 설치 (실습 검증 기준 버전)
tfenv use 1.11.2
Bash
복사
# Terraform 1.11.2 버전 사용 설정
tfenv list
Bash
복사
# tfenv로 설치한 버전 확인
terraform version
Bash
복사
# Terraform 버전 확인
[Mac OS] awscliv2 설치
brew install awscli
Bash
복사
# brew로 awscli 설치
which aws
Bash
복사
# awscli가 설치된 경로
aws --version
Bash
복사
# awscli 버전 확인
aws configure
Bash
복사
# awscli 인증 설정
Note:
aws configure 명령으로 IAM User에 대한 Credential을 정의합니다.
(AWS Access Key ID, AWS Secret Access Key, Default Region=ap-northeast-2)
export AWS_PAGER=""
aws sts get-caller-identity --query Arn
Bash
복사
# awscli 명령 확인
aws s3 ls
Bash
복사
[WSL2 - Ubuntu] Terraform 설치
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
Bash
복사
# Terraform 패키지 저장소 등록 및 패키지 목록 업데이트
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
Bash
복사
sudo apt update
Bash
복사
apt list -a terraform
Bash
복사
# 설치 가능한 Terraform 버전 목록 확인
sudo apt install terraform=1.11.2-1
Bash
복사
# Terraform 1.11.2 버전 설치 (실습 검증 기준 버전)
sudo apt-mark hold terraform
Bash
복사
# Terraform 자동 업데이트 방지 (버전 고정)
terraform version
Bash
복사
# Terraform 버전 확인
[WSL2 - Ubuntu] awscliv2 설치
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Bash
복사
# AWS CLI v2 다운로드
unzip awscliv2.zip
Bash
복사
# 대상 파일 압축 해제
sudo ./aws/install
Bash
복사
# AWS CLI v2 설치
aws --version
Bash
복사
# AWS CLI v2 설치 확인
aws configure
Bash
복사
# awscli 인증 설정
Note:
aws configure 명령으로 IAM User에 대한 Credential을 정의합니다.
(AWS Access Key ID, AWS Secret Access Key, Default Region=ap-northeast-2)
aws sts get-caller-identity --query Arn
Bash
복사
# awscli 명령 확인
aws s3 ls
Bash
복사
git clone - 실습 코드
git clone https://github.com/cloudneta/cnasg_class_tf
Bash
복사
# git clone - cnasg_class_tf
cd cnasg_class_tf
Bash
복사
# 실습 코드 경로 진입
ls -al
Bash
복사
# 실습 코드 경로 확인
tree
Bash
복사
3.2. Terraform 자원 배포 및 확인
Terraform 기본 사용법을 익히고, 실습용 코드를 배포하여 AWS 자원 생성 결과를 확인합니다.
Terraform 기본 사용법
1. vpc.tf 생성
mkdir my-vpc-ec2
cd my-vpc-ec2
Bash
복사
# 신규 디렉터리 생성
cat << EOF > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
tags = {
Name = "t101-study"
}
}
EOF
cat vpc.tf
Bash
복사
# vpc.tf 파일 생성 및 확인
terraform init
Bash
복사
# terraform init
terraform plan
Bash
복사
# terraform plan
terraform apply -auto-approve
Bash
복사
# terraform apply
terraform state list
Bash
복사
# terraform state list
terraform state show aws_vpc.myvpc
Bash
복사
# state 확인 (aws_vpc.myvpc)
export AWS_PAGER=""
aws ec2 describe-vpcs
Bash
복사
# awscli로 VPC 확인
aws ec2 describe-vpcs --filter 'Name=isDefault,Values=false'
Bash
복사
aws ec2 describe-vpcs --filter 'Name=isDefault,Values=false' --output yaml
Bash
복사
AWS 관리 콘솔에서 VPC 생성 정보를 확인하고, DNS 옵션 값을 확인합니다.
2. vpc.tf 수정 - VPC DNS 옵션 활성화
cat << EOF > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "t101-study"
}
}
EOF
cat vpc.tf
Bash
복사
# vpc.tf 수정 및 확인
ls -al
Bash
복사
# Backend에 .tfstate 확인하기
cat terraform.tfstate
Bash
복사
terraform plan
Bash
복사
# Terraform 배포
terraform apply -auto-approve
Bash
복사
AWS 관리 콘솔에서 VPC 생성 정보를 확인하고, DNS 옵션 값을 확인합니다.
또한, terraform.tfstate 파일의 정보도 확인해 보세요. (cat terraform.tfstate)
3. vpc.tf 수정 - 서브넷 2개 생성
cat << EOF > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "t101-study"
}
}
resource "aws_subnet" "mysubnet1" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.10.1.0/24"
availability_zone = "ap-northeast-2a"
tags = {
Name = "t101-subnet1"
}
}
resource "aws_subnet" "mysubnet2" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.10.2.0/24"
availability_zone = "ap-northeast-2c"
tags = {
Name = "t101-subnet2"
}
}
output "aws_vpc_id" {
value = aws_vpc.myvpc.id
}
EOF
cat vpc.tf
Bash
복사
# vpc.tf 수정 및 확인
terraform apply -auto-approve
Bash
복사
# Terraform 배포 및 State 확인
terraform state list
Bash
복사
terraform state show aws_subnet.mysubnet1
Bash
복사
terraform output
terraform output aws_vpc_id
terraform output -raw aws_vpc_id
Bash
복사
# terraform output 확인
aws ec2 describe-subnets --output text
Bash
복사
# awscli로 서브넷 확인
VPCID=$(terraform output -raw aws_vpc_id); echo $VPCID
aws ec2 describe-subnets --filters "Name=vpc-id,Values=$VPCID"
Bash
복사
# VPCID 변수 선언 및 awscli로 VPC 정보 확인
aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=$VPCID" \
--output table
Bash
복사
4. vpc.tf 수정 - 인터넷 게이트웨이 생성
cat << EOF > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "t101-study"
}
}
resource "aws_subnet" "mysubnet1" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.10.1.0/24"
availability_zone = "ap-northeast-2a"
tags = {
Name = "t101-subnet1"
}
}
resource "aws_subnet" "mysubnet2" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.10.2.0/24"
availability_zone = "ap-northeast-2c"
tags = {
Name = "t101-subnet2"
}
}
resource "aws_internet_gateway" "myigw" {
vpc_id = aws_vpc.myvpc.id
tags = {
Name = "t101-igw"
}
}
output "aws_vpc_id" {
value = aws_vpc.myvpc.id
}
EOF
cat vpc.tf
Bash
복사
# vpc.tf 수정 및 확인
terraform apply -auto-approve
Bash
복사
# Terraform 배포 및 State 확인
terraform state list
Bash
복사
terraform state show aws_internet_gateway.myigw
Bash
복사
5. vpc.tf 수정 - 디폴트 라우팅 추가
cat << EOF > vpc.tf
provider "aws" {
region = "ap-northeast-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.10.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "t101-study"
}
}
resource "aws_subnet" "mysubnet1" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.10.1.0/24"
availability_zone = "ap-northeast-2a"
tags = {
Name = "t101-subnet1"
}
}
resource "aws_subnet" "mysubnet2" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.10.2.0/24"
availability_zone = "ap-northeast-2c"
tags = {
Name = "t101-subnet2"
}
}
resource "aws_internet_gateway" "myigw" {
vpc_id = aws_vpc.myvpc.id
tags = {
Name = "t101-igw"
}
}
resource "aws_route_table" "myrt" {
vpc_id = aws_vpc.myvpc.id
tags = {
Name = "t101-rt"
}
}
resource "aws_route_table_association" "myrtassociation1" {
subnet_id = aws_subnet.mysubnet1.id
route_table_id = aws_route_table.myrt.id
}
resource "aws_route_table_association" "myrtassociation2" {
subnet_id = aws_subnet.mysubnet2.id
route_table_id = aws_route_table.myrt.id
}
resource "aws_route" "mydefaultroute" {
route_table_id = aws_route_table.myrt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.myigw.id
}
output "aws_vpc_id" {
value = aws_vpc.myvpc.id
}
EOF
cat vpc.tf
Bash
복사
# vpc.tf 수정 및 확인
terraform apply -auto-approve
Bash
복사
# Terraform 배포 및 State 확인
terraform state list
Bash
복사
terraform state show aws_route.mydefaultroute
Bash
복사
aws ec2 describe-route-tables \
--filters 'Name=tag:Name,Values=t101-rt' \
--output table
Bash
복사
# awscli로 라우팅 테이블 확인
6. sg.tf 생성
cat << EOF > sg.tf
resource "aws_security_group" "mysg" {
vpc_id = aws_vpc.myvpc.id
name = "T101 SG"
description = "T101 Study SG"
}
resource "aws_security_group_rule" "mysginbound" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.mysg.id
}
resource "aws_security_group_rule" "mysgoutbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.mysg.id
}
output "aws_security_group_id" {
value = aws_security_group.mysg.id
}
EOF
cat sg.tf
Bash
복사
# sg.tf 파일 생성 및 확인
ls *.tf
Bash
복사
# Terraform 배포 및 State 확인
terraform apply -auto-approve
Bash
복사
terraform state list
Bash
복사
terraform state show aws_security_group.mysg
Bash
복사
terraform state show aws_security_group_rule.mysginbound
Bash
복사
terraform output -raw aws_security_group_id
Bash
복사
# awscli로 생성한 보안 그룹 확인
aws ec2 describe-security-groups \
--group-ids $(terraform output -raw aws_security_group_id)
Bash
복사
7. ec2.tf 생성
cat << EOF > ec2.tf
data "aws_ami" "my_amazonlinux2023" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["al2023-ami-*-x86_64"]
}
owners = ["amazon"]
}
resource "aws_instance" "myec2" {
depends_on = [
aws_internet_gateway.myigw
]
ami = data.aws_ami.my_amazonlinux2023.id
associate_public_ip_address = true
instance_type = "t2.micro"
vpc_security_group_ids = ["\${aws_security_group.mysg.id}"]
subnet_id = aws_subnet.mysubnet1.id
user_data = <<-EOF2
#!/bin/bash
dnf install wget -y
wget https://busybox.net/downloads/binaries/1.31.0-defconfig-multiarch-musl/busybox-x86_64
mv busybox-x86_64 busybox
chmod +x busybox
echo "<h1>Web Server</h1>" > index.html
nohup ./busybox httpd -f -p 80 &
EOF2
user_data_replace_on_change = true
tags = {
Name = "t101-myec2"
}
}
output "myec2_public_ip" {
value = aws_instance.myec2.public_ip
description = "The public IP of the Instance"
}
EOF
cat ec2.tf
Bash
복사
# ec2.tf 파일 생성 및 확인
ls *.tf
Bash
복사
# Terraform 배포 및 State 확인
terraform apply -auto-approve
Bash
복사
terraform state list
Bash
복사
terraform state show data.aws_ami.my_amazonlinux2023
Bash
복사
terraform state show aws_instance.myec2
Bash
복사
terraform console
Bash
복사
# 데이터소스 값 확인
data.aws_ami.my_amazonlinux2023
data.aws_ami.my_amazonlinux2023.id
data.aws_ami.my_amazonlinux2023.image_id
data.aws_ami.my_amazonlinux2023.name
data.aws_ami.my_amazonlinux2023.owners
data.aws_ami.my_amazonlinux2023.platform_details
data.aws_ami.my_amazonlinux2023.hypervisor
data.aws_ami.my_amazonlinux2023.architecture
exit
Bash
복사
terraform output -raw myec2_public_ip
Bash
복사
# 출력된 EC2 퍼블릭IP로 curl 접속 확인
MYIP=$(terraform output -raw myec2_public_ip); echo $MYIP
Bash
복사
curl http://$MYIP
Bash
복사
초기 부팅과 cloud-init 과정으로 인해 서비스 지연이 발생할 수 있습니다.
접속 실패 시 잠시 대기 후 다시 접속해 주세요.
8. Terraform 배포 자원 삭제
terraform destroy -auto-approve
Bash
복사
# terraform destroy
실습 코드로 Terraform 배포 및 확인
cd ..
cd Section02
Bash
복사
# 실습 코드 경로 진입
ls -al
Bash
복사
# 실습 경로에 terraform 코드 확인
Note:
구성된 .tf 파일을 확인 합니다. (var.tf, ec2.tf, vpc.tf, out.tf)
terraform init
terraform init
Bash
복사
# terraform init 명령 수행
ls -al
Bash
복사
# 실습 경로 확인 (숨은 파일/디렉터리 포함)
terraform plan
terraform plan
Bash
복사
# terraform plan 명령 수행
Note:
KeyName과 SgIngressSshcidr([사용자 PC의 퍼블릭 IP 주소]/32)를 입력합니다.
사용자 PC의 퍼블릭 IP 주소는 curl -s ipinfo.io/ip로 확인합니다.
terraform apply
export TF_VAR_KeyName=[각자 SSH 키 페어]
export TF_VAR_SgIngressSshCidr=$(curl -s ipinfo.io/ip)/32
Bash
복사
# TF_VAR 변수 선언
terraform apply -auto-approve
Bash
복사
# terraform apply 명령 수행
Note:
terraform apply 명령을 수행하면 Terraform이 실행할 변경 사항에 대한 승인을 물어봅니다. (yes!)
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:
Bash
복사
-auto-approve 옵션을 추가하면 자동으로 승인을 처리합니다.
ls -al
Bash
복사
# 실습 경로 확인 (숨은 파일/디렉터리 포함)
생성된 자원 확인
terraform state list
Bash
복사
# terraform state list 확인
terraform state show 'module.vpc.aws_vpc.this[0]' | grep -w 'id' | awk '{print $3}' | tr -d '"'
VPC_ID=$(terraform state show 'module.vpc.aws_vpc.this[0]' | grep -w 'id' | awk '{print $3}' | tr -d '"'); echo $VPC_ID
Bash
복사
# VPC_ID 변수 선언
aws ec2 describe-subnets --filters "Name=vpc-id,Values=$VPC_ID" \
--query "Subnets[*].[SubnetId, CidrBlock, AvailabilityZone]" \
--output table
Bash
복사
# VPC 내 서브넷 정보 확인
aws ec2 describe-security-groups --filters "Name=vpc-id,Values=$VPC_ID" \
--query "SecurityGroups[*].[GroupId, GroupName, Description]" \
--output table
Bash
복사
# VPC 내 보안 그룹 정보 확인
terraform output -raw public_ip
Bash
복사
# EC2 인스턴스 퍼블릭 IP 변수 선언 및 접속
BASTION_IP=$(terraform output -raw public_ip); echo $BASTION_IP
curl http://$BASTION_IP
Bash
복사
3.3. 실습 환경 삭제
Terraform 배포 자원 삭제
terraform destroy -auto-approve
Bash
복사
# terraform destroy 명령
기본 사용법 테스트 삭제
cd ..
rm -rf my-vpc-ec2
Bash
복사
# my-vpc-ec2 경로 삭제
여기까지 섹션 2 - 사전 준비 실습을 마칩니다.
수고하셨습니다 :)



