๐Ÿ“ฆ ElasticBeanstalk ์‹œ์ž‘ํ•˜๊ธฐ

๊ฐœ์š”

ํ† ์ด ํ”„๋กœ์ ํŠธ ๋ฐฐํฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ ๊ณ ๋ฏผํ•˜๋˜ ์ค‘์— AWS ํ”„๋ฆฌ ํ‹ฐ์–ด๋ฅผ ๋งˆ์Œ๊ป ํ™œ์šฉํ•ด๋ณด์ž! ์ƒ๊ฐํ•ด์„œ ElasticBeanstalk์œผ๋กœ ๋ฐฐํฌ๋ฅผ ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

์—ฌํƒœ๊นŒ์ง€๋Š” ์ค„๊ณง EC2๋กœ๋งŒ ๋ฐฐํฌ๋ฅผ ํ•ด์™”์—ˆ๋Š”๋ฐ ElasticBeanstalk์„ ์ด์šฉํ•˜๋ฉด ํ›จ์”ฌ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๋ฐฐํฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋“ค์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ง์ ‘ ํ•ด๋ณด๊ธฐ ์ „๊นŒ์ง€๋Š” ๊ทธ ํŽธ์˜์„ฑ์„ ์ž˜ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋„์ „ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

ElasticBeanstalk์ด๋ž€?

ElasticBeanstalk๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๋Š” ์ธํ”„๋ผ์— ๋Œ€ํ•œ ์—ผ๋ ค ์—†์ด AWS ํด๋ผ์šฐ๋“œ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹ ์†ํ•˜๊ฒŒ ๋ฐฐํฌ ๋ฐ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ํƒ ๋˜๋Š” ์ œ์–ด์— ๋Œ€ํ•œ ์ œ์•ฝ ์—†์ด Elastic Beanstalk์˜ ๊ด€๋ฆฌ ๋ณต์žก์„ฑ์ด ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—…๋กœ๋“œํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด Elastic Beanstalk์—์„œ ์šฉ๋Ÿ‰ ํ”„๋กœ๋น„์ €๋‹, ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ, ์กฐ์ •, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒํƒœ ๋ชจ๋‹ˆํ„ฐ๋ง์— ๋Œ€ํ•œ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์‹ค EC2์— ์›น ์„œ๋ฒ„(nginX ๋“ฑ)๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ๋ฐฐํฌํ•˜๋Š” ๊ณผ์ •์ด ๊ฒฐ์ฝ” ์‰ฝ์ง€๋งŒ์€ ์•Š์•˜๋‹ค. (๋‚ด ๊ธฐ์ค€)

ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ requirements.txt ์„ค์น˜ํ•˜๊ณ .. uwsgi ์„ค์ •ํ•˜๊ณ ..

ํ•˜์ง€๋งŒ ElasticBeanstalk์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ทธ๋Ÿฌํ•œ ๊ณผ์ •๊ณผ ์‹œ๊ฐ„๋“ค์ด ์—„์ฒญ๋‚˜๊ฒŒ ์••์ถ•๋œ๋‹ค๋Š” ์‚ฌ์‹ค! ๋น -๋ฐค

์•Œ์•„์„œ EC2๋„ ์ƒ์„ฑํ•ด์ฃผ๊ณ  .config ํŒŒ์ผ๋กœ ๋ฐฐํฌ ์„ค์ •๊นŒ์ง€ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

EB CLI ์„ค์น˜

ํ•„์ž๋Š” mac์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ macOS์šฉ ์„ค์น˜ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค.

pip๋ฅผ ์ด์šฉํ•ด ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ํ•„์ž๋Š” brew๋กœ ์„ค์น˜ํ–ˆ๋‹ค.

(๋‹ค๋ฅธ OS ํ™˜๊ฒฝ์—์„œ ์„ค์น˜ํ•˜์‹ค ๋ถ„๋“ค์€ AWS docs๋ฅผ ์ฐธ๊ณ ํ•˜์ž.)

$ brew update
$ brew install awsebcli
$ eb --version
EB CLI 3.16.0 (Python 3.6.5)

EB CLI ๊ตฌ์„ฑ

๋ฐฐํฌํ•  ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

์„œ๋ฒ„์˜ ๋ฆฌ์ „ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์—”ํ„ฐ๋ฅผ ๋ˆ„๋ฅธ๋‹ค.

