AWS::Serverless::Connector - AWS Serverless Application Model

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

AWS::Serverless::Connector

設定兩個資源之間的權限。如需連接器的簡介,請參閱使用AWS SAM連接器管理資源權限

如需產生AWS CloudFormation資源的詳細資訊,請參閱AWS CloudFormation指定時產生的資源 AWS::Serverless::Connector

若要提供有關連接器的意見反應,請在serverless-application-model AWS GitHub 儲存庫中提交新問題

注意

當您部署到時AWS CloudFormation,將您的AWS SAM資源AWS SAM轉換為AWS CloudFormation資源。如需詳細資訊,請參閱 產生的AWS CloudFormation資源

語法

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

注意

我們建議在大多數使用案例中使用嵌入式連接器語法。嵌入到源資源中,可以隨著時間的推移更容易閱讀和維護。當您需要引用不在同一個AWS 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

目標資源。

類型ResourceReference| 列表 ResourceReference

必要:是

AWS CloudFormation兼容性:此屬性是唯一的,AWS SAM並且沒有相AWS CloudFormation等的屬性。

Permissions

允許來源資源對目標資源執行的權限類型。

Read包括允許從資源讀取資料的 AWS Identity and Access Management (IAM) 動作。

Write包括允許啟動資料並將資料寫入資源的 IAM 動作。

有效值ReadWrite

類型:清單

必要:是

AWS CloudFormation兼容性:此屬性是唯一的,AWS SAM並且沒有相AWS CloudFormation等的屬性。

Source

來源資源。使用AWS::Serverless::Connector語法時需要。

類型:ResourceReference

必要:有條件

AWS CloudFormation兼容性:此屬性是唯一的,AWS SAM並且沒有相AWS CloudFormation等的屬性。

SourceReference

來源資源。

注意

定義來源資源的其他屬性時,請搭配內嵌連接器語法使用。

類型:SourceReference

必要:否

AWS CloudFormation兼容性:此屬性是唯一的,AWS SAM並且沒有相AWS CloudFormation等的屬性。

範例

嵌入式接頭

下列範例使用內嵌連接器來定義AWS Lambda函Write數和 Amazon DynamoDB 表之間的資料連線:

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

下列範例使用內嵌連接器來定義ReadWrite權限:

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 DynamoDB 表格的AWS Lambda函數:

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

以下是從上面的例子中轉換的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" } ] } }