

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

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