Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS Lambda Function URLs

kensh
April 13, 2022

AWS Lambda Function URLs

Lambda Function URLs, a new feature that lets you add HTTPS endpoints to any Lambda function and optionally configure Cross-Origin Resource Sharing (CORS) headers.

This lets you focus on what matters while we take care of configuring and monitoring a highly available, scalable, and secure HTTPS service.

kensh

April 13, 2022
Tweet

More Decks by kensh

Other Decks in Technology

Transcript

  1. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Amazon Web Services Japan
    Serverless Specialist SA
    Kensuke Shimokawa
    AWS Lambda Function URLs

    View Slide

  2. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Agenda
    • Function URLs の概要
    • URLの作成と更新
    • AWS CloudFormation、AWS SAM、AWS CDK
    • リクエストとレスポンスのフォーマット
    • モニタリング
    • ガバナンスの管理
    • 制限事項
    • ユースケース

    View Slide

  3. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Function URLs の概要
    動機︓HTTPSでアクセス可能な関数の作成にかかるステップとサービスを劇的に単純化する
    Function URLs は
    • 組み込みのLambdaサービス。コンソールでワンクリックで作成
    • 関数の$LATESTまたはカスタムのエイリアスの指定が可能
    • デフォルトでIAM認可をサポート
    • パブリックなエンドポイント作成のためにIAM認可を無効化または関数内で独⾃認証(
    JWTなど)実装も可能
    • シンプルなCORSの設定 – リクエストに対して⾃動的に応答

    View Slide

  4. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    URLの作成と更新

    View Slide

  5. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    コンソールでの操作 (関数作成)

    View Slide

  6. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    コンソールでの操作 (関数更新)

    View Slide

  7. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    コンソールでの操作
    https://{url-id}.lambda-url.{region}.on.aws

    View Slide

  8. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Auth type None

    View Slide

  9. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    エイリアスに対する Function URL 発⾏
    • 関数のURLは一意であり、
    特定のエイリアスまたは関
    数の$LATESTバージョンに
    マップ
    • これにより、同じ関数に対
    して複数のURLを定義
    • 開発中には、 $ LATESTを
    テストするためにURLを定
    義し、ステージング、本番
    などの各ステージエイリア
    スに対して個別URLを定義

    View Slide

  10. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    新規API
    • CreateFunctionUrlConfig
    • UpdateFunctionUrlConfig
    • GetFunctionUrlConfig
    • DeleteFunctionUrlConfig
    • ListFunctionUrlConfig

    View Slide

  11. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    AWS CloudFormationの例
    Resources:
    MyUrlFunction:
    Type: AWS::Lambda::Function
    Properties:
    Handler: index.handler
    Runtime: nodejs14.x
    Role: arn:aws:iam::123456789012:role/lambda-url-role
    CodeUri: src/
    MyFunctionUrl:
    Type: AWS::Lambda::Url
    Properties:
    TargetFunctionArn: !Ref MyUrlFunction
    AuthType: NONE
    Cors:
    AllowOrigins:
    - “https://exmample.com”

    View Slide

  12. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    AWS SAM
    WebhookFunction:
    Type: AWS::Serverless::Function
    Properties:
    CodeUri: webhook/
    Handler: index.handler
    Runtime: nodejs14.x
    AutoPublishAlias: live
    FunctionUrlConfig:
    AuthType: NONE
    Cors:
    AllowOrigins:
    - "https://example.com"
    WebhookFunctionUrlPublicPermission:
    Type: AWS::Lambda::Permission
    Properties:
    FunctionName: !Ref WebhookFunction
    Principal: '*'
    Action: 'lambda:InvokeFunctionUrl'
    FunctionUrlAuthType: NONE

    View Slide

  13. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    CDKの例

    View Slide

  14. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    リクエストとレスポンスのフォーマット

    View Slide

  15. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    リクエストイベントフォーマット
    {
    "version": "2.0",
    "routeKey": "$default",
    "rawPath": "/",
    "rawQueryString": "",
    "headers": {
    "x-amzn-trace-id": "Root=1-6252dbdd-2a88d899750a6e1f3",
    "x-forwarded-proto": "https",
    "host": "fcodhubyw.lambda-url.ap-northeast-1.on.aws",
    "x-forwarded-port": "443",
    "content-type": "application/json",
    "x-forwarded-for": " 27.0.4.123",
    "accept": "*/*",
    "user-agent": "curl/7.71.1"
    },
    "requestContext": {
    "accountId": "anonymous",
    "apiId": "shjhdrxa6xyfze4amalxzfcodm0hubyw",
    "domainName": "fcodhubyw.lambda-url.ap-northeast-1.on.aws ",
    "domainPrefix": "shjhdrxa6xyfze4amalxzfcodm0hubyw",
    "http": {
    "method": "POST",
    "path": "/",
    "protocol": "HTTP/1.1",
    "sourceIp": "27.0.4.123",
    "userAgent": "curl/7.71.1"
    },
    "requestId": "84545120-9bda-41af-9b45-6bd9fb4503a6",
    "routeKey": "$default",
    "stage": "$default",
    "time": "10/Apr/2022:13:30:05 +0000",
    "timeEpoch": 1649597405529
    },
    "body": "hello",
    "isBase64Encoded": false
    }
    リクエストのContent-typeを application/json
    または text/* に設定しないと、bodyがデフォルト
    でbase64でエンコードされる

    View Slide

  16. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    レスポンスフォーマット
    {
    "statusCode": 201,
    "headers": {
    "Content-Type": "application/json",
    "My-Custom-Header": "Custom Value"
    },
    "body": "{ \"message\": \"Hello, world!\" }",
    "cookies": [
    "Cookie_1=Value1; Expires=21 Oct 2021 07:48 GMT",
    "Cookie_2=Value2; Max-Age=78000"
    ],
    "isBase64Encoded": false
    }

    View Slide

  17. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Sub Path
    URLのサブパスへのすべてのリクエストは、Lambda関数に転送される。
    https://{url-id}.lambda-url.{region}.on.aws/users/123
    へのGETリクエストは、次のようなペイロードでLambda関数を呼び出します。
    "requestContext": {
    "accountId": "...",
    "apiId": "...",
    "authentication": null,
    "authorizer": null,
    "domainName": ”{url-id}.lambda-url.{region}.on.aws",
    "domainPrefix": "...",
    "http": {
    "method": "GET",
    "path": "/users/123",
    "protocol": "HTTP/1.1",
    "sourceIp": "...",
    "userAgent": "..."
    },

    View Slide

  18. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Rate limit
    https://docs.aws.amazon.com/lambda/latest/dg/urls-configuration.html
    • 予約された同時実行数は、関数の最大同時呼び出し数を制限
    • 関数の1秒あたりの最大リクエストレート(RPS)は、構成された予約済み同時実行の10倍に相当。
    • たとえば、予約された同時実行数100で関数を構成する場合、最大RPSは1,000

    View Slide

  19. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Function URLs の セキュリティ
    https://docs.aws.amazon.com/lambda/latest/dg/urls-auth.html
    • AuthTypeパラメーターは、 Function URL へのリクエストを認証または承認する方法を決定
    • 関数のURLを構成するときは、次のAuthTypeオプションのいずれかを指定
    • AWS_IAM
    • Lambda は AWSIdentity and Access Management(IAM)を使用して、IAMプリンシパルの
    IDポリシーと関数のリソースベースのポリシーに基づいてリクエストを認証および承認
    • 認証されたIAMユーザーとロールのみが Function URL を介して関数を呼び出し可能
    • NONE
    • Lambdaは、関数を呼び出す前に認証を実行しません。ただし、関数のリソースベースのポ
    リシーは常に有効であり、関数のURLがリクエストを受信する前にパブリックアクセスを許
    可する必要があります。関数URLへの認証されていないパブリックアクセスを許可するには
    、このオプションを選択します。

    View Slide

  20. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    AWS_IAM auth type
    • リクエストを行うプリンシパルが Function URL と同じAWSアカウントにある場合
    • プリンシパルはIDベースのポリシーで lambda:InvokeFunctionUrl 権限を持っているか、関数のリソ
    ースベースのポリシーで権限を付与されている必要がある
    • ユーザーがIDベースのポリシーですでに lambda:InvokeFunctionUrl 権限を持っている場合、リソー
    スベースのポリシーはオプション
    • リクエストを行うプリンシパルが別のアカウントにある場合
    • プリンシパルは、lambda:InvokeFunctionUrl 権限を付与するIDベースのポリシーと、呼び出そうと
    している関数のリソースベースのポリシーで付与された権限の両方を持っている必要がある
    https://docs.aws.amazon.com/lambda/latest/dg/urls-auth.html#urls-auth-iam

    View Slide

  21. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    モニタリング

    View Slide

  22. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Monitoring function URLs with CloudTrail
    関数URLの場合、Lambdaは次のAPI操作をイベントとしてCloudTrailログファイルに記録することを自動的
    にサポート
    • CreateFunctionUrlConfig
    • UpdateFunctionUrlConfig
    • DeleteFunctionUrlConfig
    • GetFunctionUrlConfig
    • ListFunctionUrlConfigs
    デフォルトでは、CloudTrail はデータイベントである InvokeFunctionUrl リクエストをログに記録しない
    ※ CloudTrailでデータイベントロギングをオンにすると記録可能

    View Slide

  23. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    CloudWatch metrics for function URLs
    Function URLs は、次の呼び出しメトリックをサポート、Sum統計の使用を推奨
    • UrlRequestCount – 対象の Function URL に対して行われたリクエストの数
    • Url4xxError – 4XX HTTPステータスコードを返したリクエストの数
    • Url5xxError – 5XX HTTPステータスコードを返したリクエストの数
    Function URLs は次のパフォーマンスメトリックもサポート、平均または最大統計を推奨
    • UrlLatency – Function URL がリクエストを受信してから、応答を返すまでの時間(ms)

    View Slide

  24. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    ガバナンスの管理

    View Slide

  25. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Function URLs の IAM policy actions
    • lambda:InvokeFunctionUrl – Function URLを呼び出して関数実行
    • lambda:CreateFunctionUrlConfig – Function URL の作成、AuthType の設定
    • lambda:UpdateFunctionUrlConfig – Function URL の更新、AuthType の設定
    • lambda:GetFunctionUrlConfig – Function URL の設定情報の参照 (特定のエイリアス)
    • lambda:ListFunctionUrlConfigs – Function URL の設定情報の一覧参照 (エイリアスを列挙)
    • lambda:DeleteFunctionUrlConfig – Function URL の削除 (再発行すると別の URI)

    View Slide

  26. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Cross-account function URL policy
    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Effect": "Allow",
    "Principal": {
    "AWS": "arn:aws:iam::444455556666:role/example"
    },
    "Action": "lambda:UpdateFunctionUrlConfig",
    "Resource": "arn:aws:lambda:us-east-2:123456789012:function:my-function"
    }
    ]
    }
    他のAWSエンティティへの Function URL アクセスを許可または拒否するには、これらのアクションを
    IAMポリシーに包含
    次のポリシーは、AWSアカウント444455556666のロールに、アカウント123456789012の Function
    URLを更新する権限を付与

    View Slide

  27. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Function URL SCP (明⽰的な拒否)
    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Effect": "Deny",
    "Action":[
    "lambda:CreateFunctionUrlConfig",
    "lambda:UpdateFunctionUrlConfig"
    ],
    "Resource": "arn:aws:lambda:*:123456789012:function:*",
    "Condition": {
    "StringNotEquals": {
    "lambda:FunctionUrlAuthType": "AWS_IAM"
    }
    }
    }
    ]
    }
    • 条件キーは、Service Control Policy
    (SCP)使用可能
    • SCPを使用して、AWS組織の組織全
    体のアクセス許可を管理
    • ユーザーが AWS_IAM 認証タイプ以
    外を使用する Function URL を作成
    または更新することを拒否するには、
    このサービスコントロールポリシー
    を使用可能

    View Slide

  28. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    制限事項

    View Slide

  29. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    制限事項 / その他
    • Lambda関数と同様の制限を共有
    • 6 MiBのリクエストとレスポンスのペイロードサイズ
    • アカウント・リージョン単位の同時実⾏数の制限
    • Provisioned/Reserved concurrency またはアカウントの同時実⾏数の10倍のTPS
    • 統合タイムアウトはなし。最⼤タイムアウト時間は関数で設定された最⼤実⾏時間
    • 追加のコストは不要 – 既存の価格 $0.20/100万リクエスト

    View Slide

  30. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    ユースケース

    View Slide

  31. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    ユースケース
    • Webhooks
    • サーバーレスのML推論
    • データ統合とプリプロセス
    • CloudFront + Function URL
    • Web Framework との統合

    View Slide

  32. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Function URLs vs. Amazon API Gateway
    • Function URLs は、リクエストの検証、スロットリング、 カスタムオーソライザー 、カスタムドメイン名、
    使用量プランなど、APIGatewayの高度な機能を必要としないパブリックエンドポイントで単一関数のマイ
    クロサービスを実装する必要があるユースケースに最適
    • Webhookハンドラー
    • フォームバリデーター
    • モバイル支払い処理
    • 広告の配備
    • 機械学習の推論
    • Amazon API Gatewayは、あらゆる規模のAPIの作成、公開、維持、監視、保護を容易にするフルマネージド
    サービス
    • JWT /カスタムオーソライザー
    • リクエスト/レスポンスの検証と変換
    • 使用量プラン
    • AWS WAFサポート

    View Slide

  33. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    CloudFront + Function URLs
    • https://myapi.com/users/…
    • https://myapi.com/orders/…
    • https://myapi.com/products/…
    https://lumigo.io/blog/aws-lambda-function-url-is-live/
    Function URLs と CloudFront を統合する場合
    • カスタムドメインの利用
    • Sub Pathごとに個別 Function URL の指定(ルーティング)
    • CloudFront のエッジキャッシングを利用
    • CloudFront ディストリビューションを AWS WAFと統合し、WAF
    ルールのホストでAPIを保護、APIに合わせたカスタムルールを作成

    View Slide

  34. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Q&A

    View Slide

  35. © 2022, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Thank you!
    Let’s curl those furls!

    View Slide