

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

# Contoh otorisasi Lambda untuk AWS SAM
<a name="serverless-controlling-access-to-apis-lambda-authorizer"></a>

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
<a name="serverless-controlling-access-to-apis-lambda-token-authorizer"></a>

Anda dapat mengontrol akses ke Anda APIs dengan mendefinisikan otorisasi `TOKEN` Lambda dalam template Anda. AWS SAM Untuk melakukannya, Anda menggunakan tipe data [ApiAuth](sam-property-api-apiauth.md).

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

**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](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html) di *Panduan Developer API Gateway*.

## Contoh `REQUEST` otorisasi Lambda () AWS::Serverless::Api
<a name="serverless-controlling-access-to-apis-lambda-request-authorizer"></a>

Anda dapat mengontrol akses ke Anda APIs dengan mendefinisikan otorisasi `REQUEST` Lambda dalam template Anda. AWS SAM Untuk melakukannya, Anda menggunakan tipe data [ApiAuth](sam-property-api-apiauth.md).

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

```
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](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html) di *Panduan Developer API Gateway*.

## Contoh otorisasi Lambda () AWS::Serverless::HttpApi
<a name="serverless-controlling-access-to-apis-lambda-authorizer-httpapi"></a>

Anda dapat mengontrol akses ke HTTP Anda APIs dengan mendefinisikan Lambda authorizer dalam template Anda. AWS SAM Untuk melakukannya, Anda menggunakan tipe data [HttpApiAuth](sam-property-httpapi-httpapiauth.md).

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
```