본문 바로가기
강의 실습/비전공자도 이해할 수 있는 AWS 입문 실전

[실습] 5. Express 서버에 RDS 연결하기

by jint 2026. 1. 6.

1. EC2 인스턴스 생성
EC2 인스턴스 서비스에 접속하여 리전(Region)을 아시아 태평양 (서울)로 선택
"인스턴스 시작" 버튼 클릭

- 이름 : test-server

- 애플리케이션 및 OS 이미지(Amazon Machine Image) : Ubuntu Server 24.04 LTS (HVM), SSD Volume Type

- 인스턴스 유형 : t2.micro

- 키 페어
키 페어 없이 계속 진행(권장되지 않음)

- 네트워크 설정 - 방화벽(보안 그룹)
보안 그룹 이름 : test-server-security-group
설명 : test-server-security-group created 2026-01-06T13:06:17.211Z
인바운드 보안 그룹 규칙)
보안 그룹 규칙 1 (TCP, 22, 0.0.0.0/0)
유형 : ssh
프로토콜 : TCP
포트 범위 : 22
소스 유형 : 위치 무관

보안 그룹 규칙 2 (TCP, 80, 0.0.0.0/0)
유형 : HTTP
프로토콜 : TCP
포트 범위 : 80
소스 유형 : 위치 무관

- 스토리지 구성
크기(GiB) : 8
볼륨 유형 : gp2

"인스턴스 시작" 버튼 클릭


2. EC2 인스턴스 연결 후 Express 서버를 위한 node.js 설치
- Ubuntu

