๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DevOps/Terraform

[Terraform] 1. ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

by ํƒœ์˜น 2023. 8. 8.

๐Ÿ‘‡๐Ÿป ์ด์ „๊ธ€ ์ฐธ๊ณ 

 

Cloud9์—์„œ ํ‚คํŽ˜์–ด ์ƒ์„ฑ ํ›„ AWS์— ๋“ฑ๋ก

ํ‚คํŽ˜์–ด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‹ค์Œ์˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 1. AWS ์ฝ˜์†”์—์„œ ํ‚คํŽ˜์–ด ์ƒ์„ฑ -> Cloud9 ์ธ์Šคํ„ด์Šค๋กœ ๊ฐ€์ง€๊ณ  ์˜ค๊ธฐ 2. Cloud9 ์ธ์Šคํ„ด์Šค์—์„œ ํ‚คํŽ˜์–ด ์ƒ์„ฑ -> AWS์— ๋“ฑ๋กํ•˜๊ธฐ ์šฐ๋ฆฌ๋Š” 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•(SSH

taetoungs-branch.tistory.com


๊ธฐ๋Œ€ํ•˜๋Š” ๊ตฌ์„ฑ

0. provider ์ง€์ •

provider "aws" {
#  access_key = "์ž์‹ ์˜ Key ๋ฅผ ์ž…๋ ฅ" # Cloud9 ์„ ์‚ฌ์šฉํ•˜๋ฉด IAM Role ์„ ํ™œ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์ผ Mac ์‚ฌ์šฉ์ž๋Š” IAM ์—์„œ ๋ฐœ๊ธ‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
#  secret_key = "์ž์‹ ์˜ Key ๋ฅผ ์ž…๋ ฅ"
  region = "ap-northeast-2"
}

ํ…Œ๋ผํผ์—์„œ ์‚ฌ์šฉํ•˜๋Š” provider๋ž€ AWS, Azure, GCP ๋“ฑ์„ ๋งํ•œ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Terraform Registry

 

registry.terraform.io

access key, secret key๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์†์„ฑ๊ฐ’์œผ๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ํ˜„์žฌ ์‹ค์Šต์—์„œ๋Š” Cloud9์—์„œ AWSCloud9SSMInstanceProfile Role์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ƒ๋žตํ•œ๋‹ค.

 

 

1. AWS์— public key ๋“ฑ๋ก

# aws์— importํ•˜๋Š” ๋ช…๋ น์–ด๋ž‘ ๊ฐ™์€๊ฑฐ -> ์ด๋ฏธ ๊ฐ™์€ ์ด๋ฆ„์˜ ํ‚คํŽ˜์–ด๊ฐ€ ์—…๋กœ๋“œ๋˜์–ด์žˆ์œผ๋ฉด ์ถฉ๋Œ๋‚จ
resource "aws_key_pair" "key-pair" {
  key_name = "terraform-key"
  public_key = file("/home/ec2-user/.ssh/tf-key.pub")
}

์ด์ „ ๊ธ€์—์„œ AWS CLI ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ Cloud9์— ์œ„์น˜ํ•œ public key๋ฅผ importํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด์™€ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

public key๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ ์ง์ ‘ ๋„ฃ์–ด์ค„ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ๋ณด์•ˆ์ƒ ๊ทธ๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ์ฝ”๋“œ์—์„œ๋Š” Cloud9์˜ /home/ec2-user/.ssh ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” public keyํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๋„๋ก ์ž‘์„ฑํ–ˆ๋‹ค.

 

 

2. AMI ์ง€์ •

data "aws_ami" "amzn2" {
  most_recent = true
  
  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-2.0.*-x86_64-gp2"]
  }
  
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
  
  owners = ["amazon"] # Canonical
}

AWS์—์„œ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ Amazon Linux 2, HVM ๊ฐ€์ƒํ™”, x86_64 ์•„ํ‚คํ…์ฒ˜, gp2 ๋ณผ๋ฅจ ํƒ€์ž…์„ ๊ฐ€์ง„ AMI ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

 

 

3. ๋ณด์•ˆ๊ทธ๋ฃน ์ƒ์„ฑ

resource "aws_security_group" "sg_web" {
  name        = "sg_web"
  description = "Allow web inbound traffic"
  vpc_id      = "<VPC ID>" # ์‚ฌ์šฉํ•˜๊ณ ์žํ•˜๋Š” VPC ID ์ž…๋ ฅ

  ingress {
    description = "Web from VPC"
    from_port   = 0
    to_port     = 0
    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"]
  }

  tags = {
    Name = "allow_web"
  }
}

