본문 바로가기

Development/AWS

[AWS] Elastic Beanstalk

728x90

Elastic Beanstalk logo

Elastic Beanstalk is a Platform As A Service (PaaS) that streamlines the setup, deployment, and maintenance of your app on Amazon AWS. It's a managed service, coupling the server (EC2), database (RDS), and your static files (S3). You can quickly deploy and manage your application, which automatically scales as your site grows.

Elastic Beanstalk에 접속하기 위해서는 CLI를 사용하면 된다.

To work with a Amazon Elastic Beanstalk, we can use a package called awsebcli. As of this writing the latest version of is 3.0.10 and the recommended way to install it is with pip:

$ pip install awsebcli

설치가 끝난 다음, 다음 명령어를 통해서 설치가 잘 되었는지 확인하면 된다.

$ eb --version

 

.ebextensions

AWS Elastic Beanstalk 구성 파일(.ebextensions)을 웹 애플리케이션의 소스 코드에 추가하여 환경을 구성하고 포함된 AWS 리소스를 사용자 지정할 수 있습니다. 구성 파일은 .config 파일 확장명을 사용하는 YAML이나 JSON 형식 문서로, .ebextensions 폴더에 놓고 애플리케이션 소스 번들로 배포합니다.

예시 .ebextensions/network-load-balancer.config

이 예에서는 간단한 구성을 변경합니다. 해당 환경의 로드 밸런서 유형을 Network Load Balancer로 설정하기 위해 구성 옵션을 수정합니다.

option_settings:
    aws:elasticbeanstalk:environment:
        LoadBalancerType: network

구성 파일에 JSON보다 더 쉽게 읽을 수 있는 YAML을 사용하는 것이 좋습니다. YAML은 설명과 복수 명령줄, 따옴표를 사용할 수 있는 몇몇 대안 등을 지원합니다. 하지만 YAML이나 JSON을 사용해 동일한 방식으로 Elastic Beanstalk 구성 파일의 구성을 변경할 수 있습니다.

Handling database migrations

With our database setup, we still need to make sure that migrations are ran so that the database table structure is correct. We can do that by modifying .ebextensions/django.config and adding the following lines at the top of the file:

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

container_commands allow you to run arbitrary commands after the application has been deployed on the EC2 instance. Because the EC2 instance is set up using a virtual environment, we must first activate that virtual environment before running our migrate command.

Also the leader only: true setting means, "Only run this command on the first instance when deploying to multiple instances".

Don't forget that our application makes use of Django's admin, so we are going to need a superuser

728x90