

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 的 Lambda 授權方範例 AWS SAM
<a name="serverless-controlling-access-to-apis-lambda-authorizer"></a>

`AWS::Serverless::Api` 資源類型支援兩種類型的 Lambda 授權方：`TOKEN`授權方和`REQUEST`授權方。`AWS::Serverless::HttpApi` 資源類型僅支援 `REQUEST` 授權方。以下是每種類型的範例。

## Lambda `TOKEN`授權方範例 (AWS::Serverless::Api)
<a name="serverless-controlling-access-to-apis-lambda-token-authorizer"></a>

您可以在 AWS SAM 範本中定義 Lambda `TOKEN`授權方，以控制對 APIs存取。若要這樣做，請使用 [ApiAuth](sam-property-api-apiauth.md)資料類型。

以下是 Lambda `TOKEN`授權方的範例 AWS SAM 範本區段：

**注意**  
在下列範例中，SAM `FunctionRole`是隱含產生的。

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

如需 Lambda 授權方的詳細資訊，請參閱《 [API Gateway 開發人員指南》中的使用 API Gateway Lambda 授權方](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html)。 **

## Lambda `REQUEST`授權方範例 (AWS::Serverless::Api)
<a name="serverless-controlling-access-to-apis-lambda-request-authorizer"></a>

您可以在 AWS SAM 範本中定義 Lambda `REQUEST`授權方，以控制對 APIs存取。若要這樣做，請使用 [ApiAuth](sam-property-api-apiauth.md)資料類型。

以下是 Lambda `REQUEST`授權方的範例 AWS SAM 範本區段：

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

如需 Lambda 授權方的詳細資訊，請參閱《 [API Gateway 開發人員指南》中的使用 API Gateway Lambda 授權方](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html)。 **

## Lambda 授權方範例 (AWS::Serverless::HttpApi)
<a name="serverless-controlling-access-to-apis-lambda-authorizer-httpapi"></a>

您可以在 AWS SAM 範本中定義 Lambda 授權方，以控制對 HTTP APIs存取。若要這樣做，請使用 [HttpApiAuth](sam-property-httpapi-httpapiauth.md)資料類型。

以下是 Lambda 授權方的範例 AWS SAM 範本區段：

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