$30 off During Our Annual Pro Sale. View Details »

Serverlessをテストしよう / Serverless Testing

kensh
August 12, 2020

Serverlessをテストしよう / Serverless Testing

サーバーレス のテスト手法について、これまでのアプリの開発と何が違うのでしょうか? 初心者の多くがブラウザから確認するなどマニュアルの手法で動作確認や検証を行なっています。サーバーレス の特徴を掴みながら、どのようにテストしたらよいかを学べるコンテンツになります。

kensh

August 12, 2020
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

  1. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Serverless Testing
    Beyond Beginner
    1
    @_kensh
    サーバーレス をテストしよう

    View Slide

  2. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Who am I?
    2
    • Name
    • Kensuke Shimokawa
    • Company
    • Amazon Web Services Japan K.K.
    • Role
    • Serverless Specialist Solutions Architect
    @_kensh

    View Slide

  3. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Agenda
    3
    • Serverless のTEST戦略
    • Serverless のUnit Test
    • Serverless のMock Test
    • Serverless のFake “Test”
    • Serverless のIntegration Test
    @_kensh

    View Slide

  4. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4
    ServerlessのTEST戦略

    View Slide

  5. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 5
    CloudFormation
    Stack
    Role
    今まで、ふわっと流してたけど、Testってどうしよう?
    Code
    Version
    Control
    Build Test Deploy
    CloudFormation

    View Slide

  6. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Testing Pyramid
    6

    View Slide

  7. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test を Pure に保つ
    7
    Handler
    Lambda
    Trigger : Event
    Context
    Resources
    DynamoDB
    S3
    RDS
    Pure language
    logic
    functional
    を が も
    知らなくて良い

    View Slide

  8. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Testing Pyramid
    8
    Pure

    View Slide

  9. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 9
    ServerlessのUnit Test

    View Slide

  10. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10
    Serverless Unit Test では、
    Test を Pure に保とう。

    View Slide

  11. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    具体例で考えてみよう。
    11
    複数注文
    合計金額
    Mobile Order

    View Slide

  12. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    シンプルなアーキテクチャ図
    12
    Amazon
    API Gateway
    AWS
    Lambda
    Amazon
    DynamoDB
    Mobile
    client

    View Slide

  13. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    シンプルなアーキテクチャ図
    13
    Amazon
    API Gateway
    AWS
    Lambda
    Amazon
    DynamoDB
    Mobile
    client
    を が

    View Slide

  14. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    シンプルなアーキテクチャ図
    14
    Amazon
    API Gateway
    AWS
    Lambda
    Amazon
    DynamoDB
    Mobile
    client
    を が
    よく知ってますか?

    View Slide

  15. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    シンプルなアーキテクチャ図
    15
    Amazon
    API Gateway
    AWS
    Lambda
    Amazon
    DynamoDB
    Mobile
    client
    を が
    よく知ってますか?
    Integration Test には外世界の知識が必要!

    View Slide

  16. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    DynamoDB 商品価格マスタ
    16
    Amazon DynamoDB
    id name yen
    22 Tea 120
    24 Cola 150
    27 Coffee 300

    View Slide

  17. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装例
    17
    def lambda_handler(event, context):
    ordered = event['multiValueQueryStringParameters']['ordered']
    placed = []
    for id in ordered:
    result = table.query(
    KeyConditionExpression=Key('id').eq(id)
    )
    item = result['Items']
    placed.extend(item)
    mapped = list(map(lambda x: x['yen'], placed))
    reduced = reduce(lambda x,y: x + y, mapped)
    conversioned = math.ceil(reduced * rate * 100) / 100
    return {
    'statusCode': 200, 'body': json.dumps("{} doller".format(conversioned))
    }

    View Slide

  18. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装例
    18
    def lambda_handler(event, context):
    ordered = event['multiValueQueryStringParameters']['ordered']
    placed = []
    for id in ordered:
    result = table.query(
    KeyConditionExpression=Key('id').eq(id)
    )
    item = result['Items']
    placed.extend(item)
    mapped = list(map(lambda x: x['yen'], placed))
    reduced = reduce(lambda x,y: x + y, mapped)
    conversioned = math.ceil(reduced * rate * 100) / 100
    return {
    'statusCode': 200, 'body': json.dumps("{} doller".format(conversioned))
    }



    View Slide

  19. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装例
    19
    def lambda_handler(event, context):
    ordered = event['multiValueQueryStringParameters']['ordered']
    placed = []
    for id in ordered:
    result = table.query(
    KeyConditionExpression=Key('id').eq(id)
    )
    item = result['Items']
    placed.extend(item)
    mapped = list(map(lambda x: x['yen'], placed))
    reduced = reduce(lambda x,y: x + y, mapped)
    conversioned = math.ceil(reduced * rate * 100) / 100
    return {
    'statusCode': 200, 'body': json.dumps("{} doller".format(conversioned))
    }



    View Slide

  20. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装例
    20
    def lambda_handler(event, context):
    ordered = event['multiValueQueryStringParameters']['ordered']
    placed = []
    for id in ordered:
    result = table.query(
    KeyConditionExpression=Key('id').eq(id)
    )
    item = result['Items']
    placed.extend(item)
    mapped = list(map(lambda x: x['yen'], placed))
    reduced = reduce(lambda x,y: x + y, mapped)
    conversioned = math.ceil(reduced * rate * 100) / 100
    return {
    'statusCode': 200, 'body': json.dumps("{} doller".format(conversioned))
    }

    合計金額

    View Slide

  21. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装例
    21
    def lambda_handler(event, context):
    ordered = event['multiValueQueryStringParameters']['ordered']
    placed = []
    for id in ordered:
    result = table.query(
    KeyConditionExpression=Key('id').eq(id)
    )
    item = result['Items']
    placed.extend(item)
    mapped = list(map(lambda x: x['yen'], placed))
    reduced = reduce(lambda x,y: x + y, mapped)
    conversioned = math.ceil(reduced * rate * 100) / 100
    return {
    'statusCode': 200, 'body': json.dumps("{} doller".format(conversioned))
    }
    UNIT TEST
    合計金額

    View Slide

  22. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装 リファクタ例
    22
    def lambda_handler(event, context):
    ordered = event['multiValueQueryStringParameters']['ordered']
    placed = []
    for id in ordered:
    result = table.query(
    KeyConditionExpression=Key('id').eq(id)
    )
    item = result['Items']
    placed.extend(item)
    conversioned = calc(placed)
    return {
    'statusCode': 200, 'body': json.dumps("{} doller".format(conversioned))
    }
    def calc(placed):
    mapped = list(map(lambda x: x['yen'], placed))
    reduced = reduce(lambda x,y: x + y, mapped)
    conversioned = math.ceil(reduced * rate * 100) / 100
    return conversioned
    UNIT TEST
    合計金額

    View Slide

  23. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Lambda関数実装 Unit Test例
    23
    @pytest.fixture()
    def placed():
    return [{'id': '22', 'name': 'Tea', 'yen': Decimal('120')},
    {'id': '24', 'name': 'Cola', 'yen': Decimal('150')},
    {'id': '27', 'name': ‘Coffee', 'yen': Decimal('300’)}]
    def test_calc(placed, mocker):
    ret = app.calc(placed)
    assert ret == Decimal(5.19)
    合計金額
    https://docs.pytest.org/

    View Slide

  24. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Pure Language による Unit Testing の意義
    24
    • 外部の知識がなくてもテストケースが書ける
    • 他のAWSサービスとの結合方法に対する知識を用いない
    • 外部API、別のクラウド、オンプレミスとの統合に対する知識を用いない
    • テストが軽くなるため、開発やデリバリーを高速化できる。
    • イテレーションがしやすくなる
    • 外部のアーキテクチャが変更された時にも、テストケースが影響を受けにくい
    • テストの陳腐化が防げる
    • テストのメンテナンスコストの軽減
    • Pure Languageなので可読性があがる
    • AWSサービスや外部サービスの識者でなくても、テストケースから仕様をみてとれる。

    View Slide

  25. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Pure Language による Unit Testing の意義
    25
    • 外部の知識がなくてもテストケースが書ける
    • 他のAWSサービスとの結合方法に対する知識を用いない
    • 外部API、別のクラウド、オンプレミスとの統合に対する知識を用いない
    • テストが軽くなるため、開発やデリバリーを高速化できる。
    • イテレーションがしやすくなる
    • 外部のアーキテクチャが変更された時にも、テストケースが影響を受けにくい
    • テストの陳腐化が防げる
    • テストのメンテナンスコストの軽減
    • Pure Languageなので可読性があがる
    • AWSサービスや外部サービスの識者でなくても、テストケースから仕様をみてとれる。
    Serverlessはコードが小さい
    TDDしやすい!!

    View Slide

  26. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 26
    ServerlessのMock Test

    View Slide

  27. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 27
    でも、リファクタに労力がかけれない、、、

    View Slide

  28. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 28
    Serverless Unit Test では Mockを使おう。

    View Slide

  29. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test Double
    29
    Test
    Double
    Dummy Stub Spy Mock Fake
    https://martinfowler.com/bliki/TestDouble.html
    Test Doubleは、テスト目的で本番オブジェクトを
    置き換える場合の置き換えデザインパターンを表す用語です。
    さまざまな種類のdoubleがあります。

    View Slide

  30. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test Double
    30
    Test
    Double
    Dummy Stub Spy Mock Fake
    https://martinfowler.com/bliki/TestDouble.html
    Test Doubleは、テスト目的で本番オブジェクトを
    置き換える場合の置き換えデザインパターンを表す用語です。
    さまざまな種類のdoubleがあります。

    View Slide

  31. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Mock の概念
    31
    • Mock は以下の場合に使われます。
    • 実動コードを呼び出したくない場合
    • 意図したコードが実行されたことを確認する
    簡単な方法がない場合
    例)
    メール送信サービスを呼び出す機能
    https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da

    View Slide

  32. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Mocking AWS
    32
    https://github.com/spulec/moto

    View Slide

  33. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Mocking AWS
    33

    View Slide

  34. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    テストケースデコレータによる自動モック化機能
    34

    View Slide

  35. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Mock による Unit Testing の意義
    35
    • 外部の知識はある程度必要だが、
    • 擬似的な結合テストケースが書ける
    • 過度なリファクタリングに頼らず、手続きを重視したロジックを書ける
    • Mock利用のため、テストは軽いまま、開発やデリバリーを高速化できる。
    • イテレーションはしやすい
    • 外部のアーキテクチャが変更された時には、テストケースが影響を受ける
    • テストの陳腐化を防止するため、結合AWSサービス設定を変えた場合にテストケースも変更する

    View Slide

  36. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Mock による Unit Testing の意義
    36
    • 外部の知識はある程度必要だが、
    • 擬似的な結合テストケースが書ける
    • 過度なリファクタリングに頼らず、手続きを重視したロジックを書ける
    • Mock利用のため、テストは軽いまま、開発やデリバリーを高速化できる。
    • イテレーションはしやすい
    • 外部のアーキテクチャが変更された時には、テストケースが影響を受ける
    • テストの陳腐化を防止するため、結合AWSサービスを変えた場合にテストケースも変更する
    Mockが利用できれば、TDDしやすい!!

    View Slide

  37. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Testing Pyramid
    37
    Pure
    Mock

    View Slide

  38. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 38
    ServerlessのFake “Test”

    View Slide

  39. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Testing Pyramid
    39
    問題はこの層︕
    Pure
    Mock

    View Slide

  40. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Serverless は、分散アーキテクチャ
    40
    静的コンテンツ
    動的データ
    記事/試合状況
    フォロー状況管理
    認可
    (フェデレーション)
    ログの保存
    ETL処理
    ログ収集 データ分析
    クローリング
    記事に対する処理
    (タグ付けなど)
    変更通知
    画像に対する処理
    (顔座標の検出)
    Amazon
    CloudWatch
    AWS X-Ray

    View Slide

  41. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Serverless は、分散アーキテクチャ
    41
    静的コンテンツ
    動的データ
    記事/試合状況
    フォロー状況管理
    認可
    (フェデレーション)
    ログの保存
    ETL処理
    ログ収集 データ分析
    クローリング
    記事に対する処理
    (タグ付けなど)
    変更通知
    画像に対する処理
    (顔座標の検出)
    Amazon
    CloudWatch
    AWS X-Ray
    ロジックが、複数サービスにまたがる。

    View Slide

  42. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 42
    Serverless Integration Test の戦略

    View Slide

  43. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test Double の Fake を使ってみる
    43
    Test
    Double
    Dummy Stub Spy Mock Fake
    https://martinfowler.com/bliki/TestDouble.html

    View Slide

  44. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake の概念
    44
    例)
    避難訓練での、避難放送
    https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da
    • Fakeとは、
    • 実際に機能する実装を備えたオブジェクト
    • 実稼働のものとは実装が異なる
    • いくつかの実装上のショートカットを取り、
    プロダクション実装より簡素化される

    View Slide

  45. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    AWS SAM CLI での Fake
    45
    1. ローカル起動用の Dummy Event データ生成
    2. ローカルでLambdaを eventデータを指定して実行
    SAM CLI
    $ sam local invoke -e event.json
    $ sam local generate-event \
    apigateway aws-proxy \
    --path datadog_report \
    --method GET > event.json
    event.json

    View Slide

  46. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    AWS SAM CLI での Fake
    46
    1. ローカルでAPI Gatewayから、Lambda呼び出し実行
    2. http://127.0.0.1:3000 で起動
    3. ローカルブラウザ、 curl などでアクセスし実行
    SAM CLI
    $ sam local start-api
    event
    $ curl http://127.0.0.1:3000/hello

    View Slide

  47. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    AWS が提供する、その他の Fake
    47
    Step Functions Local ECS Local Container
    Endpoints
    CodeBuild Local Builds
    CodeBuild環境をローカルで
    シミュレートして、BuildSpec
    ファイルにあるコマンドと設
    定のトラブルシューティング
    をすぐに行えるようになります
    Fargate&ECSに展開
    する前にアプリケー
    ションをローカルで
    テストするのに役立
    ちます
    ローカル開発環境で実
    行されている
    StepFunctionsを使用
    してアプリケーション
    を開発およびローカル
    テストできます
    https://hub.docker.com/u/amazon
    DynamoDB Local
    プロビジョニング
    されたスループッ
    ト、データストレ
    ージ、またはデー
    タ転送のコストは
    ありません。

    View Slide

  48. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 48
    3rd Party Fake https://localstack.cloud/

    View Slide

  49. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 49
    3rd Party Fake https://localstack.cloud/
    •API Gateway at http://localhost:4567
    •Kinesis at http://localhost:4568
    •DynamoDB at http://localhost:4569
    •DynamoDB Streams at http://localhost:4570
    •S3 at http://localhost:4572
    •Firehose at http://localhost:4573
    •Lambda at http://localhost:4574
    •SNS at http://localhost:4575
    •SQS at http://localhost:4576
    •Redshift at http://localhost:4577
    •Elasticsearch Service at http://localhost:4578
    •SES at http://localhost:4579
    •Route53 at http://localhost:4580
    •CloudFormation at http://localhost:4581
    •CloudWatch at http://localhost:4582
    •SSM at http://localhost:4583
    •SecretsManager at http://localhost:4584
    •StepFunctions at http://localhost:4585
    •CloudWatch Logs at http://localhost:4586
    •EventBridge (CloudWatch Events) at http://localhost:4587
    •STS at http://localhost:4592
    •IAM at http://localhost:4593
    •EC2 at http://localhost:4597
    •KMS at http://localhost:4599

    View Slide

  50. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 50
    https://localstack.cloud/
    3rd Party Fake

    View Slide

  51. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake によるテストを、どの層に含めるか?
    51
    Pure
    Mock

    View Slide

  52. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake によるテストを、どの層に含めるか?
    52
    Pure
    Mock
    Fake
    Unit Test ?

    View Slide

  53. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake による Unit Testing の問題意識
    53
    • 外部の知識が十分でないとテストケースが書けない
    • 他のAWSサービスとの結合方法に対する知識が必要
    • Fakeモジュールに関する知識が必要 ( ie. Dockerでの起動、compose方法 )
    • テストが重くなるため、開発やデリバリーが低速化する。
    • イテレーションがし難くなる
    • 外部のアーキテクチャが変更された時にも、テストケースが容易に影響を受ける
    • テストの陳腐化が起り易い
    • テストのメンテナンスコストの増大
    • Test Case維持が属人化する
    • AWSサービスや、Fakeの識者でないと、テストケースから仕様やアーキテクチャが判断し難い

    View Slide

  54. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake による Unit Testing の問題意識
    54
    • 外部の知識が十分でないとテストケースが書けない
    • 他のAWSサービスとの結合方法に対する知識が必要
    • Fakeモジュールに関する知識が必要 ( ie. Dockerでの起動、compose方法 )
    • テストが重くなるため、開発やデリバリーが低速化する。
    • イテレーションがし難くなる
    • 外部のアーキテクチャが変更された時にも、テストケースが容易に影響を受ける
    • テストの陳腐化が起り易い
    • テストのメンテナンスコストの増大
    • Test Case維持が属人化する
    • AWSサービスや、Fakeの識者でないと、テストケースから仕様やアーキテクチャが判断し難い
    TDDし難い!!
    Unit Test と呼ぶには規模が大きい

    View Slide

  55. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake によるテストを、どの層に含めるか?
    55
    Pure
    Mock
    Fake
    Integration Test ?

    View Slide

  56. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake による Integration Testing の問題意識
    56
    • コストをかけてLocal Fakeを準備しても
    • 結局、本物のAWSサービスと動作が違う
    • 最新のサービス機能や新サービスが、Fakeに反映されていない
    • 統合サポートが不十分
    • 結合で確認したい永続層のサポートが不十分
    • Fake 間の Event Drivenな統合のサポートが不十分
    • Test が Fail したときに判断がし辛い
    • 実装ロジックが悪いのか
    • Fakeを用いたTest Caseが悪いのか
    • Fake自体の実装が悪いのか
    • ローカル環境依存の間欠的なものなのか ( docker container 多数起動によるリソース枯渇?)

    View Slide

  57. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Fake による Integration Testing の問題意識
    57
    • コストをかけてLocal Fakeを準備しても
    • 結局、本物のAWSサービスと動作が違う
    • 最新のサービス機能や新サービスが、Fakeに反映されていない
    • 統合サポートが不十分
    • 結合で確認したい永続層のサポートが不十分
    • Fake 間の Event Drivenな統合のサポートが不十分
    • Test が Fail したときに判断がし辛い
    • 実装ロジックが悪いのか
    • Fakeを用いたTest Caseが悪いのか
    • Fake自体の実装が悪いのか
    • ローカル環境依存の間欠的なものなのか ( docker container 多数起動によるリソース枯渇?)
    Integration Test はAWS上でやりたい

    View Slide

  58. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 58
    Pure
    Mock
    Fake はCodingフェーズには便利 (補足)
    Fake
    Coding中、Fakeを常駐させ
    ておき、対話させるなど便利

    View Slide

  59. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 59
    ServerlessのIntegration Test

    View Slide

  60. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 60
    Serverless Integration Testing on AWS

    View Slide

  61. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Serverless は使った分だけのお支払い (おさらい)
    61
    • リクエスト数、ならびに処理の実行時間
    に応じた課金となり、実際の処理量と金
    額が比例する
    • 事前の初期投資が不要なので、コストゼ
    ロからのスタートが可能

    View Slide

  62. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Multi Environment
    62
    Environment
    Development Pipeline Test Production
    デリバリーライフサイクルに応じて環境を個別に用意する

    View Slide

  63. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Multi Environment
    63
    デリバリーライフサイクルに応じて環境を個別に用意する
    Environment
    Development Pipeline Test Production
    Serverless Serviceはご利用分の従量課金
    だから、環境分離に向いています

    View Slide

  64. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    どう環境を分けるのか?
    64
    VPC Subnet
    Region CloudFormation
    Stack
    Account

    View Slide

  65. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    どう環境を分けるのか?
    65
    VPC Subnet
    Region CloudFormation
    Stack
    Account
    Serverless Serviceは、VPCや
    Subnetに依存していないものも
    多いため、難しい
    商用と同じ環境でStackの
    Deploy/Rollbackを、実行
    するのは危険
    Account単位のサービス
    上限のケアや、Region
    単位での、新サービスの
    対応状況の違いがある
    可能であれば、Multi
    Accout のデリバリー
    パイプラインを構築

    View Slide

  66. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Environment
    Development Pipeline Test Production
    Multi Account
    66
    デリバリーライフサイクルに応じて AWS Account を個別に用意する
    Account Account Account
    Account

    View Slide

  67. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    AWS Organizations を利用すると、Account管理が便利に
    67
    Organization Organization root
    OU
    AWS accounts
    Admin (master) AWS account
    AWS accounts
    AWS accounts
    OU
    OU
    OU
    OU
    https://www.slideshare.net/AmazonWebServicesJapan/20180214-aws-blackbeltorganizations

    View Slide

  68. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    68
    Developers
    AWS Cloud AWS Cloud
    AWS Cloud
    AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    CloudFormation
    CloudFormation
    Stack
    Test Account
    Production Account
    Pipeline Account
    Development Account
    Cross Accountにまた
    がる権限が必要
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/

    View Slide

  69. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    69
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    IAM Role
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers

    View Slide

  70. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    70
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    Code Checkout
    IAM Role
    Assum
    e
    Role
    Put Code
    CodeCommitから
    CodeをCheckout
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers

    View Slide

  71. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Developers
    Cross Account Delivery Pipeline
    71
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    Code Checkout
    IAM Role
    Assum
    e
    Role
    Put Code
    Trigger
    Sourceが checkout
    完了したら、
    CodeBuildを実⾏
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/

    View Slide

  72. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    72
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    Code Checkout
    IAM Role
    Assum
    e
    Role
    Put Code
    Trigger
    Put Package
    Codeをビルドし、
    PackageをS3にPutする
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers

    View Slide

  73. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    73
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    Code Checkout
    IAM Role
    Assum
    e
    Role
    Put Code
    Trigger
    Put Package
    Assum
    e
    Role
    Pass the Role
    CloudFormation 実⾏
    Execute
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers

    View Slide

  74. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    74
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    Code Checkout
    IAM Role
    Assum
    e
    Role
    Put Code
    Trigger
    Put Package
    Assum
    e
    Role
    Pass the Role
    Execute
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers

    View Slide

  75. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline
    75
    AWS Cloud AWS Cloud AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    Test Account
    Pipeline Account
    Development Account
    IAM Role
    Bucket
    IAM Role
    Code Checkout
    IAM Role
    Assum
    e
    Role
    Put Code
    Trigger
    Put Package
    Assum
    e
    Role
    Pass the Role
    Execute
    Cross Account でデリバリー完了!
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers

    View Slide

  76. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    実際にIntegration Test してみよう
    76
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Integration Test 対象
    サービス

    View Slide

  77. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    実際にIntegration Test してみよう
    77
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Container
    Amazon Elastic Container
    Service
    テストシナリオを
    配備しておく
    テスト実⾏⽤の
    リソースを確保

    View Slide

  78. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    実際にIntegration Test してみよう
    78
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Container
    Amazon Elastic Container
    Service
    テストシナリオを
    実⾏

    View Slide

  79. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    実際にIntegration Test してみよう
    79
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Container
    Amazon Elastic Container
    Service
    テストシナリオを
    実⾏
    Cross Account でIntegration Test 完了!

    View Slide

  80. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    80
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Container
    Amazon Elastic Container
    Service
    テスト実⾏⽤の
    リソースを確保

    View Slide

  81. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    81
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Container
    Amazon Elastic Container
    Service
    テスト実⾏⽤の
    リソースを確保
    Test 用のリソースも
    Serverlessで作るのは?

    View Slide

  82. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    82
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    Container
    Amazon Elastic Container
    Service

    View Slide

  83. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    83
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    funtion
    AWS Lambda
    テスト実⾏⽤の
    リソースを
    Serverless化

    View Slide

  84. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 84
    https://github.com/Nordstrom/serverless-artillery
    3rd Party

    View Slide

  85. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 85
    https://github.com/Nordstrom/serverless-artillery
    3rd Party

    View Slide

  86. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    86
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    funtion
    AWS Lambda
    Serverless-artillery
    テストシナリオを
    配備しておく

    View Slide

  87. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    87
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    funtion
    AWS Lambda
    Serverless-artillery
    テストシナリオを
    実⾏

    View Slide

  88. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Test のためのリソース維持やコストが気になる?
    88
    AWS Cloud
    Test Account
    https://aws.amazon.com/jp/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
    Developers
    AWS Cloud
    Development Account
    funtion
    AWS Lambda
    Serverless-artillery
    テストシナリオを
    実⾏
    Serverless による Serverless の試験完了!

    View Slide

  89. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    Cross Account Delivery Pipeline with Testing
    89
    Developers
    AWS Cloud AWS Cloud
    AWS Cloud
    AWS Cloud
    AWS CodeCommit
    AWS CodeBuild
    AWS CodePipeline CloudFormation
    CloudFormation
    Stack
    CloudFormation
    Stack
    Test Account
    Production Account
    Pipeline Account
    Development Account
    テスト実⾏⽤
    Test Requests
    Test Script

    View Slide

  90. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 90
    まとめ

    View Slide

  91. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    まとめ
    91
    • 今回は、サーバーレス のテスト方法の一部をご紹介しました。
    • 環境やプロダクトやビジネスに応じて、また、異なった戦略があるかもしれません。
    • ワークロードに適合する戦略を立てテストしましょう。
    • デプロイするだけで、実行されなければ課金されないという、サーバーレス の特徴を活か
    して、Multi Accountでテスト環境を整備するメリットもご紹介いたしました。
    • テスト効率を上げることによって、デリバリーパイプラインを高速化できます。
    • よりビジネスに貢献する開発に、その時間を当てることができます。
    • テストを自動化して、属人化したテスト手法を見直してみましょう。

    View Slide

  92. © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. 92
    Thank you !
    @_kensh

    View Slide