[FastAPI] 16. Pulumi๋ฅผ ์ด์šฉํ•ด FastAPI Serverless ํ™˜๊ฒฝ ๊ตฌ์ถ•ํ•ด๋ณด๊ธฐ

๋ฐ˜์‘ํ˜•

์šฐ๋ฆฌ๋Š” ์ง€๋‚œ ๊ธ€์—์„œ FastAPI, Magnum, AWS Serverless Stack(Lambda, API Gateway)์„ ์ด์šฉํ•˜์—ฌ FastAPI ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

2022.05.22 - [Programming/Python] - [FastAPI] 15. FastAPI๋ฅผ Serverless๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• - Mangum

 

[FastAPI] 15. FastAPI๋ฅผ Serverless๋กœ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• - Mangum

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” FastAPI์™€ Serverless(์„œ๋ฒ„๋ฆฌ์Šค) ์กฐํ•ฉ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ดˆ๊ธฐ ์„œ๋ฒ„๋ฆฌ์Šค๋ฅผ ๋ฐฐ์› ์„ ๋•Œ๋Š” AWS์˜ Lambda๋ผ๋Š” ์„œ๋น„์Šค๊ฐ€ ๋‚˜์™”์„ ๋•Œ์˜€๋Š”๋ฐ์š”. ๋‹น์‹œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ณ„๋„์˜ ํ”„

blog.neonkid.xyz

ํ•˜์ง€๋งŒ AWS Lambda์™€ API Gateway๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ์ผ์ผ์ด ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ AWS GUI Console์„ ์ตํžˆ๊ณ , ์ ์‘ํ•˜๋Š”๋ฐ ๋งŽ์€ ๋Ÿฌ๋‹ ์ปค๋ธŒ ์š”์†Œ๋ฅผ ํ•„์š”๋กœ ํ–ˆ๊ณ , ์„ค๋ น ๊ทธ๋Ÿฐ ๊ฒŒ ์ด๋ฏธ ์ˆ™๋‹ฌ๋œ ์ƒํƒœ์˜€๋‹ค. ํ•˜๋”๋ผ๋„ ์†์ด ๋งŽ์ด ๊ฐ€๋Š” ์ž‘์—… (์ž์ฃผ ์˜ค๊ณ  ๊ฐ€๋Š” ์ฝ˜์†” ๋ฉ”๋‰ด)๋“ค ๋•Œ๋ฌธ์— ์˜คํžˆ๋ ค ๋น„ํšจ์œจ์ ์ธ ๊ตฌ์ถ• ๋ฐฐ๊ฒฝ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์…จ๋˜ ๋ถ„๋“ค๋„ ๊ณ„์…จ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

 

Automation

๊ทธ๋Ÿฌ๋‚˜ ๊ตณ์ด ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•˜์ง€ ์•Š๊ณ ๋„ YAML ์ฝ”๋“œ๋‚˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • AWS SAM CLI
  • Terraform (ํ…Œ๋ผํผ)
  • Pulumi (ํ’€๋ฃจ๋ฏธ)

 

AWS SAM์€ Serverless Application Model์˜ ์•ฝ์ž๋กœ AWS์˜ ์„œ๋ฒ„๋ฆฌ์Šค ํ”Œ๋žซํผ์„ ๋ช…๋ น์–ด๋กœ ๊ตฌ์ถ•ํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.

Terraform์€ ์„œ๋ฒ„๋ฆฌ์Šค ์™ธ์— AWS, GCP, Azure ๋“ฑ์˜ ํด๋ผ์šฐ๋“œ ๋ฆฌ์†Œ์Šค๋ฅผ HCL ์–ธ์–ด๋กœ ์ž‘์„ฑํ•ด ๋ฐฐํฌํ•˜๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

Pulumi๋Š” Terraform์™€ ๊ทธ ๊ธฐ๋Šฅ์ด ์œ ์‚ฌํ•˜์ง€๋งŒ HCL์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด(Go, Python ๋“ฑ)๋กœ ์ž‘์„ฑํ•ด ๋ฐฐํฌํ•˜๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

 

์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ Pulumi๋ฅผ ์ด์šฉํ•ด Python ์–ธ์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ AWS ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

AWS IAM

๋จผ์ € ๋ฐฐํฌ ๋„๊ตฌ(Pulumi)๊ฐ€ AWS ๋ฆฌ์†Œ์Šค๋ฅผ ์ด์šฉํ•ด ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. AWS ์ฝ˜์†”์— ๋กœ๊ทธ์ธ ํ•œ ํ›„ IAM์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.

 

