本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AWS::Serverless::Connector
配置两种资源之间的权限。有关连接器的简介,请参阅使用 Amazon SAM 连接器管理资源权限。
有关生成的 Amazon CloudFormation 资源的更多信息,请参阅 在指定了 AWS::Serverless::Connector 的情况下生成的 Amazon CloudFormation 资源。
要提供有关连接器的反馈,请在无服务器应用程序模型 Amazon GitHub 存储库中提交新问题
注意
部署到 Amazon CloudFormation 时,Amazon SAM 会将您的 Amazon SAM 资源转换为 Amazon CloudFormation 资源。有关更多信息,请参阅 为 Amazon SAM 生成的 Amazon CloudFormation 资源。
语法
要在 Amazon Serverless Application Model (Amazon SAM) 模板中声明此实体,请使用以下任一语法:
注意
对于大多数用例,我们建议使用嵌入式连接器语法。随着时间的推移,嵌入在源资源中使其更易于读取和维护。当您需要引用不在同一 Amazon SAM 模板中的源资源(例如嵌套堆栈中的资源或共享资源)时,请使用 AWS::Serverless::Connector
语法。
嵌入式连接器
<source-resource-logical-id>
: Connectors:<connector-logical-id
: Properties: Destination:ResourceReference
|List of ResourceReference
Permissions:List
SourceReference:SourceReference
AWS::Serverless::Connector
Type: AWS::Serverless::Connector Properties: Destination:
ResourceReference
|List of ResourceReference
Permissions:List
Source:ResourceReference
属性
-
Destination
-
目标资源。
必需:是
Amazon CloudFormation 兼容性:此属性为 Amazon SAM 独有,没有 Amazon CloudFormation 等效属性。
-
Permissions
-
允许源资源对目标资源执行的权限类型。
Read
包括允许从资源中读取数据的 Amazon Identity and Access Management (IAM) 操作。Write
包括允许初始化和向资源写入数据的 IAM 操作。有效值:
Read
或Write
类型:列表
必需:是
Amazon CloudFormation 兼容性:此属性为 Amazon SAM 独有,没有 Amazon CloudFormation 等效属性。
-
Source
-
源资源。使用
AWS::Serverless::Connector
语法时为必需。类型:资源引用
必需:条件
Amazon CloudFormation 兼容性:此属性为 Amazon SAM 独有,没有 Amazon CloudFormation 等效属性。
-
SourceReference
-
源资源。
注意
为源资源定义其他属性时,使用嵌入式连接器语法。
类型:源引用
必需:否
Amazon CloudFormation 兼容性:此属性为 Amazon SAM 独有,没有 Amazon CloudFormation 等效属性。
示例
嵌入式连接器
以下示例使用嵌入式连接器定义 Amazon 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
以下示例使用 AWS::Serverless::Connector 资源让 Amazon Lambda 函数对 Amazon DynamoDB 表进行读取和写入:
MyConnector: Type: AWS::Serverless::Connector Properties: Source: Id: MyFunction Destination: Id: MyTable Permissions: - Read - Write
以下示例使用 AWS::Serverless::Connector 资源让 Lambda 函数写入 Amazon SNS 主题,两个资源位于同一个模板中:
MyConnector: Type: AWS::Serverless::Connector Properties: Source: Id: MyLambda Destination: Id: MySNSTopic Permissions: - Write
以下示例使用 AWS::Serverless::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
以下是上面示例中转换后的 Amazon 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" } ] } }