# 배포 자료

## Overview

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FqsgV4BvIz6XhSoEqCHde%2Fshutterstock_611605280.jpg?alt=media\&token=0197f731-f1c7-42ce-b79b-60e425060584)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fw8fj25UKNOpt73rPEGVz%2Fline.png?alt=media\&token=b7554112-5a73-42fe-87f6-e7e603a0d8ac)

What is **Cloud Computing** ?

* **IT 리소스**를 인터넷을 통해 **온디맨드로 제공**하고 **사용한 만큼만 비용을 지불**하는 것을 말합니다.
* 물리적 데이터 센터와 서버를 구입, 소유 및 유지 관리하는 대신, 클라우드 공급자로부터 필요에 따라 컴퓨팅 파워, 스토리지, 데이터베이스와 같은 기술 서비스에 액세스할 수 있습니다.
* AWS, GCP, Azure 등이 있습니다 .

**Advantages** of cloud computing&#x20;

* 민첩성
* 탄력성
* 비용 절감
* 몊 분 만에 전 세계에 배포 (지리적 리전으로 확장)

⇒ 효율적

**Types** of Cloud Computing

* **IaaS** (Infrastructure as a Service)
* **PaaS** (Platform as a Service)
* **SaaS** (Software as a Service)

{% embed url="<https://www.redhat.com/cms/managed-files/iaas-paas-saas-diagram5.1-1638x1046.png>" %}
<https://www.redhat.com/ko/topics/cloud-computing/iaas-vs-paas-vs-saas>
{% endembed %}

Top 4 **AWS Services**

1. Amazon **EC2** (Elastic Compute Cloud)
2. Amazon **RDS** (Relational Database Services)
3. Amazon **S3** (Simple Storage Service)
4. Amazon **Lambda**

## Setup and requirements

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2F33juGXU5X2eYTynuVu8I%2Fline.png?alt=media\&token=cd26b8d2-5628-4e5e-9c20-9519920d3f3b)

#### **AWS 계정 생성**

{% embed url="<https://aws.amazon.com/ko/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free+Tier+Categories=*all&awsf.Free+Tier+Types=*all&ef_id=Cj0KCQjwxIOXBhCrARIsAL1QFCZialvEjCDOYR_bm3XFpvdcx-IY1o2Xfoj1UvTPxSWglVvMK4LQZb8aAgddEALw_wcB:G:s&s_kwcid=AL!4422!3!563761819873!p!!g!!aws&s_kwcid=AL!4422!3!563761819873!p!!g!!aws&sc_channel=ps&trk=d66b5e73-988d-4ff9-aa68-e067ce087ab2>" %}

#### **IAM 사용자 생성**

{% embed url="<https://ukayzm.github.io/aws-create-iam-user/>" %}

## **Simple Deployment with AWS EC2**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FczEqNTvZjjaNIP15yzxs%2Fline.png?alt=media\&token=217530cf-ecc7-47d1-9fec-cda2069dbcfe)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FyQChPeWVyCEWMC6J2Z6T%2Fshutterstock_1931225858.jpg?alt=media\&token=9482082b-226b-4517-9d32-bd84ab71755f)

### Task 1. Create EC2 instance

**Connect to console**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2F52jmMY6kxAWE3OFAv23Z%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-07-27%20%E1%84%8B%E1%85%A9%E1%84%8C%E1%85%A5%E1%86%AB%2012.40.33.png?alt=media\&token=d54c6b4a-5eca-4d7d-b879-27335a9acb1b)

**Start Instance**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2F8V4Ul1eCYPLSHDybq7xO%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202022-07-29%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%204.30.23.png?alt=media\&token=b2378467-9029-42cd-a633-7a882efff49b)

**Start setting**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FzpJHNKJQ1jp39d9TLZc7%2F3.png?alt=media\&token=24270a3c-4ff8-4614-9b66-b3cdb657d989)

**Add** **Name and Tag**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fghulbi8fDxtUFzk47sJC%2F4_name_tag.png?alt=media\&token=6063a039-4534-4f89-9752-0147a0ed798a)

**Application & OS Image (AMI)**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2F2X3aFARWV57KP982hk70%2F5_ami.png?alt=media\&token=36b28c53-5b4b-4cde-95ac-d1bff1126ad9)

**Create Key**

☄️  절대 노출 금지

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FFGsrSKCWS7WJ64qdXXZx%2F6_keys.png?alt=media\&token=fcb78a92-cf40-4476-9a0d-181bd453e000)

**Network settings**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FefPYnkgkWqGHhnamD6jZ%2F7_network.png?alt=media\&token=c9e441e0-3dad-4a9d-b766-3ae468986a47)

**Storage setting**

✨ 넉넉하게 설정해주면 나중에 메모리 부족으로 인한 에러 해결하는 시간을 단축할 수 있음

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FPBRRJq7HoTX1PgPPHZYj%2F8_storage.png?alt=media\&token=d160aea1-83cf-4475-bd32-68aabb9f58dc)