Pulumi๊ฐ€ ๋‚ด AWS ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒˆ๋กœ์šด ์œ ์ €๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Add users๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

 

์œ ์ € ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  access type์„ Access Key๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ€๋ฉด User Group์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋งŒ๋“ค์–ด๋‘” User Group์ด ์—†์œผ๋ฏ€๋กœ Create group์„ ํ†ตํ•ด ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ๊ทธ๋ฃน์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

์‚ฌ์šฉ์ž ๊ทธ๋ฃน์—๋Š” ํ•ด๋‹น ๊ทธ๋ฃน์ด ๊ฐ€์งˆ ๊ถŒํ•œ์„ ์ง€์ •ํ•˜๊ณ , ๊ทธ ๊ถŒํ•œ์„ ์†Œ์†๋œ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒ์† ๋ฐ›๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

์„œ๋ฒ„๋ฆฌ์Šค ๊ตฌ์ถ•์—๋Š” ์•„๋ž˜์˜ 3๊ฐ€์ง€ ๊ถŒํ•œ ์ •์ฑ…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

  • IAMFullAccess
  • AmazonAPIGatewayAdministrator
  • AWSLambdaFullAccess

 

์œ„์˜ ์ •์ฑ…์ด ํ•„์š”ํ•œ ์ด์œ ๋ฅผ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด ์ง€๋‚œ ๊ธ€์—์„œ ์šฐ๋ฆฌ๋Š” Lambda๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๋ฅผ ๋ฐฐํฌํ•˜์˜€๊ณ , API Gateway๋ฅผ ์‚ฌ์šฉํ•ด Lambda์™€ Proxy๋กœ ์—ฐ๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š”๋ฐ, API Gateway๊ฐ€ Lambda์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ถŒํ•œ์„ ๋ถ€์—ฌํ–ˆ์—ˆ์ฃ . ๋”ฐ๋ผ์„œ IAMFullAccess ์ •์ฑ…๊นŒ์ง€ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๊ฒŒ 3๊ฐ€์ง€ ์ •์ฑ…์„ ์ฃผ๊ณ  ์‚ฌ์šฉ์ž ๊ทธ๋ฃน์„ ๋งŒ๋“ค๋ฉด ์œ„์™€ ๊ฐ™์ด ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž ๊ทธ๋ฃน์ด ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ทธ๋ฃน์˜ ์ฒดํฌ ๋ฐ•์Šค๋ฅผ ํด๋ฆญํ•œ ํ›„ ๋‹ค์Œ ๊ณผ์ •์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.

 

3๋‹จ๊ณ„ ๊ณผ์ •์€ ์„ ํƒ ์‚ฌํ•ญ์ด๋ฏ€๋กœ ์ƒ๋žตํ•˜๊ณ  4๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. access type์ด access key์ด๊ณ , User Group์ด ์ž˜ ์„ค์ •๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ ํ›„ ์‚ฌ์šฉ์ž ์ƒ์„ฑ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

์œ ์ € ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด Access Key ID์™€ Secret Access Key๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ์ƒ์„ฑ๋œ Secret access key๋Š” ์ฝ˜์†”์—์„œ ๋‘ ๋ฒˆ ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ .csv ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ๋ณ„๋„๋กœ ์ €์žฅํ•ด๋†“์œผ์‹œ๋Š” ๊ฑธ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

 

 

 

Set environment Access Key

Pulumi์—์„œ ๋ฐฉ๊ธˆ ๋ถ€์—ฌํ•œ AWS ๊ณ„์ •์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— Access Key์™€ Secret Access Key ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

$ export AWS_ACCESS_KEY_ID=${ACCESS_KEY_ID}โ€‹
$ export AWS_SECRET_ACCESS_KEY=${SECRET_ACCESS_KEY}

OS X, Linux์—์„œ ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  export ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

> $Env:AWS_ACCESS_KEY_ID = ${ACCESS_KEY_ID}
> $Env:AWS_SECRET_ACCESS_KEY = ${SECRET_ACCESS_KEY}

Windows์—์„œ๋Š” PowerShell ํ˜น์€ Windows Terminal์„ ์—ด๊ณ  $Env ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

Install Pulumi

์ด์ œ AWS์— ๋Œ€ํ•œ ์…‹ํŒ…์€ ๋ชจ๋‘ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ž์‹ ์˜ ์ปดํ“จํ„ฐ์— pulumi CLI๋ฅผ ์„ค์น˜ํ•˜๊ณ , ์ƒˆ๋กœ์šด Pulumi ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

