

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

# HttpApiFunctionAuth
<a name="sam-property-function-httpapifunctionauth"></a>

在事件層級設定授權。

設定特定 API \$1 路徑 \$1 方法的驗證

## 語法
<a name="sam-property-function-httpapifunctionauth-syntax"></a>

若要在 AWS Serverless Application Model (AWS SAM) 範本中宣告此實體，請使用下列語法。

### YAML
<a name="sam-property-function-httpapifunctionauth-syntax.yaml"></a>

```
  [AuthorizationScopes](#sam-function-httpapifunctionauth-authorizationscopes): List
  [Authorizer](#sam-function-httpapifunctionauth-authorizer): String
```

## Properties
<a name="sam-property-function-httpapifunctionauth-properties"></a>

 `AuthorizationScopes`   <a name="sam-function-httpapifunctionauth-authorizationscopes"></a>
要套用至此 API、路徑和方法的授權範圍。  
`DefaultAuthorizer` 如果存在，此處列出的範圍會覆寫 套用的任何範圍。  
*類型：*清單  
*必要*：否  
*CloudFormation 相容性*：此屬性對 是唯一的 AWS SAM ，並且沒有 CloudFormation 同等屬性。

 `Authorizer`   <a name="sam-function-httpapifunctionauth-authorizer"></a>
特定函數`Authorizer`的 。若要使用 IAM 授權，請在範本的 `Globals`區段`EnableIamAuthorizer`中指定 `AWS_IAM`和 `true`的 。  
如果您已在 API 上指定全域授權方，並想要將特定函數設為公有，請將 `Authorizer`設定為 以覆寫 `NONE`。  
*類型：*字串  
*必要*：否  
*CloudFormation 相容性*：此屬性對 是唯一的 AWS SAM ，並且沒有 CloudFormation 同等屬性。

## 範例
<a name="sam-property-function-httpapifunctionauth--examples"></a>

### Function-Auth
<a name="sam-property-function-httpapifunctionauth--examples--function-auth"></a>

在函數層級指定授權

#### YAML
<a name="sam-property-function-httpapifunctionauth--examples--function-auth--yaml"></a>

```
Auth:
  Authorizer: OpenIdAuth
  AuthorizationScopes:
    - scope1
    - scope2
```

### IAM 授權
<a name="sam-property-function-httpapifunctionauth--examples--iam-authorization"></a>

在事件層級指定 IAM 授權。若要在事件層級使用`AWS_IAM`授權，您還必須在範本的 `Globals`區段`EnableIamAuthorizer`中指定 `true` 的 。如需詳細資訊，請參閱[AWS SAM 範本的全域區段](sam-specification-template-anatomy-globals.md)。

#### YAML
<a name="sam-property-function-httpapifunctionauth--examples--iam-authorization--yaml"></a>

```
Globals:
  HttpApi:
    Auth:
      EnableIamAuthorizer: true

Resources:
  HttpApiFunctionWithIamAuth:
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ApiEvent:
          Type: HttpApi
          Properties:
            Path: /iam-auth
            Method: GET
            Auth:
              Authorizer: AWS_IAM
      Handler: index.handler
      InlineCode: |
        def handler(event, context):
          return {'body': 'HttpApiFunctionWithIamAuth', 'statusCode': 200}
      Runtime: python3.9
```