ubuntu@ip-172-31-12-219:~$ sudo su
root@ip-172-31-12-219:/home/ubuntu# apt-get update && /
apt-get install -y ca-certificates curl gnupg && /
mkdir -p /etc/apt/keyrings && /
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && /
NODE_MAJOR=20 && /
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list && /
apt-get update && /
apt-get install nodejs -y
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:4 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 Packages [15.0 MB]
Get:5 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]        
Get:6 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe Translation-en [5982 kB]
Get:7 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 Components [3871 kB]
Get:8 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 c-n-f Metadata [301 kB]
Get:9 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse amd64 Packages [269 kB]
Get:10 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse Translation-en [118 kB]
Get:11 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse amd64 Components [35.0 kB]
Get:12 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble/multiverse amd64 c-n-f Metadata [8328 B]
Get:13 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1684 kB]
Get:14 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main Translation-en [311 kB]
Get:15 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [175 kB]
Get:16 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 c-n-f Metadata [15.8 kB]
Get:17 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [1506 kB]
Get:18 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe Translation-en [306 kB]
Get:19 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [378 kB]
Get:20 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 c-n-f Metadata [31.4 kB]
Get:21 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Packages [2413 kB]
Get:22 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted Translation-en [550 kB]
Get:23 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Components [212 B]
Get:24 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 c-n-f Metadata [516 B]
Get:25 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Packages [30.3 kB]
Get:26 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse Translation-en [6048 B]
Get:27 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Components [940 B]
Get:28 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 c-n-f Metadata [488 B]
Get:29 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 Packages [40.4 kB]
Get:30 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main Translation-en [9208 B]
Get:31 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 Components [7328 B]
Get:32 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 c-n-f Metadata [368 B]
Get:33 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [29.5 kB]
Get:34 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe Translation-en [17.9 kB]
Get:35 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Components [10.5 kB]
Get:36 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 c-n-f Metadata [1444 B]
Get:37 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 Components [216 B]
Get:38 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 c-n-f Metadata [116 B]
Get:39 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:40 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 c-n-f Metadata [116 B]
Get:41 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [1391 kB]
Get:42 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [225 kB]                                                                                                                             
Get:43 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [21.5 kB]                                                                                                                          
Get:44 http://security.ubuntu.com/ubuntu noble-security/main amd64 c-n-f Metadata [9504 B]                                                                                                                       
Get:45 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [916 kB]                                                                                                                         
Get:46 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [207 kB]                                                                                                                         
Get:47 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [71.4 kB]                                                                                                                      
Get:48 http://security.ubuntu.com/ubuntu noble-security/universe amd64 c-n-f Metadata [19.4 kB]                                                                                                                  
Get:49 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Packages [2286 kB]                                                                                                                      
Get:50 http://security.ubuntu.com/ubuntu noble-security/restricted Translation-en [523 kB]                                                                                                                       
Get:51 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B]                                                                                                                      
Get:52 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Packages [27.4 kB]                                                                                                                      
Get:53 http://security.ubuntu.com/ubuntu noble-security/multiverse Translation-en [5956 B]                                                                                                                       
Get:54 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [208 B]                                                                                                                      
Get:55 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 c-n-f Metadata [384 B]                                                                                                                  
Fetched 39.2 MB in 14s (2783 kB/s)                                                                                                                                                                               
Reading package lists... Done
bash: /: Is a directory
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
ca-certificates is already the newest version (20240203).
ca-certificates set to manually installed.
curl is already the newest version (8.5.0-2ubuntu10.6).
curl set to manually installed.
gnupg is already the newest version (2.4.4-2ubuntu17.3).
gnupg set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 68 not upgraded.
bash: /: Is a directory
bash: /: Is a directory
bash: /: Is a directory
bash: /: Is a directory
deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main
bash: /: Is a directory
Hit:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble InRelease
Hit:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease                        
Hit:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease                      
Get:4 https://deb.nodesource.com/node_20.x nodistro InRelease [12.1 kB]                                  
Get:5 https://deb.nodesource.com/node_20.x nodistro/main amd64 Packages [13.2 kB]                       
Hit:6 http://security.ubuntu.com/ubuntu noble-security InRelease
Fetched 25.3 kB in 1s (43.0 kB/s)
Reading package lists... Done
bash: /: Is a directory
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 68 not upgraded.
Need to get 32.0 MB of archives.
After this operation, 197 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_20.x nodistro/main amd64 nodejs amd64 20.19.6-1nodesource1 [32.0 MB]
Fetched 32.0 MB in 1s (59.9 MB/s)
Selecting previously unselected package nodejs.
(Reading database ... 71735 files and directories currently installed.)
Preparing to unpack .../nodejs_20.19.6-1nodesource1_amd64.deb ...
Unpacking nodejs (20.19.6-1nodesource1) ...
Setting up nodejs (20.19.6-1nodesource1) ...
Processing triggers for man-db (2.12.0-4build2) ...
Scanning processes...                                                                                                                                                                                             
Scanning linux images...                                                                                                                                                                                          

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
root@ip-172-31-12-219:/home/ubuntu# node -v
v20.19.6



3. EC2 인스턴스에서 Express 서버 clone
링크 : https://github.com/JSCODE-EDU/rds-express-sequelize-sample

 

GitHub - JSCODE-EDU/rds-express-sequelize-sample: <비전공자도 이해할 수 있는 AWS 입문/실전> 中 예제 프로젝

<비전공자도 이해할 수 있는 AWS 입문/실전> 中 예제 프로젝트. Contribute to JSCODE-EDU/rds-express-sequelize-sample development by creating an account on GitHub.

github.com

 

- Ubuntu

root@ip-172-31-12-219:/home/ubuntu# git clone https://github.com/JSCODE-EDU/rds-express-sequelize-sample.git
Cloning into 'rds-express-sequelize-sample'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 0), reused 7 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (7/7), 11.40 KiB | 2.85 MiB/s, done.
root@ip-172-31-12-219:/home/ubuntu# ls
rds-express-sequelize-sample
root@ip-172-31-12-219:/home/ubuntu# cd rds-express-sequelize-sample
root@ip-172-31-12-219:/home/ubuntu/rds-express-sequelize-sample# ls
app.js  package-lock.json  package.json
root@ip-172-31-12-219:/home/ubuntu/rds-express-sequelize-sample# cat app.js
const express = require('express')
const app = express()
const port = 80;
require('dotenv').config()
const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(
  process.env.DATABASE_NAME,
  process.env.DATABASE_USERNAME,
  process.env.DATABASE_PASSWORD,
  {
    host: process.env.DATABASE_HOST,
    dialect: 'mysql'
  }
);

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, async () => {
  try {
    await sequelize.authenticate();
    console.log('DB 연결 성공!');
  } catch (err) {
    console.log('DB 연결 X', err);
  }
  console.log(`Example app listening on port ${port}`)
})


