๐Ÿš€ AWS Amplify๋กœ React ์•ฑ์„ ํ’€์Šคํƒ ์„œ๋ฒ„๋ฆฌ์Šค ๋ฐฐํฌ ๋ฐ ์ž๋™ํ™” แตƒโคปแถป

jwo0o0ยท2022๋…„ 10์›” 23์ผ
1

AWS

๋ชฉ๋ก ๋ณด๊ธฐ
1/4
post-thumbnail
post-custom-banner

AWS Amplify๋กœ โš›๏ธReact ์•ฑ์„ ๐Ÿš€๋ฐฐํฌํ•˜๊ณ  ์ž๋™ํ™”๊นŒ์ง€ ์ ์šฉํ•ด๋ณด์ž

์ฐธ๊ณ  ๋ฌธ์„œ

1. AWS ๊ณ„์ • ์ƒ์„ฑ

2. AWS ๊ณ„์ • ๋ณด์•ˆ ์„ค์ •, ์‚ฌ์šฉ์ž ๊ทธ๋ฃน ์ƒ์„ฑ

3. IAM ์‚ฌ์šฉ์ž ์ƒ์„ฑ

  • ์ด ๋•Œ secret key ๋“ฑ ์ •๋ณด ์ €์žฅ, ๋ณ„์นญ ์ƒ์„ฑ

4. aws-cli ์„ค์น˜

npm install -g @aws-amplify/cli sudo๋ฅผ ํ†ตํ•ด ์‹คํ–‰ํ•ด์„œ ์ „์—ญ์œผ๋กœ ์„ค์น˜ํ•˜์ž!

  • ๋งฅ๋ถ ๊ธฐ์ค€ For all users ๊ถŒํ•œ์œผ๋กœ ์„ค์น˜ํ•  ๊ฒฝ์šฐ /usr/local/aws-cli์— ์„ค์น˜๋จ
  • aws configure์„ ํ†ตํ•ด region์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๐Ÿ‡ฐ๐Ÿ‡ท ํ•œ๊ตญ์˜ ๊ฒฝ์šฐ northeast-2
  • aws ec2 describe-vpcs ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •๋˜์—ˆ๋‹ค๋ฉด config ํŒŒ์ผ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ~/.aws ํด๋”์— ๊ฐ€๋ณด๋ฉด config ํŒŒ์ผ๊ณผ credentials ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๋‹ค์Œ ๋‹จ๊ณ„๋Š” AWS Cloud9 ์„ค์ • ๋‹จ๊ณ„์ธ๋ฐ ์ผ๋‹จ ์ƒ๋žตํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค.

Amazon Amplify์— ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ ํ•˜๊ธฐ

1. Amplfiy ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

1-1. Amplify ์„ค์น˜

npm install aws-amplify

1-2. Amplify ๊ตฌ์„ฑ

amplify configure ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด Amplfiy๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.

  • ์šฐ์„  ์ฝ˜์†”์—์„œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ƒˆ๋กœ์šด IAM ๊ณ„์ •์„ ์ƒ์„ฑํ•œ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ์ฝ˜์†”์—์„œ ์ƒ์„ฑํ•œ IAM ๊ณ„์ •์— ๋กœ๊ทธ์ธ๊นŒ์ง€ ํ•ด์ฃผ์ž.

1-3. Amplify ์ดˆ๊ธฐํ™”

create-react-app์œผ๋กœ ์ƒ์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— amplify init ๋ช…๋ น์–ด๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

์ดˆ๊ธฐํ™” ์™„๋ฃŒ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ๋“ค์ด ์ผ์–ด๋‚œ๋‹ค.

โœ๏ธ ํ”„๋กœ๋น„์ €๋‹(provisioning)
์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์— ๋งž๊ฒŒ ์‹œ์Šคํ…œ ์ž์›์„ ํ• ๋‹น, ๋ฐฐ์น˜, ๋ฐฐํฌํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ ํ•„์š” ์‹œ์Šคํ…œ์„ ์ฆ‰์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋กœ ๋ฏธ๋ฆฌ ์ค€๋น„ํ•ด๋‘๋Š” ๊ฒƒ

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ•„์š”ํ•œ ํด๋ผ์šฐ๋“œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  AWS ํ”„๋กœํ•„์„ ํฌํ•จํ•œ ํ”„๋กœ์ ํŠธ ์„ค์ •์„ ํ™•์ธํ•œ๋‹ค.
  • /amplify ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
  • src/aws-export.js๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. amplify์—์„œ ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š” ์„œ๋น„์Šค์— ๋Œ€ํ•œ ๊ตฌ์„ฑ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.
  • amplify ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” AWS amplify ์ฝ˜์†” ๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ํด๋ผ์šฐ๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    AWS amplify ์ฝ˜์†”์—์„œ ์•„๊นŒ ์„ค์ •ํ–ˆ๋˜ amplifyapp์ด๋ผ๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

