AWS Serverless Meetup - AppSync

AppSync๋ฅผ ํ™œ์šฉํ•œ ๋ฆฌ์–ผํƒ€์ž„ ์„œ๋ฒ„๋ฆฌ์Šค ์•„ํ‚คํ…์ณ

Speaker : ๊น€ํƒœ์šฐ https://dev.classmethod.jp/author/kim-taewoo/

์„œ๋ฒ„๋ฆฌ์Šค ์•„ํ‚คํ…์ณ ํŠน์ง•

  • ๊ด€๋ฆฌํ•  ์„œ๋ฒ„๊ฐ€ ์—†๋‹ค (NoOps)

  • ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ํ•„์š”ํ•œ ๋งŒํผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

  • ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๋Š” Lambda or Fargate

    • Lambda์˜ ๊ฒฝ์šฐ ์ž์ฒด์ ์œผ๋กœ connection์„ ์œ ์ง€ํ•  ์ˆ˜ ์—†๋‹ค!

Fargate

: a serverless compute engine for containers that works with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS)

Lambda

: a compute service that lets you run code without provisioning or managing servers

์„œ๋ฒ„๋ฆฌ์Šค ์•„ํ‚คํ…์ณ๋„ ๋ฆฌ์–ผํƒ€์ž„ ๊ธฐ๋Šฅ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค

How?

: by using API Gateway websocket or Pubsub

  • API Gateway ์—์„œ websocket์„ ์ง€์›ํ•จ

  • Connection ๊ธฐ๋ฐ˜์˜ solution์€ ์•„๋‹ˆ์ง€๋งŒ PubSub Solution๋„ ๊ฐ€๋Šฅํ•˜๊ธฐ๋Š” ํ•จ

It's possible, but

  • Front-end ์™€ Back-end ๊ฐ„์˜ ํ†ต์‹ ์„ ์œ„ํ•œ Data structure ์ •์˜ ํ•ด์•ผํ•จ

  • Data ์ฃผ๊ณ  ๋ฐ›์„ ๋•Œ์˜ Validation ํ•„์š”

  • Front & Back ์–‘์ธก์—์„œ ๊ฐœ๋ฐœ์ด ์ด๋ฃจ์–ด์ ธ์•ผ ํ•จ

  • Back-end ์‹ ๊ฒฝ์จ์•ผ ํ•  ๊ฒƒ์ด ๋งŽ์Œ

    • High availality

    • reliability

    • scalability

---> AppSync ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทน๋ณต ๊ฐ€๋Šฅํ•˜๋‹ค

AWS AppSync

  • Strong typing์œผ๋กœ ์ธํ•ด Validation ์ฝ”๋“œ ๊ฑฐ์˜ ์ƒ๋žต ๊ฐ€๋Šฅ ( GraphQL์˜ ํŠน์ง• )

  • Real-time ํ†ต์‹ ์„ ์œ„ํ•ด์„œ Back-end์—์„œ ํ•ด์•ผํ•  ๊ฒƒ์€ Subscription ์„ ์Šคํ‚ค๋งˆ์— ์„ ์–ธํ•ด๋‘๋Š” ๊ฒƒ ๋ฐ–์— ์—†์Œ

    • Schema์— ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์“ธ๊ฑฐ๋ผ๊ณ  ๋ช…์‹œ๋งŒ ํ•ด๋†“์œผ๋ฉด ๋œ๋‹ค!

  • Frontend ์ธก์—์„œ๋Š” AppSync SDK๋‚˜ Amplify๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํžˆ Subscription์ด ๊ฐ€๋Šฅํ•˜๋‹ค!

Pros of AppSync

  1. GraphQL์—์„œ ๋น ๋ฅธ prototype ์ƒ์„ฑ ๋ฐ ๊ฐœ๋ฐœ

  2. ์‹ค์‹œ๊ฐ„ ํ˜‘์—… mobile & web app

  3. ์›ํ™œํ•œ ์˜คํ”„๋ผ์ธ ์ž‘์—…

  4. ์•ˆ์ „ํ•œ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค

  5. ์—ฌ๋Ÿฌ ์›๋ณธ์˜ ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉ

  6. ๋ฐ์ดํ„ฐ ์ถฉ๋Œ ๊ฐ์ง€ ๋ฐ ์ถฉ๋Œ ํ•ด๊ฒฐ

Real time Data ํ†ต์‹ ์„ ์œ„ํ•œ AppSync Architecture

image-20200307010708273

Serverless Streaming Architecture