$ eb init
Select a default region
1) us-east-1 : ๋ฏธ๊ตญ ๋™๋ถ€(๋ฒ„์ง€๋‹ˆ์•„ ๋ถ๋ถ€)
2) us-west-1 : ๋ฏธ๊ตญ ์„œ๋ถ€(์บ˜๋ฆฌํฌ๋‹ˆ์•„ ๋ถ๋ถ€ ์ง€์—ญ)
3) us-west-2 : ๋ฏธ๊ตญ ์„œ๋ถ€(์˜ค๋ ˆ๊ณค)
4) eu-west-1 : ์œ ๋Ÿฝ(์•„์ผ๋žœ๋“œ)
5) eu-central-1 : ์œ ๋Ÿฝ(ํ”„๋ž‘ํฌํ‘ธ๋ฅดํŠธ)
6) ap-south-1 : ์•„์‹œ์•„ ํƒœํ‰์–‘(๋ญ„๋ฐ”์ด)
7) ap-southeast-1 : ์•„์‹œ์•„ ํƒœํ‰์–‘(์‹ฑ๊ฐ€ํฌ๋ฅด)
8) ap-southeast-2 : ์•„์‹œ์•„ ํƒœํ‰์–‘(์‹œ๋“œ๋‹ˆ)
9) ap-northeast-1 : ์•„์‹œ์•„ ํƒœํ‰์–‘(๋„์ฟ„)
10) ap-northeast-2 : ์•„์‹œ์•„ ํƒœํ‰์–‘(์„œ์šธ)
...
(default is 3): 3

AWS access id์™€ secret key๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

(์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด AWS docs๋ฅผ ํ™•์ธํ•˜์ž.)

You have not yet set up your credentials or your credentials are incorrect.
You must provide your credentials.
(aws-access-id): aws-access-id
(aws-secret-key): aws-secret-key

์ƒˆ๋กœ์šด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋ฏ€๋กœ 1์„ ์ž…๋ ฅํ•œ๋‹ค.

Select an application to use
1) [ Create new Application ]
(default is 1): 1

์ƒ์„ฑํ•  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•œ๋‹ค.

๊ธฐ๋ณธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„์€ eb init์„ ์‹คํ–‰ํ•˜๋Š” ์œ„์น˜์˜ ํด๋” ์ด๋ฆ„์ด๋‹ค.

Enter Application Name
(default is "project_name"): project_name
Application eb has been created.

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ํ”„๋ ˆ์ž„์›Œํฌ์™€ ์–ธ์–ด๋ฅผ ์„ ํƒํ•œ๋‹ค.

ํ•„์ž์˜ ํ”„๋กœ์ ํŠธ๋Š” Python๊ณผ Django๋กœ ๊ตฌ์„ฑํ–ˆ์œผ๋ฏ€๋กœ 3์„ ์ž…๋ ฅํ–ˆ๋‹ค.

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
11) Java
(default is 1): 3

EB ํ™˜๊ฒฝ์— SSH ํ‚ค ํŽ˜์–ด๋ฅผ ํ• ๋‹นํ•˜๋ ค๋ฉด y๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค๋ฉด AWS docs๋ฅผ ํ™•์ธํ•˜์ž.

Do you want to set up SSH for your instances?
(y/n): y

๊ธฐ์กด ํ‚ค ํŽ˜์–ด๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.

Select a keypair.
1) [ Create new KeyPair ]
2) ugaemi
(default is 1): 2

๋ฐฐํฌ ํ™˜๊ฒฝ ์„ค์ •

์ด์ œ ์„ค์ • ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ณด์ž.

.ebextensions ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ํ•˜์œ„์— .config ํ™•์žฅ์ž ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

ํŒŒ์ผ์˜ ๋ฒˆํ˜ธ ์ˆœ์„œ๋กœ ์‹คํ–‰ ๋˜๋ฏ€๋กœ 01_init.config, 02_package.config ์™€ ๊ฐ™์ด ์ •๋ฆฌํ•œ๋‹ค.

option_settings:
    "aws:elasticbeanstalk:container:python":
      WSGIPath: config/wsgi.py
    "aws:elasticbeanstalk:container:python:staticfiles":
      /static/: "www/static/"
    "aws:elasticbeanstalk:application:environment":
      DJANGO_SETTINGS_MODULE: config.settings

์„ค์ • ํŒŒ์ผ ์ž‘์—…์ด ๋๋‚ฌ๋‹ค๋ฉด ๋‹ค์‹œ ํ„ฐ๋ฏธ๋„๋กœ ๊ฐ€์„œ eb create๋ฅผ ๋•… ๋•Œ๋ ค ์ฃผ์ž.

$ eb create
Enter Environment Name
(default is EBDjangoProject-dev): project_name-dev