1-4. Amplify ์„ค์น˜

npm install aws-amplify

2. API ์ถ”๊ฐ€

2-1. Amplify React ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

npm install @aws-amplify/ui-react

2-2. src/App.js ํŒŒ์ผ ์ˆ˜์ •

โ—๏ธ ๊ณต์‹ ์•ˆ๋‚ด์„œ์—๋Š”

import { withAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react';
import Amplify from 'aws-amplify';
import awsExports from './aws-exports';

Amplify.configure(awsExports);

function App() {
  return (
    <div>
      <h1>Notes App</h1>
      <AmplifySignOut />
    </div>
  );
}

export default withAuthenticator(App);

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ amplify ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ๋‚˜๋Š” 'AmplifySignOut' is not exported from '@aws-amplify/ui-react' ์—๋Ÿฌ๊ฐ€ ๋– ์„œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์—์„œ ๊ฑธ์–ด์ค€ ๋งํฌ ์—ฌ๊ธฐ๋ฅผ ๋ณด๊ณ  ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

import { Amplify } from 'aws-amplify';
import { withAuthenticator } from '@aws-amplify/ui-react';
import '@aws-amplify/ui-react/styles.css';
import { Header } from './Components/Header';
import { Main } from './Components/Main';

import awsconfig from './aws-exports';
Amplify.configure(awsconfig);

function App() {
  return (
      <div className="App">
        <Header />
        <Main />
      </div>
  );
}

export default withAuthenticator(App)

โ†‘ ๋‚ด๊ฐ€ ํ•œ ๋ฐฉ์‹

2-3. authentication ์ถ”๊ฐ€

AWS ๊ณต์‹ ๊ฐ€์ด๋“œ์—๋Š” ์—†๋Š” ๋‚ด์šฉ์ธ๋ฐ, ์ด ์ž‘์—…์„ ์•ˆํ•ด์ฃผ์—ˆ๋”๋‹ˆ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์—์„œ ์•ˆ๋‚ดํ•ด์ค€ Amplify Dev Center ๊ฐ€์ด๋“œ๋ฅผ ๋ณด๊ณ  ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

amplify add auth

2-4. amplify ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ

amplify push

โ—๏ธ amplify๋กœ ๋ฐฐํฌ ํ›„ ์ฒ˜์Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ๋Š” ๊ณ„์ •์„ ์ƒ์„ฑํ•˜๊ณ  ๋กœ๊ทธ์ธ ๊ณผ์ •์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด localhost์—์„œ๋„ ์•ฑ์ด ๋ฌธ์ œ์—†์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๐ŸŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ถ”๊ฐ€

AWS amplify๋Š” AWS AppSync ๋ฐ Amazon DynamoDB๋ฅผ ์‚ฌ์šฉํ•ด GraphQL API๋ฅผ ๊ตฌ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

AppSync๋Š” GraphQL ์„œ๋น„์Šค๋กœ API๋ฅผ ๊ด€๋ฆฌ, DynamoDB๋Š” NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋‹ค. ๋”ฐ๋ผ์„œ GraphQL ์„ค์ •๊นŒ์ง€ ์™„๋ฃŒํ•ด์ฃผ๋ฉด ํ”„๋ก ํŠธ์—”๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„œ๋ฒ„๋ฆฌ์Šค๋กœ ๋ฐฐํฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  Amplify Dev Center ๊ฐ€์ด๋“œ๋ฅผ ๋ณด๋ฉด REST ๋ฐฉ์‹์œผ๋กœ api์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ตฌ๊ธ€๋ง ํ•ด๋ณด๋ฉด ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•˜๋‹ค.

๋‚˜๋Š” ๊ฐ€์ด๋“œ์— ๋‚˜์™€์žˆ๋Š” ์ด ๊ณผ์ •์„ ์ƒ๋žตํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ๋Š” json-server๋กœ ๋งŒ๋“  ์„œ๋ฒ„์— axios๋กœ ์š”์ฒญํ•˜๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์•„์ง ์„œ๋ฒ„๋ฆฌ์Šคํ•œ ์ƒํƒœ๋Š” ์•„๋‹ˆ๋‹ค.

3. ๋ฐฐํฌ ์ž๋™ํ™”

์ด์ œ Github ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— amplify์— ์—ฐ๊ฒฐํ•˜๊ณ  ์ง€์†์ ์ธ ๋ฐฐํฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์„ค์ •์„ ํ•ด๋ณด์ž.

3-1. amplify app์„ github ๊ณ„์ •์— ์—ฐ๊ฒฐ

amplify add hosting


๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ต์…˜์„ ์„ค์ •ํ•œ๋‹ค.
(Hosting with Amplify Console / Continuous deployment)


๊ทธ๋ฆฌ๊ณ  github ๊ณ„์ •๊ณผ ์—ฐ๊ฒฐํ•œ๋‹ค.

  • ๊ณต์‹๊ฐ€์ด๋“œ๋ฅผ ๋”ฐ๋ผ amplifyconsole-backend-role์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ์—ญํ• ์„ ์ƒ์„ฑํ•˜์ž.

  • ํ™˜๊ฒฝ์€ amplify init์„ ํ•  ๋•Œ ์ƒ์„ฑํ–ˆ๋˜ dev๋ฅผ ์„ ํƒํ•œ๋‹ค.

3-2. ๋ฐฐํฌ ๊ณผ์ • ํ™•์ธํ•˜๊ธฐ

github ๋ ˆํฌ์ง€ํ† ๋ฆฌ์™€ branch๊นŒ์ง€ ์—ฐ๊ฒฐํ–ˆ๋‹ค๋ฉด ์ด์ œ ํ•ด๋‹น ๋ธŒ๋žœ์น˜์— ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์ด ํ‘ธ์‰ฌ๋  ๋•Œ๋งˆ๋‹ค amplify๊ฐ€ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ ์šฉํ•ด ์ž๋™์œผ๋กœ ๋นŒ๋“œํ•˜๊ณ  ์ƒˆ๋กœ ๋ฐฐํฌํ•œ๋‹ค. (๋Œ€๋ฐ• ์งฑ์ธ๋“ฏ ๐Ÿ‘)

๋นŒ๋“œ ๊ธฐ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์‹œ๊ฐ„์ด ์–ผ๋งˆ๋‚˜ ์†Œ์š”๋˜์—ˆ๋Š”์ง€๋„ ๋ณผ ์ˆ˜ ์žˆ๊ณ , ํ˜„์žฌ ๋ฒ„์ „์— ๋ฌธ์ œ๊ฐ€ ์žˆ์œผ๋ฉด ์ด์ „ ๋ฒ„์ „์„ ์„ ํƒํ•ด์„œ ๋ฐฐํฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋นŒ๋“œ ๊ณผ์ •์„ ์ž์„ธํžˆ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋นŒ๋“œ๊ฐ€ ์‹คํŒจํ–ˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๋ฅผ ์ž์„ธํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ›  ๋‚˜๋Š” ํ”„๋ŸฐํŠธ์—”๋“œ์—์„œ ๋นŒ๋“œ์—์„œ ๊ณ„์† ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ์‚ฝ์งˆํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ aws amplify ํ™˜๊ฒฝ์„ ์ง€์šฐ๊ณ  ๋‹ค์‹œ ์„ค์น˜ํ–ˆ๋‹ค.

3-3. ๋ฐฐํฌ ๋ธŒ๋žœ์น˜ ์„ค์ •

๋‚˜๋Š” aws-deploy๋ผ๋Š” ๋ฐฐํฌ์šฉ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด AWS Amplify์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๊ณ  main ๋ธŒ๋žœ์น˜๋Š” CI/CD ๋น„ํ™œ์„ฑํ™”๋ฅผ ํ•ด๋†“์•˜๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€ ์šฐ์—ฌ๊ณก์ ˆ ๋์— Amplify๋กœ ๋ฐฐํฌ ์–ด๋Š์ •๋„๋Š” ์™„๋ฃŒ!

profile
๊ฐœ๋ฐœ๋กœ ๋ฐฅ๋ฒŒ์ด ํ•˜๊ณ  ์‹ถ์€ ์‚ฌ๋žŒ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€