image-20200307011320581

  • ์ „ํ˜•์ ์ธ ์„œ๋ฒ„๋ฆฌ์Šค ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆฌ๋ฐ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜

  • Eventual Consistency or Event-driven processing ๋“ฑ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ์— ์ ํ•ฉ

  • Scalable & Reliable & Cost Effective

  • ๋ณ‘๋ ฌ๋„๊ฐ€ ๋งค์šฐ ๋†’์€ ์ž‘์—…์— ๋Œ€ํ•ด์„œ๋Š” ์‹ค์‹œ๊ฐ„์„ฑ์„ ๋ณด์žฅํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ Kinesis DS๊ฐ€ ์ข‹์Œ

  • DynamoDB Streams๋Š” ์ตœ๋Œ€ 24์‹œ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๋ณด์กด, ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์˜ ์ˆœ๋ฒˆ์ด ๋ณด์ฆ ๊ฐ€๋Šฅํ•œ ์„œ๋น„์Šค

image-20200307012139759

  • RDS๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ์—๋„ ์ŠคํŠธ๋ฆฌ๋ฐ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•จ

  • Kinesis Data Streams ๋Š” ์ƒค๋“œ๋ฅผ ์ ์ ˆํžˆ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•จ

    • ์ƒค๋“œ

      : ์ŠคํŠธ๋ฆผ์—์„œ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„๋˜๋Š” ๋ฐ์ดํ„ฐ ๋ ˆ์ฝ”๋“œ ์‹œํ€€์Šค

  • Kinesis Data Streams ์—์„œ ๊ตณ์ด Lamda ๋กœ ์—ฐ๊ณ„ํ•  ํ•„์š”๋Š” ์—†์Œ

    • but, AppSync ์—ฐ๊ณ„๋ฅผ ์œ„ํ•œ ์šฉ๋„๋กœ๋Š” Lambda ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค!

  • DB(MySQL, PostgreSQL etc.) ์œผ๋กœ ๋ถ€ํ„ฐ ์ง์ ‘ Lambda ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•จ

image-20200307013708527
  1. SQL ์—์„œ Lambda ๋กœ ์—ฐ๊ณ„๋˜๋Š” ํŒจํ„ด

  2. Kinesis ์—์„œ Lambda ๋กœ ์—ฐ๊ณ„๋˜๋Š” ํŒจํ„ด

  3. API Gateway ์—์„œ Lambda ๋กœ ์—ฐ๊ณ„๋˜๋Š” ํŒจํ„ด

  4. CloudWatch Alarms ์—์„œ Lambda ๋กœ ์—ฐ๊ณ„๋˜๋Š” ํŒจํ„ด

  5. CloudWatch Events ์—์„œ Lambda ๋กœ ์—ฐ๊ณ„๋˜๋Š” ํŒจํ„ด

    ... and a lot more!

Lambda๋กœ ๋ถˆ๋Ÿฌ์˜จ ํ›„

image-20200307014333774

Demo Project Architecture

image-20200307014508453

AppSync๋ฅผ Subscription์˜ ์šฉ๋„๋กœ๋งŒ ์“ธ๊ฑฐ๋ผ๋ฉด ๊ตณ์ด Appsync์šฉ DB ์—ฐ๊ฒฐํ•  ํ•„์š” ์—†์ด DynamicDB Stream๋ฅผ ์“ฐ๋ฉด ๋จ!

Microservice

  • API Gateway -> Lambda -> DynamoDB ๊ตฌ์„ฑ

  • ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” Serverless Architecture API back-end pattern

  • ๊ฐ„๋‹จํ•œ input form์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด ๋ฐ๋ชจ์ฝ”๋“œ ์ž‘์„ฑ

AWS AppSync

  • AppSync ๊ตฌ์ถ• ์‹œ์—๋Š” Serverless Framework๋ฅผ ์ถ”์ฒœ

  • Subscription ์„ Schema์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ client ์ธก์—์„œ Subscription์ด ๊ฐ€

    • ๋ฐฑ์•ค๋“œ ์ž‘์—… ๋ฐœ์ƒ x

  • ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์€ mutation, subscription ๋ฟ์ด์ง€๋งŒ, query๋ฅผ schema์— ๋“ฑ๋กํ•˜์ง€ ์•Š์œผ๋ฉด GraphQL schema error๊ฐ€ ๋˜๋ฏ€๋กœ ๊ฐ„๋‹จํ•œ query ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•จ

    • getOrder ์ž‘์„ฑ

  • Mutation ํ•˜๊ธฐ ์œ„ํ•ด์„œ์˜ DynamoDB ๋ฅผ Data source๋กœ์„œ ์ƒ์„ฑํ•˜๊ณ , TTL (Time-To-Live) ๋ฅผ ์„ค์ •ํ•œ๋‹ค

    • expiresAt์„ AWSTimestamp ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•œ๋‹ค