๐Ÿ‘‡๐Ÿป ๋ช…๋ น์–ด ์„ค๋ช…

๋”๋ณด๊ธฐ

ํ•ด๋‹น ํ…Œ๋ผํผ ์ฝ”๋“œ๋Š” AWS์— ๋ณด์•ˆ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ƒ์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

1. ๊ธฐ๋ณธ ์ •๋ณด :

resource "aws_security_group" "sg_web" {
  name        = "sg_web"
  description = "Allow web inbound traffic"
  vpc_id      = "<VPC ID>" # ์‚ฌ์šฉํ•˜๊ณ ์žํ•˜๋Š” VPC ID ์ž…๋ ฅ


- `resource "aws_security_group" "sg_web"`: AWS ๋ณด์•ˆ ๊ทธ๋ฃน ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์„ ์–ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ฆฌ์†Œ์Šค์˜ local name์€ "sg_web"์ž…๋‹ˆ๋‹ค.
- `name`: ์ƒ์„ฑ๋  ๋ณด์•ˆ ๊ทธ๋ฃน์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.
- `vpc_id`: ํ•ด๋‹น ๋ณด์•ˆ ๊ทธ๋ฃน์ด ์—ฐ๊ฒฐ๋  VPC์˜ ID์ž…๋‹ˆ๋‹ค. `<VPC ID>`๋Š” ์‹ค์ œ VPC ID๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


2. ์ˆ˜์‹  ๊ทœ์น™ (Ingress) :

  ingress {
    description = "Web from VPC"
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }


- `from_port` ๋ฐ `to_port`: ์ˆ˜์‹ ์„ ํ—ˆ์šฉํ•˜๋Š” ํฌํŠธ ๋ฒ”์œ„์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 0์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด ์‹ค์ œ ํฌํŠธ ์ •๋ณด๊ฐ€ ๋ˆ„๋ฝ๋˜์—ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
- `protocol`: "-1"์€ ๋ชจ๋“  ํ”„๋กœํ† ์ฝœ์„ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
- `cidr_blocks`: ์ˆ˜์‹ ์„ ํ—ˆ์šฉํ•˜๋Š” IP ์ฃผ์†Œ ๋ฒ”์œ„์ž…๋‹ˆ๋‹ค. "0.0.0.0/0"์€ ๋ชจ๋“  IP ์ฃผ์†Œ์—์„œ์˜ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


3. ๋ฐœ์‹  ๊ทœ์น™ (Egress) :

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }


- `from_port` ๋ฐ `to_port`: ๋ฐœ์‹ ์„ ํ—ˆ์šฉํ•˜๋Š” ํฌํŠธ ๋ฒ”์œ„์ž…๋‹ˆ๋‹ค. 0์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์œผ๋‚˜, ํ”„๋กœํ† ์ฝœ์ด "-1"๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด ๋ชจ๋“  ํฌํŠธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
- `protocol`: "-1"์€ ๋ชจ๋“  ํ”„๋กœํ† ์ฝœ์„ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
- `cidr_blocks`: ๋ฐœ์‹ ์„ ํ—ˆ์šฉํ•˜๋Š” IP ์ฃผ์†Œ ๋ฒ”์œ„์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ "0.0.0.0/0"์€ ๋ชจ๋“  IP ์ฃผ์†Œ๋กœ์˜ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.


4. ํƒœ๊ทธ (Tags) :

  tags = {
    Name = "allow_web"
  }


- `tags`: AWS ๋ฆฌ์†Œ์Šค์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” Name ํƒœ๊ทธ์— "allow_web" ๊ฐ’์„ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

"sg_web"์ด๋ผ๋Š” ์ด๋ฆ„์˜ AWS ๋ณด์•ˆ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•œ๋‹ค. ์ด ๋ณด์•ˆ๊ทธ๋ฃน์€ ๋ชจ๋“  IP์—์„œ TCP ํ”„๋กœํ† ์ฝœ๋กœ์˜ ์ˆ˜์‹  ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๊ณ , ๋ชจ๋“  IP๋กœ์˜ ๋ชจ๋“  ํ”„๋กœํ† ์ฝœ ๋ฐ ํฌํŠธ๋กœ์˜ ๋ฐœ์‹ ์„ ํ—ˆ์šฉํ•˜๋Š” ์„ค์ •์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. (์›๋ž˜ ์ด๋ ‡๊ฒŒ ๋‹ค ์˜คํ”ˆํ•ด๋†“์œผ๋ฉด ์•ˆ๋˜์ง€๋งŒ ์‹ค์Šต ์ƒํ™ฉ์ด๋ฏ€๋กœ ์ „์ฒดํ—ˆ์šฉ์„ ํ•ด๋†“์•˜๋‹ค.)

 

 