app.js 소스코드에서 sequelize란, 데이터베이스를 연결시켜주는 ORM같은 라이브러리이다.
백엔드 서버가 데이터베이스와 연결하기 위한 코드이다.


4. .env 파일 수정
주의) .env 파일은 반드시 .gitignore에 추가해서 Github에 올라가지 않게 해야 한다.

- Ubuntu

root@ip-172-31-12-219:/home/ubuntu/rds-express-sequelize-sample# ls -al
total 68
drwxr-xr-x 3 root   root    4096 Jan  6 13:19 .
drwxr-x--- 5 ubuntu ubuntu  4096 Jan  6 13:19 ..
-rw-r--r-- 1 root   root     138 Jan  6 13:19 .env
drwxr-xr-x 8 root   root    4096 Jan  6 13:19 .git
-rw-r--r-- 1 root   root      13 Jan  6 13:19 .gitignore
-rw-r--r-- 1 root   root     634 Jan  6 13:19 app.js
-rw-r--r-- 1 root   root   37036 Jan  6 13:19 package-lock.json
-rw-r--r-- 1 root   root     130 Jan  6 13:19 package.json
root@ip-172-31-12-219:/home/ubuntu/rds-express-sequelize-sample# vi .env


clone 받고 .env 파일이 있는데 원래는 이렇게 구성하면 안된다. 편의상 사용하기 좋게 올려놓은 것이다.
.env 파일에 RDS에 연결하기 위한 정보를 입력한다.

- Ubuntu Vim

DATABASE_NAME=test
DATABASE_USERNAME=admin
DATABASE_PASSWORD=testdb123
DATABASE_HOST=test-db.cbmowk46y5r2.ap-northeast-2.rds.amazonaws.com
~                                                                                                                                                                                                                 
~                                                                                                                                                                                                                 
~                                                                                                                                                                                                                 
~                                                                                                                                                                                                                 
:wq


데이터베이스 연결시 위처럼 크게 4가지 값을 많이 쓴다.


5. Express 서버가 RDS와 잘 연결되는 지 확인
라이브러리 설치 후 서버를 실행한다.

- Ubuntu

root@ip-172-31-12-219:/home/ubuntu/rds-express-sequelize-sample# npm i

added 97 packages, and audited 98 packages in 5s

13 packages are looking for funding
  run `npm fund` for details

9 vulnerabilities (3 low, 5 high, 1 critical)

To address all issues, run:
  npm audit fix

Run `npm audit` for details.
npm notice
npm notice New major version of npm available! 10.8.2 -> 11.7.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.7.0
npm notice To update run: npm install -g npm@11.7.0
npm notice
root@ip-172-31-12-219:/home/ubuntu/rds-express-sequelize-sample# node app.js
Executing (default): SELECT 1+1 AS result
DB 연결 성공!
Example app listening on port 80


성공적으로 RDS에 연결된 것을 확인할 수 있다.


참고링크 : https://www.inflearn.com/course/%EB%B9%84%EC%A0%84%EA%B3%B5%EC%9E%90-%EC%9D%B4%ED%95%B4%ED%95%A0%EC%88%98%EC%9E%88%EB%8A%94-aws-%EC%9E%85%EB%AC%B8%EC%8B%A4%EC%A0%84

 

비전공자도 이해할 수 있는 AWS 입문/실전| JSCODE 박재성 - 인프런 강의

현재 평점 4.9점 수강생 2,883명인 강의를 만나보세요. 비전공자 입장에서도 쉽게 이해할 수 있고, 실전에서 바로 적용 가능한 AWS 입문 강의를 만들어봤습니다! EC2를 활용한 백엔드 API 서버 배포, R

www.inflearn.com

댓글