JAWS DAYS 2021のオンライン登壇で使用した発表資料です。 https://jawsdays2021.jaws-ug.jp/timetable/track-b-1600/
0Copyright© System Integrator Corp. All rights reserved.System IntegratorCognito+API Gateway+Lambda+S3ではじめるサーバーレスアプリ構築~SIer企業がはじめて挑戦してみた話~
View Slide
1Copyright© System Integrator Corp. All rights reserved.System Integrator横山 弘典(@yokoyantech)• 株式会社システムインテグレータ• 製品企画室• 宮崎→福岡→埼玉• AWS歴• 約7年• アソシエイト3冠• SAプロフェッショナル• JAWS歴• 2014年から毎年参加• 2020年初登壇(動画)
2Copyright© System Integrator Corp. All rights reserved.System Integrator毎年ボッチ参加なので今年こそはJAWS仲間を作りたい!
3Copyright© System Integrator Corp. All rights reserved.System Integrator1. 本日お伝えすること2. サーバーレスアーキテクチャを採用した理由3. サーバーレスでREST APIを作る4. サーバーレスでWEBアプリを作る5. 非同期処理を作る6. デプロイ/負荷試験Agenda
4Copyright© System Integrator Corp. All rights reserved.System Integrator新サービス開発で、はじめてサーバーレスを使った体験談をお伝えします
5Copyright© System Integrator Corp. All rights reserved.System Integrator株式会社システムインテグレータ• 埼玉県さいたま市にある会社• 東証1部• 創業27年目• 事業内容• 自社パッケージソフト(EC、ERPなど)• 企画、開発• カスタマイズ• 自社WEBサービス• 開発、運営• 技術スタック• AWS、Azure• Java、C#、Python、Rubyなど
6Copyright© System Integrator Corp. All rights reserved.System Integrator
7Copyright© System Integrator Corp. All rights reserved.System Integrator
8Copyright© System Integrator Corp. All rights reserved.System Integrator1. 本日お伝えすること2. サーバーレスアーキテクチャを採用した理由3. サーバーレスでREST APIを作る4. サーバーレスでWEBアプリを作る5. 非同期処理を作る6. デプロイ/負荷試験Agenda
9Copyright© System Integrator Corp. All rights reserved.System Integrator限られた人数でやることはいっぱいアプリ開発 インフラ開発 各種サポート
10Copyright© System Integrator Corp. All rights reserved.System Integrator家族や子供との時間も大切にしたい
11Copyright© System Integrator Corp. All rights reserved.System Integrator精神論で頑張らないAWSで仕組みを変える
12Copyright© System Integrator Corp. All rights reserved.System Integratorサーバーレスを使うと好きなことをやる時間が増える!
13Copyright© System Integrator Corp. All rights reserved.System Integrator受験者がコードを提出外部APIで採点(テストケース実行)成否を判定私達のプロダクト特徴
14Copyright© System Integrator Corp. All rights reserved.System IntegratorVPCAWS Cloud受験者Amazon EC2Public subnetElastic Load BalancingNAT gatewayAmazon RDS他社外部API(提出/採点)※EC2+αPrivate subnet
15Copyright© System Integrator Corp. All rights reserved.System Integrator
16Copyright© System Integrator Corp. All rights reserved.System Integrator
17Copyright© System Integrator Corp. All rights reserved.System Integrator大規模イベントでは、採点システムが数百台必要になるケースもあった
18Copyright© System Integrator Corp. All rights reserved.System Integrator新サービス(SQL)では、自社で採点システムを保持・運用
19Copyright© System Integrator Corp. All rights reserved.System IntegratorEC2などのよくある悩みコスト高い(待機時間含)運用が自力(HDD容量等)自動スケール設定が必要
20Copyright© System Integrator Corp. All rights reserved.System Integratorコスト安い(実行時間)運用が楽自動スケール設定が不要サーバーレスで解決
21Copyright© System Integrator Corp. All rights reserved.System Integratorスパイク対策、コスト減、運用を楽にするためにサーバーレスを採用
22Copyright© System Integrator Corp. All rights reserved.System Integrator1. 本日お伝えすること2. サーバーレスアーキテクチャを採用した理由3. サーバーレスでREST APIを作る4. サーバーレスでWEBアプリを作る5. 非同期処理を作る6. デプロイ/負荷試験Agenda
23Copyright© System Integrator Corp. All rights reserved.System IntegratorAPI Gateway Lambda NoSQLサーバーレスでREST API
24Copyright© System Integrator Corp. All rights reserved.System Integrator提出API提出コード書き込み提出結果保持サーバーレスでREST API
25Copyright© System Integrator Corp. All rights reserved.System IntegratorMongoDB Atlas• MongoDBとは• jsonを格納するドキュメント型のNoSQL• Atlasとは• MongoDB公式が提供するDaaS• フルマネージドサービス• AWS連携が簡単• VPCピアリングできる!• セキュリティグループによる制御できる!• 東京リージョンあり
26Copyright© System Integrator Corp. All rights reserved.System IntegratorVPCEC2からPRIVATEなAPIを呼び出したい(受験者→提出API)AWS CloudusersAmazon API Gateway(PRIVATE)AWS LambdaPeeringconnectionVPCAmazon EC2Public subnetElastic Load BalancingNAT gatewayPrivate subnet
27Copyright© System Integrator Corp. All rights reserved.System IntegratorVPCEC2からPRIVATEなAPIを呼び出したい(受験者→提出API)AWS CloudusersAmazon API Gateway(PRIVATE)AWS LambdaPeeringconnectionVPCAmazon EC2Public subnetElastic Load BalancingNAT gatewayPrivate subnetPrivate Subnet内のLambdaをAPI Gatewayがどうやって呼び出すの?
28Copyright© System Integrator Corp. All rights reserved.System Integrator【原因】LambdaをVPC内に配置した際に想像した図が間違っていた
29Copyright© System Integrator Corp. All rights reserved.System IntegratorAWS CloudAWS管理VPC 私達のVPCAWS LambdaElastic networkinterfaceLambdaをVPC内に配置すると、私達のVPCの中に、ENIができるPrivate subnet
30Copyright© System Integrator Corp. All rights reserved.System IntegratorAWS CloudAWS管理VPC 私達のVPCAWS LambdaElastic networkinterfacePrivate subnetLambdaの実体は、AWS管理VPC側に存在する
31Copyright© System Integrator Corp. All rights reserved.System IntegratorAWS CloudAWS管理VPC 私達のVPCAWS LambdaElastic networkinterfacePrivate subnetLambdaの実体は、AWS管理VPC側に存在する
32Copyright© System Integrator Corp. All rights reserved.System IntegratorAWS CloudAWS管理VPC 私達のVPCAWS LambdaElastic networkinterfaceLambdaは、 ENI経由で私達のVPC内のリソースにアクセスできるAmazon RDSPrivate subnet
33Copyright© System Integrator Corp. All rights reserved.System IntegratorPrivate subnetAPI GatewayからLambdaを呼ぶときは、AWS内部のネットワークを通るAWS CloudAWS管理VPC 私達のVPCAmazon API Gateway(PRIVATE)AWS LambdaElastic networkinterface
34Copyright© System Integrator Corp. All rights reserved.System Integrator【対策】VPC Endpoint とENIを使う
35Copyright© System Integrator Corp. All rights reserved.System IntegratorVPC【修正後】EC2からPRIVATEなAPIを呼び出したいAWS CloudusersAmazon API Gateway(PRIVATE)AWS LambdaPeeringconnectionVPCAmazon EC2Public subnetElastic Load BalancingNAT gatewayElastic networkinterfaceEndpointsPrivate subnet
36Copyright© System Integrator Corp. All rights reserved.System Integrator1. 本日お伝えすること2. サーバーレスアーキテクチャを採用した理由3. サーバーレスでREST APIを作る4. サーバーレスでWEBアプリを作る5. 非同期処理を作る6. デプロイ/負荷試験Agenda
37Copyright© System Integrator Corp. All rights reserved.System IntegratorS3 Cognito API GatewayサーバーレスでWEBアプリ
38Copyright© System Integrator Corp. All rights reserved.System IntegratorHTML/JS配置ログイン時認証問題登録API などサーバーレスでWEBアプリ
39Copyright© System Integrator Corp. All rights reserved.System IntegratorVPCS3で静的ホスティング+API呼び出しAWS CloudusersAmazon API Gateway(Edge)Amazon S3Amazon CognitoAWS LambdaPeeringconnectionVPCElastic networkinterfacePrivate subnet
40Copyright© System Integrator Corp. All rights reserved.System Integratorクリップボード貼り付け処理
41Copyright© System Integrator Corp. All rights reserved.System Integratorhttpで動いていた
42Copyright© System Integrator Corp. All rights reserved.System Integrator【原因】S3は、Web Site EndpointのHTTPSアクセスをサポートしていない
43Copyright© System Integrator Corp. All rights reserved.System Integrator【対策1】CloudFrontを使ってHTTPSを実現
44Copyright© System Integrator Corp. All rights reserved.System Integratorhttps://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/WebsiteHosting.html
45Copyright© System Integrator Corp. All rights reserved.System Integrator【対策2】S3 REST APIエンドポイントを使う
46Copyright© System Integrator Corp. All rights reserved.System Integratorhttp://バケット名.s3-website-ap-northeast-1.amazonaws.com/index.htmlウェブサイト エンドポイント
47Copyright© System Integrator Corp. All rights reserved.System Integratorhttps://バケット名.s3-ap-northeast-1.amazonaws.com/index.htmlREST API エンドポイント
48Copyright© System Integrator Corp. All rights reserved.System Integratorhttps://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/WebsiteEndpoints.html#WebsiteRestEndpointDiff
49Copyright© System Integrator Corp. All rights reserved.System Integrator1. 本日お伝えすること2. サーバーレスアーキテクチャを採用した理由3. サーバーレスでREST APIを作る4. サーバーレスでWEBアプリを作る5. 非同期処理を作る6. デプロイ/負荷試験Agenda
50Copyright© System Integrator Corp. All rights reserved.System IntegratorLambda SQS Lambdaサーバーレスで非同期処理
51Copyright© System Integrator Corp. All rights reserved.System Integrator採点キュー書き込みキュー管理 採点処理サーバーレスで非同期処理
52Copyright© System Integrator Corp. All rights reserved.System IntegratorVPC提出処理と採点処理の間にSQSを入れて非同期化AWS Cloudusers Amazon API Gateway(PRIVATE)AWS Lambda(提出)PeeringconnectionVPCAmazon EC2Elastic networkinterfaceEndpointsAmazon SQS AWS Lambda(採点)Public subnetElastic Load BalancingPrivate subnet
53Copyright© System Integrator Corp. All rights reserved.System Integrator【問題1】Lambdaがデプロイできない
54Copyright© System Integrator Corp. All rights reserved.System Integrator【原因】SQSの可視性タイムアウト(30秒)がLambdaのタイムアウト(300秒)より短い
55Copyright© System Integrator Corp. All rights reserved.System Integrator【対策】SQSの可視性タイムアウトをLambdaのタイムアウトより長くする
56Copyright© System Integrator Corp. All rights reserved.System Integrator【問題2】採点Lambdaに1度に複数の採点キューが渡されてしまう
57Copyright© System Integrator Corp. All rights reserved.System IntegratorVPC提出処理と採点処理の間にSQSを入れて非同期化AWS Cloudusers Amazon API Gateway(PRIVATE)AWS Lambda(提出)PeeringconnectionVPCAmazon EC2Elastic networkinterfaceEndpointsAmazon SQS AWS Lambda(採点)Public subnetElastic Load BalancingPrivate subnet
58Copyright© System Integrator Corp. All rights reserved.System Integrator【原因】SQSトリガーのバッチサイズが1より大きい
59Copyright© System Integrator Corp. All rights reserved.System Integrator【対策】SQSトリガーのバッチサイズを1にする
60Copyright© System Integrator Corp. All rights reserved.System Integratorマネジメントコンソールからは変更ができない
61Copyright© System Integrator Corp. All rights reserved.System Integrator1. 本日お伝えすること2. サーバーレスアーキテクチャを採用した理由3. サーバーレスでREST APIを作る4. サーバーレスでWEBアプリを作る5. 非同期処理を作る6. デプロイ/負荷試験Agenda
62Copyright© System Integrator Corp. All rights reserved.System IntegratorCodePipelineCodeBuild+ServerlessCodeDeployサーバーレスのデプロイ
63Copyright© System Integrator Corp. All rights reserved.System Integrator
64Copyright© System Integrator Corp. All rights reserved.System IntegratorServerless-framework• node.js製のツール• サーバーレスアプリをAWS上に簡単にデプロイできる(内部的にはCloudFormationが動いている)• 構成をYAMLで書ける• 例)GETリクエストでLambda関数helloを実行するAPI Gatewayを作成する• CodeBuild等と組み合わせると簡単にPipelineができる# serverless.ymlfunctions:index:handler: handler.helloevents:- http: GET hello$ serverless deploy
65Copyright© System Integrator Corp. All rights reserved.System Integratorサーバーレスの負荷試験https://github.com/Nordstrom/serverless-artillery
66Copyright© System Integrator Corp. All rights reserved.System IntegratorServerless-artillery• サーバーレスで負荷試験ができる• AWS上に負荷を生成するLambdaを簡単に生成できる• 試験シナリオがYAMLで書ける• 例)秒間10アクセスの負荷を5秒実行する• 負荷試験実行も簡単config:target: http://TESTURL/phases:- duration: 5arrivalRate: 10scenarios:- flow:- get:url: "/"slsart deploy --region=ap-northeast-1slsart invoke --region=ap-northeast-1
67Copyright© System Integrator Corp. All rights reserved.System Integrator限られた人数でやることはいっぱいアプリ開発 インフラ開発 各種サポート
68Copyright© System Integrator Corp. All rights reserved.System Integrator精神論で頑張らないAWSで仕組みを変える(わからなくても まずは挑戦!)
69Copyright© System Integrator Corp. All rights reserved.System Integratorサーバーレスを活用してみなさん素敵な人生を!
70Copyright© System Integrator Corp. All rights reserved.System Integrator※本資料掲載の情報・画像など、すべてのコンテンツの無断複写・転載を禁じます。