**Finish !!!**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FpUqZQM4Tvt6V7uo5qUBA%2F9_finish.png?alt=media\&token=6c8b7336-49c8-4d2a-907a-b6fdea9ff1c5)

**Verify**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fz5IqMRvK3Ke9svwcnAhk%2F10_verify.png?alt=media\&token=ca35ab7c-245e-4dc9-a679-46903a30e0c9)

### Task 2. Create Elastic IP

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FsouQ6tbUtSmYAMwP03AO%2F1.png?alt=media\&token=64c669af-1483-4662-a333-a4514eb0cee3)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FFQVrvAn0IN9L8zvwext1%2F2.png?alt=media\&token=fd3baf03-d045-4ada-9e6b-9be36061cc92)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FgLr6Tuy0s2DPXrmBDq13%2F3.png?alt=media\&token=3dd31e10-5740-48bf-bb29-e2083328a6ce)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FrhaKEgg23ZkRrLTOpKIx%2F4.png?alt=media\&token=b45b422d-a19b-4505-b15a-3af935dcbccf)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FbCvsvNcDkkY4PxDa2xSS%2F5.png?alt=media\&token=5f8a1c14-116c-4230-8be0-19d2f6034238)

### Task 3. Connect to EC2 instance

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2F3roeRlSaJ59QJQ9sVDqF%2F6.png?alt=media\&token=962addb7-b400-4552-958d-366a3a946267)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FBgpQSetbfsyA4iCNfE0n%2F7.png?alt=media\&token=8cadaf35-394c-4806-a38d-08c6f0a97a17)

### Task 4. EC2 setting

#### Install and start docker

```bash
sudo yum update

# Install docker
sudo yum install docker

# Install docker-compose 
sudo curl -L <https://github.com/docker/compose/releases/latest/download/docker-compose-$>(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

# Fix permissions
sudo chmod +x /usr/local/bin/docker-compose

# Create symbolic link
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# Add user to group so we can use commands without sudo
# (+ Login again, to apply changes)
sudo usermod -a -G docker ec2-user

# Start docker
sudo service docker start

# Verify the installation
docker -v
docker-compose -v
```

#### Install nvm, Nodejs and Yarn

```bash
# install nvm
curl -o- <https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh> | bash

# Activate nvm
. ~/.nvm/nvm.sh

#Install Node.js
nvm install --lts

# Install yarn
curl -o- -L <https://yarnpkg.com/install.sh> | bash

# Reload the bash profile
source ~/.bashrc
```

#### Install git

```bash
# Install git
sudo yum install -y git
```

### Task 5. Clone sample project

**sample app**

{% embed url="<https://github.com/litsynp/docker-example>" %}

```bash
# Clone your repo
git clone <link-to-your-repo>
## e.g git clone --recursive <https://github.com/litsynp/docker-example.git>
```

**project directory structure**

```bash
❯ ls
backend             docker-compose.yml          nginx
frontend            docker-compose.prod.yml     settins
```

### Task 6. Additional S**ettings**

**환경 변수 및 시크릿 설정**

#### IP setting (sample app 기준)

* **localhost**를 **고정IP** 주소로 수정
* **.env** 파일 **DJANGO\_ALLOWED\_HOSTS** 에 고정IP 추가
* REACT\_APP\_BACKEND\_URL 수정
* 설정에 따라 필요한 값들 수정

#### Additional settings

* docker-compose build 후 makemigrateions, migrate, dollectstatic, loaddata 순서대로 설정

### Task 7. Run docker-compose

```bash
docker-compose -f docker-compose.prod.yml up -d --build
```

### Task 8. Verify

#### **고정 IP로 접근하여 확인**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fm4VYvslwPg65wqq4jKDO%2Fresult.png?alt=media\&token=4737830b-42fb-4a3c-be39-b2bb93fe8655)

## Create domain with AWS Route 53

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fw8fj25UKNOpt73rPEGVz%2Fline.png?alt=media\&token=b7554112-5a73-42fe-87f6-e7e603a0d8ac)

### Task 1. Register domain

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FPPtBuNSIjZF0sfhxuXI5%2F1.png?alt=media\&token=05d4dc95-7f92-4b1c-8f18-306b2ae91ad7)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fv0QxYAUX0VzsTOcJbk9A%2F2.png?alt=media\&token=e1e0d8f0-aebc-4430-8897-893a765ee6a7)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FKc3eyVPIlMUN41WDl7Zp%2F3.png?alt=media\&token=6cd7530b-164c-4c00-afd9-91e200e45e10)

#### Finish register domain !

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FDjbbiwD2Xvh5TZx6qGI6%2F4.png?alt=media\&token=8ec9effe-7550-4358-aeb4-66539c564fd7)

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FkfGtKBBho1usDOZpVrpi%2F5.png?alt=media\&token=50c5dab2-3a25-417c-85fa-82a3c6e2dcfd)

### Task 2. Create Record

#### **도메인 이름과 IP 주소 연결**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FYwYkRIYArWntDdB3A4CW%2F6.png?alt=media\&token=2677a74c-2545-4555-85e9-5d5282c98ccc)

