AWS CloudFormation:如何从另一个 AWS 账户为 Lambda 代码指定一个存储桶?

AWS CloudFormation: How to specify a bucket from another AWS account for Lambda code?

在编写 AWS CloudFormation 模板以创建 Lambda 函数时,'Code' 字段是必需的。 我在这里找到了文档:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

文档说您可以将 Lambda 函数的源指定为 S3 存储桶中的 zip 文件。在 S3Bucket 字段中,它显示 "You can specify a bucket from another AWS account as long as the Lambda function and the bucket are in the same region."

如果您在 S3Bucket 字段中输入存储桶名称,它会尝试在同一个 AWS 账户中查找该存储桶。所以我的问题是如何从另一个 AWS 账户指定一个存储桶?

我为 CFT 创建的 yaml 代码片段:

  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      Role: !GetAtt LambdaRole.Arn
      FunctionName: 'MyLambda'
      MemorySize: 1024
      Timeout: 30
      Code: 
        S3Bucket: 'my-bucket'
        S3Key: 'my-key'

一个S3桶就是一个S3桶。它在哪个 AWS 帐户中并不重要。如果您有权访问存储桶,那么您就可以访问它。

只需提供 S3 存储桶的名称(在此特定情况下它必须位于同一区域)并确保您使用的凭据可以访问 S3 存储桶。

如果您在多个 AWS 区域部署 Cloudformation 堆栈,您可以使用 cfs3-uploader.

等工具在每个区域快速创建相同的 S3 存储桶