Contoh otorisasi Lambda - AWS Serverless Application Model

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh otorisasi Lambda

Tipe sumber daya AWS::Serverless::Api yang mendukung dua tipe otorisasi Lambda: otorisasi TOKEN dan otorisasi REQUEST. Tipe sumber daya AWS::Serverless::HttpApi hanya mendukung otorisasi REQUEST. Berikut ini adalah contoh dari setiap tipe.

Contoh TOKEN otorisasi Lambda () AWS::Serverless::Api

Anda dapat mengontrol akses ke API Anda dengan menentukan otorisasi TOKEN Lambda dalam template Anda. AWS SAM Untuk melakukannya, Anda menggunakan tipe data ApiAuth.

Berikut ini adalah contoh bagian AWS SAM template untuk Authorizer LambdaTOKEN:

catatan

Dalam contoh berikut, SAM dihasilkan FunctionRole secara implisit.

Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaTokenAuthorizer Authorizers: MyLambdaTokenAuthorizer: FunctionArn: !GetAtt MyAuthFunction.Arn MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x

Untuk informasi selengkapnya tentang otorisasi Lambda, lihat Gunakan otorisasi Lambda API Gateway di Panduan Developer API Gateway.

Contoh REQUEST otorisasi Lambda () AWS::Serverless::Api

Anda dapat mengontrol akses ke API Anda dengan menentukan otorisasi REQUEST Lambda dalam template Anda. AWS SAM Untuk melakukannya, Anda menggunakan tipe data ApiAuth.

Berikut ini adalah contoh bagian AWS SAM template untuk Authorizer LambdaREQUEST:

Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaRequestAuthorizer Authorizers: MyLambdaRequestAuthorizer: FunctionPayloadType: REQUEST FunctionArn: !GetAtt MyAuthFunction.Arn Identity: QueryStrings: - auth MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x

Untuk informasi selengkapnya tentang otorisasi Lambda, lihat Gunakan otorisasi Lambda API Gateway di Panduan Developer API Gateway.

Contoh otorisasi Lambda () AWS::Serverless::HttpApi

Anda dapat mengontrol akses ke API HTTP Anda dengan mendefinisikan otorisasi Lambda dalam template Anda. AWS SAM Untuk melakukannya, Anda menggunakan tipe data HttpApiAuth.

Berikut ini adalah contoh bagian AWS SAM template untuk Authorizer Lambda:

Resources: MyApi: Type: AWS::Serverless::HttpApi Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaRequestAuthorizer Authorizers: MyLambdaRequestAuthorizer: FunctionArn: !GetAtt MyAuthFunction.Arn FunctionInvokeRole: !GetAtt MyAuthFunctionRole.Arn Identity: Headers: - Authorization AuthorizerPayloadFormatVersion: 2.0 EnableSimpleResponses: true MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: HttpApi Properties: ApiId: !Ref MyApi Path: / Method: get PayloadFormatVersion: "2.0" MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x