https://www.pulumi.com/docs/get-started/install/

 

Download and Install

This page contains detailed instructions for installing Pulumi.

www.pulumi.com

 

$ curl -sSL https://get.pulumi.com | sh

Linux์—์„œ๋Š” curl์„ ํ†ตํ•ด ์ตœ์‹  ๋ฒ„์ „์˜ pulumi๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

$ brew install pulumi/tap/pulumi

OS X์—์„œ๋Š” Homebrew๋ฅผ ์ด์šฉํ•˜์—ฌ pulumi๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

> choco install pulumi

Windows์—์„œ๋Š” Chocolatey ํ˜น์€ WinGet ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž์—์„œ pulumi๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

 

 

Create Project

Pulumi๋Š” ํ”„๋กœ์ ํŠธ ๋‹จ์œ„๋กœ ํด๋ผ์šฐ๋“œ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ๊ธ€์—์„œ ์šฐ๋ฆฌ๋Š” FastAPI ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๊ณ , ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— infra ๋””๋ ‰ํ„ฐ๋ฆฌ์— pulumi ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

api ๋ฐ‘์œผ๋กœ infra ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ AWS๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ AWS ํด๋”๋ฅผ ํ•œ ๊ฐœ ๋” ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ „ ๊ธ€์—์„œ ๋งŒ๋“ค์–ด๋‘์—ˆ๋˜ ํ”„๋กœ์ ํŠธ ๋ชจ๋“ˆ ์••์ถ• ํŒŒ์ผ์„ fastapi-serverless-layer.zip ์ด๋ฆ„์œผ๋กœ ์ˆ˜์ •ํ•œ ํ›„ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค.

 

$ pulumi new aws-python

infra/aws ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฒฝ๋กœ์—์„œ ์œ„ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ƒˆ๋กœ์šด Pulumi ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด __main__.py, Pulumi.yaml, Pulumi.${stack}.yaml ์ด๋ ‡๊ฒŒ 3๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  venv ํด๋” ๋“ฑ๋„ ์ƒ์„ฑ๋˜๊ฒ ์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณผ ๊ฒƒ์€ ์ด 3๊ฐ€์ง€ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

 

 

 

Pulumi stack ํŒŒ์ผ์—์„œ๋Š” ํ•ด๋‹น ์Šคํƒ์— ๋Œ€ํ•œ ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” AWS๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ณ  ์„œ์šธ ๋ฆฌ์ „(ap-northeast-2)์— ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

์ฝ”๋“œ ์ž‘์„ฑ

aws ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์—, ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค ๊ฒƒ์„ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ์„œ๋ฒ„๋ฆฌ์Šค ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ–ˆ๋Š”์ง€ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € Lambda ํ”„๋กœ์ ํŠธ๋Š” AWS์˜ Cloudwatch๋กœ ์ด๋ฒคํŠธ ๋กœ๊ทธ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

aws ๋””๋ ‰ํ„ฐ๋ฆฌ์— iam.py ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์œ„์ฒ˜๋Ÿผ LambdaRole๊ณผ Role ์ •์ฑ…์— Cloudwatch๋กœ ๋กœ๊ทธ ๊ทธ๋ฃน์„ ๋งŒ๋“ค๊ณ  ์ด๋ฒคํŠธ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ถŒํ•œ์„ ์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

์œ„์—์„œ ๋งŒ๋“ค์–ด๋’€๋˜ iam.py๋ฅผ importํ•˜์—ฌ Lambda_ ์ธ์Šคํ„ด์Šค์— ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ Pulumi์—์„œ ํ•ด๋‹น ๊ถŒํ•œ์„ ์œ„์ž„๋ฐ›๊ณ  Lambda ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ Cloudwatch์— ํ•ด๋‹น ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋กœ๊ทธ ๊ทธ๋ฃน์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

Deploy

์ด์ œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์œ„์—์„œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ AWS์— ๋ฐฐํฌ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

$ pulumi preview

Pulumi๋Š” ๋ฐฐํฌํ•˜๊ธฐ ์ „ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•ด์ฃผ๋Š” ์œ ํšจ์„ฑ ์ฒดํฌ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. preview ๋ช…๋ น์„ ํ†ตํ•ด ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๊ฐ€ ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์ฝ”๋“œ์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€๋ฅผ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

 