DynamoDB Streams -> Lambda

  • DynamoDB Streams ๋Š” ์ตœ๋Œ€ 24 ์‹œ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๋ณด์กด, ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์˜ ์ˆœ๋ฒˆ์ด ๋ณด์ฆ๊ฐ€๋Šฅํ•œ ์„œ๋น„์Šค

    • but, Realtime Service ์—์„œ๋Š” ์ตœ๋Œ€ 24 ์‹œ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ๋ณด์กด์€ ๋ณ„ ์˜๋ฏธ๊ฐ€ ์—†๊ธด ํ•จ

  • ๋ณ‘๋ ฌ๋„๋ฅผ ๋†’์ด๋ ค๋ฉด ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ ˆํ•˜๋ฉด ๋จ!

Lambda -> Appsync

  • ์‹ค์„œ๋น„์Šค์šฉ์œผ๋กœ releaase ๋œ AppSync Client SDK ๋Š” JavaScript version ๋ฟ์ด๋ฏ€๋กœ Amplify๋ฅผ ๊ถŒ์žฅ

  • AppSync Clinet SDK๋ฅผ Lambda ์ƒ์—์„œ ๋™์žฅ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ฃผ์˜ํ•  ์ 

    • ismorphic-fetch๋ฅผ import ํ•ด์•ผ ํ•จ

    • disableOffline ์„ true๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•จ

AppSync๋กœ ์ด๋Ÿฐ๊ฒƒ๋„ ๋˜์ง€ ์•Š์„๊นŒ?

State Management by AppSync

  • Serverless Architecture pattern ์—์„œ๋Š” Rich Client๋ฅผ ์ง€ํ–ฅ

    • but, Front-end code์˜ complexity๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€์•Š์Œ

  • Front-end ์˜ code complexity ์˜ ์ƒ๋‹น ๋ถ€๋ถ„์— ๊ธฐ์—ฌํ•˜๋Š” State Management

  • State Mangement ๋ฅผ ๋„์ž…ํ•˜๊ธฐ ์œ„ํ•œ code์˜ complexity๋ฅผ AppSync Subscription ์— ์˜์กดํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ์ง€ ์•Š์„๊นŒ?

    • but, Network์˜ ์†๋„/์•ˆ์ •์„ฑ ๋“ฑ์— ์˜์กด์„ฑ์ด ์ƒ๊น€

  • ๊ทธ๋Ÿฌ๋‚˜ 5G ์‹œ๋Œ€์—๋Š” ํฐ ๋ฌธ์ œ ์—†์ง€ ์•Š์„๊นŒ?

+

GraphQL์ด ๋А๋ฆฐ ์ด์œ 

  • http ๋ผ์„œ

  • resolver ๋•Œ๋ฌธ

    • ์ด ๋ถ€๋ถ„์€ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ

โ€‹

๋‹น๊ทผ๋งˆ์ผ“๋„ GraphQL ์“ด๋‹ค๊ณ  ํ•จ! ์†๋‹ฅ์†๋‹ฅ

-> admin ๋งŒ๋“ค ๋•Œ ์ข‹๋‹ค๊ณ  ํ•จ! ํ•œ๋ฒˆ์— ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์–ด์„œ!

-> GraphQL ์“ฐ๋ฉด node.js์—์„œ data๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด ๋น ๋ฅผ ์ˆ˜๊ฐ€ ์—†๋Š” ๋ฐฉ์‹์ž„!

๋‹น๊ทผ๋งˆ์ผ“์ด gRPC ์“ฐ๋Š” ์ด์œ  == ์†๋„๊ฐ€ ์—„์ฒญ ๋น ๋ฆ„

+

Looked up after the meetup

  1. gRPC

    image-20200307032408544

    • gRPC (gRPC Remote Procedure Calls) is an open source remote procedure call (RPC) system initially developed at Google in 2015

    • In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.

    • As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.

    • On the server side, the server implements this interface and runs a gRPC server to handle client calls.

    • On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.

  1. apollo gateway

    image-20200307032923930

    • Apollo is an implementation of GraphQL designed for the needs of product engineering teams building modern, data-driven applications.

    • It encourages an agile, incremental approach and takes special care to avoid requiring any changes to existing APIs and services.

    • Apollo puts particular emphasis on tooling and workflows.

    • Apollo is best used as a new layer in your stack that sits between your services and your applications.

    • It's a combination of open source components, commercial extensions, and cloud services.

  1. 3factor app

    • 3factor app is an architecture pattern for modern full-stack apps.

    • Today, it is possible to build apps that have high feature velocity and scalability from the get go.

    • An architecture pattern which is composed of 3 factors:

      1. Realtime GraphQL

      2. Reliable eventing

      3. Async serverless

Last updated

Was this helpful?