DNS CNAME์„ ์ž…๋ ฅํ•ด์ค€๋‹ค.

Enter DNS CNAME prefix
(default is EBDjangoProject-dev): project_name-dev

๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ ํƒ€์ž…์„ ์„ ํƒ ํ•ด์ค€๋‹ค.

Select a load balancer type
1) classic
2) application
3) network
(default is 1): 2

ํ”„๋กœ์ ํŠธ์— .elasticbeanstalk/config.yml ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค.

branch-defaults:
  master:
    environment: project_name
    group_suffix: null
global:
  application_name: project_name
  branch: null
  default_ec2_keyname: ugaemi
  default_platform: Python 3.6
  default_region: ap-northeast-2
  include_git_submodules: true
  instance_profile: null
  platform_name: null
  platform_version: null
  profile: eb-cli
  repository: null
  sc: git
  workspace_type: Application

์ •์ƒ์ ์œผ๋กœ ๋ฐฐํฌ๊ฐ€ ์™„๋ฃŒ ๋˜์—ˆ๋‹ค๋ฉด eb open์„ ์ž…๋ ฅํ•ด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—ด์–ด ๋ณด์ž!

์ถ”๊ฐ€ ์„ค๋ช…

profile

profile ์ด๋ฆ„์€ ๊ฐ eb ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

mac ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ ~/.aws/config ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑํ•œ ํ”„๋กœํ•„์— ๋Œ€ํ•œ ์„ค์ • ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

์—ฌ๊ธฐ ์ž‘์„ฑ ๋˜์–ด์žˆ๋Š” profile ์ด๋ฆ„๊ณผ .elasticbeanstalk/config.yml์˜ profile ์ด๋ฆ„์€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

[profile eb-cli]
aws_access_key_id = access_key_id
aws_secret_access_key = secret_access_key

DB ์„ค์ •

ํ•„์ž๋Š” RDS PostgreSQL์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, DB ์„ค์ •์—์„œ ์กฐ๊ธˆ ํ—ค๋งธ๋‹ค.

ํŠนํžˆ PostgreSQL์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์„ ๊ฐ„๊ณผํ•ด์„œ ๋งŽ์ด ๋Œ์•„๊ฐ”๋‹ค.

packages:
  yum:
    postgresql96-devel: []

๋ฐ”๋กœ ์ด ๋ถ€๋ถ„ ์ด์—ˆ๋Š”๋ฐ, ๊ฐ์ž ์‚ฌ์šฉํ•˜๋Š” ๋ฒ„์ „์„ ์ž˜ ํ™•์ธํ•˜๊ณ  ๊ทธ์— ๋งž๋Š” ํŒจํ‚ค์ง€๋ฅผ ์„ค์ • ํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค :)

container_commands

์ปจํ…Œ์ด๋„ˆ์—์„œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•„์ž๋Š” DB migrate์™€ collectstatic ๋ช…๋ น์–ด๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.

container_commands:
  01_migrate:
    command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations && python manage.py migrate"
    leader_only: true
  02_collectstatic:
    command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"

๋Š๋‚€ ์ 

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์ด์ฏค์—์„œ ๋งˆ์น˜์ง€๋งŒ, ์ž‘์„ฑํ•œ ๋ถ€๋ถ„ ์ด์™ธ์— ์›น ์ฝ˜์†”๊ณผ CLI์—์„œ ํ™•์ธํ•˜๊ณ  ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด ์ •๋ง ๋งŽ๋‹ค.

๊ทธ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•ด ๋ณด๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค!

ํ™•์‹คํžˆ ๋ช‡ ์ค„ ์•ˆ๋˜๋Š” ๋ช…๋ น์–ด๋กœ ๊ทธ๊ฐ„ EC2๋กœ ๋ฐฐํฌํ•˜๋ฉฐ ๊ณ ์ƒํ–ˆ๋˜ ์…‹ํŒ…๋“ค์„ ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์–ด ๋†€๋ผ์› ๋‹ค.

๋˜ awsebcli๋กœ ํ„ฐ๋ฏธ๋„ ํ™˜๊ฒฝ์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ๋„ ์ข‹์•˜๋‹ค.

.ebextensions๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์€ ์ข€ ๋” ๊ณต๋ถ€ํ•ด ๋ด์•ผ๊ฒ ์ง€๋งŒ, ์•„๋ฌดํŠผ ๋ฐฐํฌ๊ฐ€ ์ •๋ง ์ˆ˜์›”ํ•ด ์ง„ ๊ฒƒ์€ ์‚ฌ์‹ค์ด๋‹ค..

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space