lambdaからrdsにアクセスする - aws...aws cloud amazon rds aws lambda service vpc aws...
Post on 23-Jan-2021
24 Views
Preview:
TRANSCRIPT
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
28th July 2020Kensuke Shimokawa
LambdaからRDSにアクセスするこれまでの課題とこれから
@_kensh
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Who am I?
NameKensuke Shimokawa
CompanyAmazon Web Services Japan K.K.
RoleServerless Specialist Solutions Architect
@_kensh
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
本セッションは
想定される聴講者
• AWSサーバーレスをご利用されている方• サーバーレスでRDBMSを利用したい方• サーバーレスサービス構築に興味がある方
ゴール
• Lambdaのこれまでの課題を再確認し、その改善を理解する
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Table of contents
• VPCへの接続性のこれまでの課題と改善
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
なぜRDBMSを使いたいのか
• 既存資産を活かしたい• 人的資源(SQL習得者)• 過去の成果物• 豊富なライブラリ
• SQL表現の豊かさ• トランザクションを利用したい
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCへの接続性の課題
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPC Lambdaについて
• グローバールサービス• Amazon S3、AWS IAM、etc.
• リージョナルサービス• Amazon API Gateway、AWS Lambda、etc.
• VPCサービス• Amazon Aurora/RDS、Amazon ElastiCache、etc.
リージョナルサービスであるLambdaをVPCサービスであるRDSに接続するには、VPCにアタッチしたLambdaにする必要がある→ここでは、VPC Lambdaと呼称することにします
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
AWS Lambda実行環境
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
AWS Lambda実行環境
Invoke API
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
Elastic network interfaceAWS Lambda
実行環境
10秒程度のENI作成時間
Invoke API
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
Elastic network interfaceAWS Lambda
実行環境
10秒程度のENI作成時間
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
10秒程度のENI作成時間AWS Lambda実行環境
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
リージョンあたりのENIの上限: 5000
サブネット内 IPアドレス空間枯渇
createNetworkInterface APIのレート制限
AWS Lambda実行環境
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCへの接続性の改善
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda実行環境
https://d1.awsstatic.com/whitepapers/Overview-AWS-Lambda-Security.pdf
EC2 ベアメタルインスタンス
Host OS
Host Kernel KVM
Firecracker Firecracker
MicroVM MicroVM
Lambda実行環境
Lambda実行環境
Lambdaランタイム
Lambdaランタイム
関数コード 関数コード• Firecrackerは、AWSが開発した新しいオープンソースのハイパーバイザー
• サーバーレスワークロード用に、2018年に導入
• MicroVMを実行するハードウェアは、複数のアカウントで実行されるワークロード間で共有
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda実行環境
https://d1.awsstatic.com/whitepapers/Overview-AWS-Lambda-Security.pdf
EC2 ベアメタルインスタンス
Host OS
Host Kernel KVM
Firecracker Firecracker
MicroVM MicroVM
Lambda実行環境
Lambda実行環境
Lambdaランタイム
Lambdaランタイム
関数コード 関数コード
AWS Lambda実行環境
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPC Lambdaが改善
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
AWS Lambda実行環境
HyperplaneNAT
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPC Lambdaが改善
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
AWS Lambda実行環境
HyperplaneNAT
AWS Hyperplane を活用して Lambda VPC からCustomer VPC にNAT機能を提供
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ENI作成タイミングの改善
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
Elastic network interfaceAWS Lambda
実行環境
HyperplaneNAT
createFunction API
関数作成時点でENIの作成最大90秒程度
createNetworkInterface API
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
AWS Lambda実行環境
Invoke API
Elastic network interface
HyperplaneNAT
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
Elastic network interfaceAWS Lambda
実行環境
HyperplaneNAT
10秒程度のENI作成時間が実行時にのってこない
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
AWS HyperplaneによりLambda VPC側でNAT可能に
Security Group
Subnet, Security Group単位でENIは共有される
HyperplaneNAT Elastic network
interface
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VPCリソースへのアクセス
Private subnet
Customer VPC
AWS Cloud
Amazon RDS
AWS Lambda Service VPC
Security Group
Security Group
ENIの共有効率が上がり、ENI個数上限、IP枯渇が緩和
Subnet, Security Group単位でENIは共有される
HyperplaneNAT
ENI
ENI
AWS Lambda実行環境
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS X-Rayでのコールドスタートの確認Lambdaサービス
Lambda関数
改善前
改善後
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS X-Rayでのコールドスタートの確認
改善前
改善後
コールドスタート=約14s
コールドスタート=438ms
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
RDBMSへの接続性の課題
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon Auroraとのコネクション
primary
ReadReplica
Write
Read
非同期レプリケーション
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon Auroraとのコネクション
primary
ReadReplica
writerendpoint
readerendpoint
Write
Read
connection
非同期レプリケーション
connectionの可視化
connection
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Readするインスタンスがスパイク
primary
ReadReplica
connection
writerendpoint
readerendpoint
Write
Read
短時間に大量の接続による負荷が集中
非同期レプリケーション
connection
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Read Replicaによる負荷分散
primary
ReadReplica
connection
writerendpoint
readerendpoint
Write
Read
非同期レプリケーション
ReadReplica
ReadReplica
ラウンドロビン接続
connection
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スケールアウトをスケールアウトで対応
primary
ReadReplica
connection
writerendpoint
readerendpoint
Write
Read
非同期レプリケーション
ReadReplica
ReadReplica
ラウンドロビン接続スケールアウト
スケールアウト
connection
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Writeするインスタンスがスパイク
primary
ReadReplica
connection
writerendpoint
readerendpoint
Write
Read ReadReplica
ReadReplica
短時間に大量の接続による負荷が集中
connection
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Writeのスパイクに対応するにはスケールアップ
primary
ReadReplica
connection
connection
writerendpoint
readerendpoint
Write
Read ReadReplica
ReadReplica
スケールアップ
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
RDBMSへの接続性の緩和を考える
Writerへのスパイクはスケールアップで対処するしかない?
接続性緩和のために、これまで利用されていた手法(復習)
• NoSQLを利用• Lambda関数実装で対処• コネクションプールの利用
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
RDBMSへの接続性の緩和NoSQLを利用
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
WriteもRDBMSでないといけないか
RDBMSを利用したい理由の多くがSQLでクエリしたいという要求では?
• Readクエリだけであれば、Read Replicaでの対応も視野に入れる• Writeが大量に発生する場合、Amazon DynamoDBにWriteをオフロードできないか?
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Read/WriteともにDynamoDB を利用
Write
Read
Amazon DynamoDB
DynamoDBにWrite負荷をオフロード
スケールアウトスケールアウト
DynamoDBに対するクエリ
スケーラビリティの仕組み上、LambdaとDynamoDBの相性は良くReadのクエリ操作がDynamoDBの方式でよい場合よく利用される
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
コマンドクエリ責務分離パターン
• コマンドクエリ責務分離• (Command-Query Responsibility Segregation:CQRS)パターンはデータを更新するコマンドと参照するクエリを分離することで、ユースケースに応じて個別にスケールすることを可能とするパターン
• データソースを分離することで異なるデータ構造を取ることも可能となる• コマンド側とクエリ側が結果整合性を許容する必要がある
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
コマンドクエリ責務共有
ユーザー
サービス
ドメインモデル ドメインモデル
データ転送
Read(クエリ)Write(コマンド)
DB
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
コマンドクエリ責務分離(CQRS)パターン
ユーザー
コマンドサービス
ドメインモデル
データ転送
クエリサービス
データ転送
DB DB
Read(クエリ)Write(コマンド)
結果整合
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
DynamoDB Streamを利用したCQRSパターン
Write
Read Amazon Aurora
Amazon DynamoDB DynamoDB Stream
AWS Lambda
DynamoDBにWrite負荷をオフロード
ReadはSQLによるクエリを可能にLambdaからAuroraへのTPSは管理可能
スケールアウトスケールアウト
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
RDBMSへの接続性の緩和Lambda関数実装で対処
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda側でRDBMSに対する負荷軽減を試みる
• コネクションの使い回しをやめる• グローバルスコープにコネクションを維持しておき再利用すると、
Lambdaインスタンスが利用がない時もコネクションは維持される。• 毎回コネクションをOpen/Closeするように実装を変更
• Amazon ElastiCacheを用いてDB負荷をオフロード• Read: クエリ結果をキャッシュ• Write: 頻繁に更新される一時情報はDBでなくCacheを利用
• ElastiCacheが利用できるように実装を変更
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
RDBMSへの接続性の緩和コネクションプールの利用
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
コネクションプーリング方式
• アプリケーション内プーリング• プーリング用ミドルウエアでプーリング• AWS Managed Serviceを利用しプーリング
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Writeのスパイク(再掲)
primary
connectionwriter
endpoint
直接データベースのエンドポイントに接続するとデータベース側が高負荷になる
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambdaの同時実行モデル 初期化処理
Cold Start Warm Start
ハンドラー処理
②① ③
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
関数コード内にプールした場合
primary
writerendpoint
Lambdaランタイム
関数コード
プールの中に3コネクションを格納しても、Lambdaの特性上、1リクエストを処理し終わるまで他のリクエストを受け付けないため、1コネクションが利用中となるのみ
利用中 利用なし
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
プーリング用ミドルウエアでプールした場合
primary
writerendpoint
ミドルウエアでプーリング管理をすることにより、データベース負荷を緩和可能
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
プーリング用ミドルウエアでプールした場合
primary
writerendpoint
ミドルウエアの導入により、プーリングはできるが基底のEC2やコンテナの管理が必要になる
Undifferentiated Heavy Lifting
EC2
Container
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Managed Serviceでプールした場合
primary
writerendpoint
Amazon RDS Proxyの導入によりプーリングの管理はすべて、AWSマネージドにオフロードできる
(詳細は次のセッションで)
Amazon RDS Proxy
proxyendpoint
NEW
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
まとめ
• LambdaでVPCリソースであるRDSのにアクセスする際の接続性が改善• DynamoDBがワークロードに相応しいケースでは、今後もDynamoDBは推奨ソリューション
• LambdaからRDSへアクセスする際にスケーリングのギャップからRDS側に負荷がかかることがあり、それをLambdaの実装で緩和したり自前のプーリングの仕組みを導入し緩和していた
• undifferentiated heavy liftingな運用を強いられていた• この機能をAWSマネージドとしてAWSの管理にオフロードしたい
• これからは、RDS Proxyが利用できる
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Thank you !
@_kensh
top related