

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# AWS::Serverless::Connector
<a name="sam-resource-connector"></a>

配置两种资源之间的权限。有关连接器的简介，请参阅[使用 AWS SAM 连接器管理资源权限](managing-permissions-connectors.md)。

有关生成的 AWS CloudFormation 资源的更多信息，请参阅[CloudFormation 指定时生成的资源 AWS::Serverless::Connector](sam-specification-generated-resources-connector.md)。

要提供有关连接器的反馈，[请在*serverless-application-model AWS GitHub 存储库*中提交新问题](https://github.com/aws/serverless-application-model/issues/new?assignees=&labels=area%2Fconnectors,stage%2Fneeds-triage&template=other.md&title=%28Feature%20Request%29)。

**注意**  
部署到时 AWS CloudFormation， AWS SAM 会将您的 AWS SAM 资源转换为 CloudFormation 资源。有关更多信息，请参阅 [生成的 CloudFormation 资源用于 AWS SAM](sam-specification-generated-resources.md)。

## 语法
<a name="sam-resource-connector-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下任一语法。

**注意**  
对于大多数用例，我们建议使用嵌入式连接器语法。随着时间的推移，嵌入在源资源中使其更易于读取和维护。当您需要引用不在同一 AWS SAM 模板中的源资源（例如嵌套堆栈中的资源或共享资源）时，请使用`AWS::Serverless::Connector`语法。

### 嵌入式连接器
<a name="sam-resource-connector-syntax-embedded"></a>

```
<source-resource-logical-id>:
  Connectors:
    <connector-logical-id:
      Properties:
        [Destination](#sam-connector-destination): ResourceReference | List of ResourceReference
        [Permissions](#sam-connector-permissions): List
        [SourceReference](#sam-connector-sourcereference): SourceReference
```

### AWS::Serverless::Connector
<a name="sam-resource-connector-syntax-connector"></a>

```
Type: AWS::Serverless::Connector
Properties:
  [Destination](#sam-connector-destination): ResourceReference | List of ResourceReference
  [Permissions](#sam-connector-permissions): List
  [Source](#sam-connector-source): ResourceReference
```

## Properties
<a name="sam-resource-connector-properties"></a>

 `Destination`   <a name="sam-connector-destination"></a>
目标资源。  
*类型*:[ResourceReference](sam-property-connector-resourcereference.md)\$1 清单 [ResourceReference](sam-property-connector-resourcereference.md)  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Permissions`   <a name="sam-connector-permissions"></a>
允许源资源对目标资源执行的权限类型。  
`Read`包括允许从资源中读取数据的 AWS Identity and Access Management (IAM) 操作。  
`Write` 包括允许初始化和向资源写入数据的 IAM 操作。  
*有效值*：`Read` 或 `Write`  
*类型*：列表  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Source`   <a name="sam-connector-source"></a>
源资源。使用 `AWS::Serverless::Connector` 语法时为必需。  
*类型*：[ResourceReference](sam-property-connector-resourcereference.md)  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `SourceReference`   <a name="sam-connector-sourcereference"></a>
源资源。  
为源资源定义其他属性时，使用嵌入式连接器语法。
*类型*：[SourceReference](sam-property-connector-sourcereference.md)  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-resource-connector-examples"></a>

### 嵌入式连接器
<a name="sam-resource-connector-examples-embedded"></a>

以下示例使用嵌入式连接器定义 AWS Lambda 函数和 Amazon DynamoDB 表之间的 `Write` 数据连接：

```
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyTable:
    Type: AWS::Serverless::SimpleTable
  MyFunction:
    Type: AWS::Serverless::Function
    Connectors:
      MyConn:
        Properties:
          Destination:
            Id: MyTable
          Permissions:
            - Write
    ...
```

以下示例使用嵌入式连接器定义 `Read` 和 `Write` 权限：

```
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Connectors:
      MyConn:
        Properties:
          Destination:
            Id: MyTable
          Permissions:
            - Read
            - Write
  MyTable:
    Type: AWS::DynamoDB::Table
    ...
```

以下示例使用嵌入式连接器定义具有 `Id` 以外属性的源资源：

```
Transform: AWS::Serverless-2016-10-31
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Connectors:
      ApitoLambdaConn:
        Properties:
          SourceReference:
            Qualifier: Prod/GET/foobar
          Destination:
            Id: MyTable
          Permissions:
            - Read
            - Write
  MyTable:
    Type: AWS::DynamoDB::Table
    ...
```

### AWS::Serverless::Connector
<a name="sam-resource-connector--examples-connector"></a>

以下示例使用该[AWS::Serverless::Connector](#sam-resource-connector)资源对一个 AWS Lambda 函数 Amazon DynamoDB 表进行读取和写入：

```
MyConnector:
  Type: AWS::Serverless::Connector
  Properties:
    Source:
      Id: MyFunction
    Destination:
      Id: MyTable
    Permissions:
      - Read
      - Write
```

以下示例使用 [AWS::Serverless::Connector](#sam-resource-connector) 资源让 Lambda 函数写入 Amazon SNS 主题，两个资源位于同一个模板中：

```
MyConnector:
  Type: AWS::Serverless::Connector
  Properties:
    Source:
      Id: MyLambda
    Destination:
      Id: MySNSTopic
    Permissions:
      - Write
```

以下示例使用 [AWS::Serverless::Connector](#sam-resource-connector) 资源让 Amazon SNS 主题写入 Lambda 函数，然后该函数写入 Amazon DynamoDB 表，所有资源都位于同一个模板中：

```
Transform: AWS::Serverless-2016-10-31
Resources:
  Topic:
    Type: AWS::SNS::Topic
    Properties:
      Subscription:
        - Endpoint: !GetAtt Function.Arn
          Protocol: lambda

  Function:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs16.x
      Handler: index.handler
      InlineCode: |
        const AWS = require('aws-sdk');
        exports.handler = async (event, context) => {
          const docClient = new AWS.DynamoDB.DocumentClient();
          await docClient.put({ 
            TableName: process.env.TABLE_NAME, 
            Item: {
              id: context.awsRequestId,
              event: JSON.stringify(event)
            }
          }).promise();
        };
      Environment:
        Variables:
          TABLE_NAME: !Ref Table

  Table:
    Type: AWS::Serverless::SimpleTable

  TopicToFunctionConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source: 
        Id: Topic
      Destination: 
        Id: Function
      Permissions:
        - Write

  FunctionToTableConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source: 
        Id: Function
      Destination: 
        Id: Table
      Permissions:
        - Write
```

以下是上面示例中转换后的 AWS CloudFormation 模板：

```
"FunctionToTableConnectorPolicy": {
  "Type": "AWS::IAM::ManagedPolicy",
  "Metadata": {
    "aws:sam:connectors": {
      "FunctionToTableConnector": {
        "Source": {
          "Type": "AWS::Lambda::Function"
        },
        "Destination": {
          "Type": "AWS::DynamoDB::Table"
        }
      }
    }
  },
  "Properties": {
    "PolicyDocument": {
      "Version": "2012-10-17",		 	 	 
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "dynamodb:PutItem",
            "dynamodb:UpdateItem",
            "dynamodb:DeleteItem",
            "dynamodb:BatchWriteItem",
            "dynamodb:PartiQLDelete",
            "dynamodb:PartiQLInsert",
            "dynamodb:PartiQLUpdate"
          ],
          "Resource": [
            {
              "Fn::GetAtt": [
                "MyTable",
                "Arn"
              ]
            },
            {
              "Fn::Sub": [
                "${DestinationArn}/index/*",
                {
                  "DestinationArn": {
                    "Fn::GetAtt": [
                      "MyTable",
                      "Arn"
                    ]
                  }
                }
              ]
            }
          ]
        }
      ]
    },
    "Roles": [
      {
        "Ref": "MyFunctionRole"
      }
    ]
  }
}
```

# ResourceReference
<a name="sam-property-connector-resourcereference"></a>

对 [AWS::Serverless::Connector](sam-resource-connector.md) 资源类型使用的资源的引用。

**注意**  
对于同一模板中的资源，请提供 `Id`。对于不在同一模板中的资源，请使用其他属性的组合。有关更多信息，请参阅 [AWS SAM 连接器参考](reference-sam-connector.md)。

## 语法
<a name="sam-property-connector-resourcereference-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-connector-resourcereference-syntax.yaml"></a>

```
  [Arn](#sam-connector-resourcereference-arn): String
  [Id](#sam-connector-resourcereference-id): String
  [Name](#sam-connector-resourcereference-name): String
  [Qualifier](#sam-connector-resourcereference-qualifier): String
  [QueueUrl](#sam-connector-resourcereference-queueurl): String
  [ResourceId](#sam-connector-resourcereference-resourceid): String
  [RoleName](#sam-connector-resourcereference-rolename): String
  [Type](#sam-connector-resourcereference-type): String
```

## Properties
<a name="sam-property-connector-resourcereference-properties"></a>

 `Arn`   <a name="sam-connector-resourcereference-arn"></a>
资源的 ARN。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Id`   <a name="sam-connector-resourcereference-id"></a>
同一模板中资源的[逻辑 ID](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html)。  
指定后`Id`，如果连接器生成 AWS Identity and Access Management (IAM) 策略，则将从资源`Id`中推断出与这些策略关联的 IAM 角色。如果 `Id` 未指定，则为连接器提供资源 `RoleName`，以便将生成的 IAM 策略附加到 IAM 角色。
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Name`   <a name="sam-connector-resourcereference-name"></a>
资源的名称。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Qualifier`   <a name="sam-connector-resourcereference-qualifier"></a>
缩小其范围的资源限定符。`Qualifier` 替换资源约束 ARN 末尾的 `*` 值。有关示例，请参阅[API Gateway 调用 Lambda 函数](#sam-property-connector-resourcereference--examples--api-gateway-invoking-a-lambda-function)。  
限定符定义因资源类型而异。有关受支持的源资源和目的地资源类型的列表，请参阅 [AWS SAM 连接器参考](reference-sam-connector.md)。
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `QueueUrl`   <a name="sam-connector-resourcereference-queueurl"></a>
Amazon SQS 队列 URL。此属性仅适用于 Amazon SQS 资源。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `ResourceId`   <a name="sam-connector-resourcereference-resourceid"></a>
资源的 ID。例如，API Gateway API ID。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `RoleName`   <a name="sam-connector-resourcereference-rolename"></a>
与资源关联的角色名称。  
指定 `Id` 后，如果连接器生成 IAM 策略，则将从资源 `Id` 中推断出与这些策略关联的 IAM 角色。如果 `Id` 未指定，则为连接器提供资源 `RoleName`，以便将生成的 IAM 策略附加到 IAM 角色。
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Type`   <a name="sam-connector-resourcereference-type"></a>
资源的 CloudFormation 类型。有关更多信息，请参阅[AWS 资源和属性类型参考](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html)。  
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-property-connector-resourcereference--examples"></a>

### API Gateway 调用 Lambda 函数
<a name="sam-property-connector-resourcereference--examples--api-gateway-invoking-a-lambda-function"></a>

以下示例使用该[AWS::Serverless::Connector](sam-resource-connector.md)资源允许 Amazon API Gateway 调用 AWS Lambda 函数。

#### YAML
<a name="sam-property-connector-resourcereference--examples--api-gateway-invoking-a-lambda-function--yaml"></a>

```
Transform: AWS::Serverless-2016-10-31
Resources:
  MyRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service: lambda.amazonaws.com
      ManagedPolicyArns:
        - !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Role: !GetAtt MyRole.Arn
      Runtime: nodejs16.x
      Handler: index.handler
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            return {
              statusCode: 200,
              body: JSON.stringify({
                "message": "It works!"
              }),
            };
          };

  MyApi:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      Name: MyApi
      ProtocolType: HTTP

  MyStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      ApiId: !Ref MyApi
      StageName: prod
      AutoDeploy: True

  MyIntegration:
    Type: AWS::ApiGatewayV2::Integration
    Properties:
      ApiId: !Ref MyApi
      IntegrationType: AWS_PROXY
      IntegrationUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations
      IntegrationMethod: POST
      PayloadFormatVersion: "2.0"

  MyRoute:
    Type: AWS::ApiGatewayV2::Route
    Properties:
      ApiId: !Ref MyApi
      RouteKey: GET /hello
      Target: !Sub integrations/${MyIntegration}

  MyConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source: # Use 'Id' when resource is in the same template
        Type: AWS::ApiGatewayV2::Api
        ResourceId: !Ref MyApi
        Qualifier: prod/GET/hello # Or "*" to allow all routes
      Destination: # Use 'Id' when resource is in the same template
        Type: AWS::Lambda::Function
        Arn: !GetAtt MyFunction.Arn
      Permissions:
        - Write

Outputs:
  Endpoint:
    Value: !Sub https://${MyApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/prod/hello
```

# SourceReference
<a name="sam-property-connector-sourcereference"></a>

对 [AWS::Serverless::Connector](sam-resource-connector.md) 资源类型使用的源资源的引用。

## 语法
<a name="sam-property-connector-sourcereference-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-connector-sourcereference-syntax.yaml"></a>

```
[Qualifier](#sam-connector-sourcereference-qualifier): String
```

## Properties
<a name="sam-property-connector-sourcereference-properties"></a>

 `Qualifier`   <a name="sam-connector-sourcereference-qualifier"></a>
缩小其范围的资源限定符。`Qualifier` 替换资源约束 ARN 末尾的 `*` 值。  
限定符定义因资源类型而异。有关受支持的源资源和目的地资源类型的列表，请参阅 [AWS SAM 连接器参考](reference-sam-connector.md)。
*类型*：字符串  
*必需*：条件  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-property-connector-sourcereference--examples"></a>

**以下示例使用嵌入式连接器定义具有 `Id` 以外属性的源资源：**

```
Transform: AWS::Serverless-2016-10-31
...
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Connectors:
      ApitoLambdaConn:
        Properties:
          SourceReference:
            Qualifier: Prod/GET/foobar
          Destination:
            Id: MyTable
          Permissions:
            - Read
            - Write
  MyTable:
    Type: AWS::DynamoDB::Table
    ...
```