#### **레코드 생성**

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2FQ1bdAEh18dkc6ZQzZ9Fb%2F7.png?alt=media\&token=1af21272-d98f-4f57-94f4-2f5d4b736cf3)

이제 [www.judyyy.com](http://www.judyyy.com) 으로 접속 해보자

> ‼️  데이터가 사라졌다

### Task 3. Domain settings

**sample app 에서 다음과 같은 항목들을 업데이트 해준다.**

* .env 파일 **DJANGO\_ALLOWED\_HOSTS** 에 도메인 주소 추가
  * ex. `domain.kr` `www.domain.kr` 둘 다 필요
* `nginx.conf` 에 `listen 8080` 아래에 `server_name`
  * ex. `domain.kr` `www.domain.kr`; 둘 다 넣어줘야함
* REACT\_APP\_BACKEND\_URL 수정

다시 접속해보자 !!

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fr9VjvWyRFh6TYJMIsD67%2F9.png?alt=media\&token=112cba97-8090-4367-9ad7-d6d375be98c1)

{% embed url="<http://www.judyyy.com/>" %}

🙌🏻 🙌🏻 🙌🏻

## Trouble Shooting

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fw8fj25UKNOpt73rPEGVz%2Fline.png?alt=media\&token=b7554112-5a73-42fe-87f6-e7e603a0d8ac)

✔️ **No space left on device on my EC2 AWS Instance**

{% embed url="<https://medium.com/@wlarch/no-space-left-on-device-on-my-ec2-aws-instance-cfbd69fba37a>" %}

✔️ docker-compose INTERNAL ERROR: cannot create temporary directory

{% embed url="<https://stackoverflow.com/questions/40755494/docker-compose-internal-error-cannot-create-temporary-directory>" %}

✔️ 메모리 부족 현상을 해결하기 위한 Swap memory 설정

{% embed url="<https://wingsnote.com/116>" %}

```bash
$ sudo dd if=/dev/zero of=/swapfile bs=1K count=16M
# (1K * 16M = 16 GiB)
```

* **오류:** `swapon: /swapfile: insecure permissions 0644, 0600 suggested.`
  * <https://github.com/Stadicus/RaspiBolt/issues/37>

    ```bash
    $ sudo chmod 0600 /swapfile
    ```

✔️ `.pem` 파일로 vscode에서 Remote SSH 설정 \_ mac

{% embed url="<https://tttap.tistory.com/141>" %}

✔️ `.pem` 파일로 vscode에서 Remote SSH 설정 \_ window 10

{% embed url="<https://swiftcoding.org/lightsail-from-window10>" %}

✔️ vscode 코드 저장 오류

vscode에서 ssh로 연결 후 코드 수정 및 저장 시 (NoPermissions (FileSystemError): Error: EACCES: permission denied, open \~) 오류 나면, `/home` 에서

```bash
sudo chown -R $USER 디렉토리경로
```

✔️ vscode 도커 확장 프로그램 설치 오류: `Error: connect EACCES /var/run/docker.sock` 해결하기

{% embed url="<https://github.com/tesYolan/AvatarOrchestrator/issues/35>" %}

```jsx
sudo chmod 777 /var/run/docker.sock
```

✔️ `npm install` 중 오류: `JavaScript heap out of memory`

{% embed url="<https://stackoverflow.com/questions/40112279/npm-install-javascript-heap-out-of-memory>" %}

```bash
node --max-old-space-size=8000 $(which npm) install # npm install 실행
```

## Reference

![](https://2669959621-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxncaDDrGpdjhVy86HbwM%2Fuploads%2Fw8fj25UKNOpt73rPEGVz%2Fline.png?alt=media\&token=b7554112-5a73-42fe-87f6-e7e603a0d8ac)

* [AWS 배포(공유용)](https://www.notion.so/AWS-b7db4c9b1b8945b18f5340d9a9a40440)
* [클라우드 컴퓨팅이란 | 클라우드 컴퓨팅 소개 | Amazon Web Services](https://aws.amazon.com/ko/what-is-cloud-computing/)
* [Deploy to AWS using Docker compose (simple)](https://medium.com/@umairnadeem/deploy-to-aws-using-docker-compose-simple-210d71f43e67)
* [Deploy Node.js application with MySQL database to AWS EC2 using Docker Compose](https://medium.com/hackernoon/deploy-node-js-application-with-mysql-database-to-aws-ec2-using-docker-compose-3e5034c034ce)
* [자습서: Amazon EC2 인스턴스에서 Node.js 설정](https://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html)
* [솜씨좋은장씨](https://somjang.tistory.com/entry/Docker-Amazon-Linux2-AMI-%EC%97%90%EC%84%9C-docker-compose-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0)
* [\[AWS\] Route 53으로 DNS와 IP 주소 연결하는 법](https://devlog-wjdrbs96.tistory.com/292)

### Notion ver.

{% embed url="<https://soobin1412.notion.site/Deploy-to-AWS-c694c7f9bcac48198806d353910efb04>" %}

rimages