PASSPHRASE๋Š” ์ฒ˜์Œ Pulumi ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ์„ ๋•Œ ์„ค์ •ํ•œ PASSPHRASE ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

์œ„์™€ ๊ฐ™์ด lambda Role๋กœ ์‹œ์ž‘ํ•˜์—ฌ Lambda Layer๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํ•จ์ˆ˜๋ฅผ ๋„ฃ๊ณ , API Gateway๋ฅผ ๋งŒ๋“œ๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ง„ํ–‰ํ•˜๋ฉด ์ž˜ ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

$ pulumi up

๋ชจ๋“  ๊ฒ€์ฆ์ด ๋๋‚ฌ์œผ๋ฉด up ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ž์‹ ์˜ AWS์— ๋ฐฐํฌํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๋•Œ, ์œ„ ๊ณผ์ •์ด ๋งž๋Š”์ง€๋ฅผ ๋‹ค์‹œ ๋ฌผ์–ด๋ด…๋‹ˆ๋‹ค. ๋งž์œผ๋ฉด yes ์ž˜๋ชป๋œ ๊ฒƒ์ด ์žˆ๋‹ค๋ฉด no๋ฅผ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด API Gateway ์—”๋“œํฌ์ธํŠธ ์ฃผ์†Œ๊ฐ€ ์ƒ์„ฑ์ด ๋˜์—ˆ์œผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ์„œ๋ฒ„๋ฆฌ์Šค ์Šคํƒ์ด ์™„์„ฑ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

API๊ฐ€ ์ž˜ ํ˜ธ์ถœ๋œ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

 

 

 

๋งˆ์น˜๋ฉฐ...

์—ฌ๊ธฐ๊นŒ์ง€ Pulumi๋ฅผ ์‚ฌ์šฉํ•ด์„œ AWS ํ™˜๊ฒฝ์„ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ์ง„ํ–‰ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. 

๊ฐœ์ธ์ ์œผ๋กœ FastAPI๋ฅผ Serverless๋กœ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” ๊ธฐ์กด์˜ FaaS ํ™˜๊ฒฝ์˜ ์œ ์ง€๋ณด์ˆ˜์— ๋Œ€ํ•œ ๋ถˆํŽธํ•จ์„ ๋งŽ์ด ๋œ์–ด์คฌ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

 

์ด์ „์˜ FaaS๋Š” ํ•จ์ˆ˜๋ณ„๋กœ API๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ API Gateway EndPoint์— ์ผ์ผ์ด ๋Œ€์ž…ํ•˜๋Š” ํ˜•ํƒœ์˜€๋‹ค๋ฉด ์ง€๊ธˆ์€ API Gateway์—์„œ Lambda ์ฝ”๋“œ ํ•˜๋‚˜๋ฅผ Proxy๋กœ ๊ฑธ์–ด๋‘๊ณ , ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ•ด๋‹น ์ฝ”๋“œ์—๊ฒŒ ๋งก๊ธฐ๊ฒ ๋‹ค๋Š” ๊ฒƒ์ด ๋ฉ๋‹ˆ๋‹ค. 

 

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ์ถ”๊ฐ€ API๋ฅผ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค Lambda ํ•จ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ํ•˜์ง€ ์•Š์•„๋„ ํ•˜๋‚˜์˜ Lambda ํ”„๋กœ์ ํŠธ์—์„œ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๊ณ , ๋” ๋‚˜์•„๊ฐ€์„œ๋Š” CI/CD์— ์ ์šฉํ•  ๋•Œ Lambda ์ฝ”๋“œ ํŒŒ์ผ์„ ์žฌ๋ฐฐํฌํ•˜๋Š” ํ˜•ํƒœ๋กœ๋งŒ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณผ์ • ์ž์ฒด๊ฐ€ ๊ฐ„์†Œํ™” ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ถ”๊ฐ€๋กœ ํ˜„์žฌ Lambda๋Š” ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ํŒŒ์ผ๋กœ ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ ๋ฟ ์•„๋‹ˆ๋ผ Docker Image๋กœ๋„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ตณ์ด Docker Image๊นŒ์ง€ ์จ์•ผ ํ•˜๋‚˜๋ผ๋Š” ์ž…์žฅ์ด์ง€๋งŒ ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ์•„์ง ์–ด์ƒ‰ํ•˜๋‹ค๋ฉด Docker Image๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

 

 

 
๋ฐ˜์‘ํ˜•
TAGS.

Tistory Comments