cloud bee

Django 이론 & AWS 환경 실습 구현 본문

AWS/infra

Django 이론 & AWS 환경 실습 구현

who you 2023. 1. 18. 01:34

궁금해 하는 사람

우선 Django가 과연 무엇일까? 처음에 접할 때 많이 궁금한 사항이었을 것이다.
그리하여 Django가 무엇인지에 대해 한번 알아보도록 할 것이다.


Django!?

Django는 장고라고 불리며, 파이썬으로 만들어진 무료 오픈소스 웹 애플리케이션 프레임 워크라고 한다.

Django를 사용하는 간단한 이유는 다음과 같다.
-> 웹사이트를 구축할 때 Web Api를 쉽게 구현할 수 있기 때문이다.
-> 비용도 쉽게 절약할 수 있기 때문이다.
-> Django 코드 구조는 매우 효율적이라서 개발자가 많은 기능을 쉽게 추가할 수 있다.
예시는 다음과 같다. 로그인 기능추가, 회원가입 기능 추가, 파일 업로드 기능, 사용자 생성 기능 등이 존재한다.

누군가 서버에 웹 사이트 요청할 경우 장고에서 일어나는 일은 다음과 같다.

1. 웹 서버의 요청이 올 때는 장고로 전달된다
2. 장고 urlresolver는 웹 페이지의 주소를 가져와 무엇을 할지 확인한다.
3. 패턴 목록을 가져오고 URL과 맞는지 처음부터 하나씩 대조해 식별한다.
4. 일치 패턴 존재 시 view에 넘겨준다.

장고가 따르는 패턴은 다음과 같다.
- 모델-템플릿-뷰 패턴이다.


AWS 환경에서 실습 구현

다음 깃허브 자료를 활용하여 구현할 것이다.
https://github.com/daniel-jebarson/pokemon-website

 

GitHub - daniel-jebarson/pokemon-website: This is the website created with django and python

This is the website created with django and python - GitHub - daniel-jebarson/pokemon-website: This is the website created with django and python

github.com


우선 먼저 아래 코드를 참고하고, cloudformation을 통해 vpc-stack을 생성해 준다.

Parameters:
  EnvironmentName:
    Description: An environment name that is prefixed to resource names
    Type: String
    Default: "skills"

  VpcCIDR:
    Description: Please enter the IP range (CIDR notation) for this VPC
    Type: String
    Default: 10.0.0.0/16

  PublicSubnet1CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
    Type: String
    Default: 10.0.10.0/24

  PublicSubnet2CIDR:
    Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
    Type: String
    Default: 10.0.11.0/24

  PrivateSubnet1CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
    Type: String
    Default: 10.0.20.0/24

  PrivateSubnet2CIDR:
    Description: Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone
    Type: String
    Default: 10.0.21.0/24

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-vpc

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: !Ref PublicSubnet1CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-a

  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 2, !GetAZs  '' ]
      CidrBlock: !Ref PublicSubnet2CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-c

  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet1CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-a

  PrivateSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 2, !GetAZs  '' ]
      CidrBlock: !Ref PrivateSubnet2CIDR
      MapPublicIpOnLaunch: false
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-c

  NatGateway1EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc

  NatGateway2EIP:
    Type: AWS::EC2::EIP
    DependsOn: InternetGatewayAttachment
    Properties:
      Domain: vpc

  NatGateway1:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway1EIP.AllocationId
      SubnetId: !Ref PublicSubnet1

  NatGateway2:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt NatGateway2EIP.AllocationId
      SubnetId: !Ref PublicSubnet2

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-public-rt

  DefaultPublicRoute:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet1

  PublicSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet2


  PrivateRouteTable1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-a-rt

  DefaultPrivateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway1

  PrivateSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable1
      SubnetId: !Ref PrivateSubnet1

  PrivateRouteTable2:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-private-c-rt

  DefaultPrivateRoute2:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable2
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway2

  PrivateSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRouteTable2
      SubnetId: !Ref PrivateSubnet2


다음 명령어를 사용하여 stack을 생성해 주도록 한다.
aws cloudformation create-stack --stack-name vpc-stack --template-body file://vpc.yml

이제 간단하게 보안그룹을 생성해 주도록 한다.
( bastion-SG를 생성한다. )
( 음 테스트 용으로 8000번도 뚫어준다. bastion으로 간단하게 테스트할 것이다. )

보안그룹 설정

 

bastion 이름 지정


Type을 다음과 같이 t3.micro로 지정해 준다.
그리고 키페어를 부여해 준다.

인스턴스 유형 선택



이제 다음과 같이 public-a 서브넷을 선택하고, 보안그룹을 선택해 준다.

서브넷 마스크 선택


이후 생성을 진행해 준다.

이제 인스턴스에 들어가서 다음 명령어를 입력해 Django 환경을 구성한다.
다음으로 bash 명령어를 입력해 준다.

cd ~

yum install python-pip -y

yum install python3-devel -y

yum install postgresql-devel python3-devel -y

yum install gcc -y

pip install psycopg2-binary

wget https://github.com/daniel-jebarson/pokemon-website/archive/refs/heads/main.zip

unzip main.zip

cd pokemon-website-main

python3 -m venv env

source env/bin/activate

pip install django==2.2

pip install wheel

pip install django-heroku

pip install psycopg2-binary

pip install python-decouple

wget https://www.sqlite.org/snapshot/sqlite-snapshot-202106031851.tar.gz
mv sqlite-snapshot-202106031851.tar.gz /usr/src
cd /usr/src
tar -xvf sqlite-snapshot-202106031851.tar.gz
cd sqlite-snapshot-202106031851
./configure
make
make install

cp /usr/local/bin/sqlite3 /usr/bin/sqlite3
export LD_LIBRARY_PATH="/usr/local/lib"

pip install requests

이제 다음 명령어를 실행하여 장고 마이그래이션을 진행한다.

python manage.py makemigrations
python manage.py migrate

마이그래이션


이제 admin 유저를 생성해 준다.
python manage.py createsuperuser

유저 생성

이제 다음 명령어를 실행하여 장고를 실행시킨다.

python manage.py runserver 0.0.0.0:8000

이후 해당 인스턴스의 EIP로 접속한다.

접근 테스트 1



admin 경로로 접속한다.

접근 테스트 2
접근 테스트 3



이제 Pokedex에 들어가 본다.
다음과 같이 Pokedex/<포켓몬이름>을 적으라고 뜬다.

접근 테스트 4



/Pokedex/Pikachu에 들어가 본다.

결과물 1



/Pokedex/Charizard/ 에 들어가 본다.

결과물 2

'AWS > infra' 카테고리의 다른 글

AWS WAF 개념 정리  (0) 2023.02.04
EC2 로그인 실패시 SQS 알림 전송  (0) 2023.02.01
Amazon RDS 정리  (0) 2023.01.28
web server와 was  (0) 2023.01.25
스팟 인스턴스 vs 예약 인스턴스 vs 온디맨드 인스턴스  (4) 2023.01.15
Comments