4. ์ธ์Šคํ„ด์Šค ์ •์˜

resource "aws_instance" "example-2a" {
  ami           = data.aws_ami.amzn2.id # ์ด์ „์— ์ •์˜๋œ aws_ami ๋ฐ์ดํ„ฐ ์†Œ์Šค์—์„œ amzn2 AMI์˜ ID๋ฅผ ์ฐธ์กฐ
  instance_type = "t2.micro"
  key_name      = "tf-key"
  vpc_security_group_ids = [aws_security_group.sg_web.id]  # ์ด์ „์— ์ •์˜๋œ aws_security_group ๋ฆฌ์†Œ์Šค์—์„œ sg_web ๋ณด์•ˆ ๊ทธ๋ฃน์˜ ID๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  subnet_id = "ap-northeast-2a์˜ ์„œ๋ธŒ๋„ท id"
  availability_zone = "ap-northeast-2a"
  user_data = file("./userdata.sh")
  
  tags = {
    Name = "Terraform-ec2-2a"
  }
}

resource "aws_instance" "example-2c" {
  ami           = data.aws_ami.amzn2.id 
  instance_type = "t2.micro"
  key_name      = "tf-key"
  vpc_security_group_ids = [aws_security_group.sg_web.id]
  subnet_id = "ap-northeast-2c์˜ ์„œ๋ธŒ๋„ท id"
  availability_zone = "ap-northeast-2c"
  user_data = file("./userdata.sh")

  tags = {
    Name = "Terraform-ec2-2c"
  }
}

ap-northeast-2 ๋ฆฌ์ „์— ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ๊ฐ€์šฉ ์˜์—ญ(2a์™€ 2c)์— EC2 ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ์ƒ์„ฑํ•œ๋‹ค.

๋‘ ์ธ์Šคํ„ด์Šค ๋ชจ๋‘ ๋™์ผํ•œ AMI, ์ธ์Šคํ„ด์Šค ์œ ํ˜•, ํ‚ค ์ด๋ฆ„, ๋ณด์•ˆ ๊ทธ๋ฃน ๋ฐ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์ฐจ์ด์ ์€ ๋‹ค๋ฅธ ์„œ๋ธŒ๋„ท, ๊ฐ€์šฉ ์˜์—ญ์— ์œ„์น˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

user_data๋Š” EC2 ์ธ์Šคํ„ด์Šค ์‹œ์ž‘ ์‹œ ์‹คํ–‰ํ•  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

./userdata.sh๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์„œ user_data์˜ ๊ฐ’์œผ๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.

 

 

5. user_data ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ

#!/bin/sh

# Install a LAMP stack
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum -y install httpd php-mbstring

# Start the web server
chkconfig httpd on
systemctl start httpd

# Install the web pages for our lab
if [ ! -f /var/www/html/immersion-day-app-php7.tar.gz ]; then
   cd /var/www/html
   wget https://aws-joozero.s3.ap-northeast-2.amazonaws.com/immersion-day-app-php7.tar.gz  
   tar xvfz immersion-day-app-php7.tar.gz
fi

# Install the AWS SDK for PHP
if [ ! -f /var/www/html/aws.zip ]; then
   cd /var/www/html
   mkdir vendor
   cd vendor
   wget https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip
   unzip aws.zip
fi

# Update existing packages
yum -y update

Amazon Linux 2 AMI์—์„œ LAMP(Linux, Apache, MariaDB, PHP) ์Šคํƒ์„ ์„ค์น˜ํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ์ด๋‹ค.

์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ํ…Œ๋ผํผ์œผ๋กœ ์ƒ์„ฑ๋œ ์ƒˆ EC2 ์ธ์Šคํ„ด์Šค์—์„œ ์‹คํ–‰๋œ๋‹ค.

์ด๋Ÿฐ ํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค๋ฉด ok!

 

 

 

๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ƒ์„ฑ ๋ฐ ์—ฐ๊ฒฐ ์ž‘์—…์€ ๋‹ค์Œ ๊ธ€์—์„œ ์ด์–ด์„œ ์ž‘์„ฑํ•˜๊ฒ ๋‹ค! ๐Ÿ‘พ

'DevOps > Terraform' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Terraform] 2. ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์ƒ์„ฑ ๋ฐ ์—ฐ๊ฒฐ  (0) 2023.08.08

๋Œ“๊ธ€