【问题标题】:How to refer the environment variable value for s3 bucket name in sam template?如何在 sam 模板中引用 s3 存储桶名称的环境变量值?
【发布时间】:2021-11-14 18:45:46
【问题描述】:

我正在尝试通过引用定义的环境变量来构建 s3 事件应用程序。

在我所指的模板下方


AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  CreateThumbnail:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler
      Runtime: runtime
      Timeout: 60
      Policies: AWSLambdaExecute
      Environment:
        Variables:
          BUCKET_NAME: !Sub "${S3}"

      Events:
        CreateThumbnailEvent:
          Type: S3
          Properties:
            Bucket: ""How can refer the s3 bucket form the environment here"
            Events: s3:ObjectCreated:*

  

我实际上需要从“环境变量”中引用“BUCKET NAME”,还需要从同一个 s3 添加事件触发器。

有人可以帮忙吗?

谢谢

【问题讨论】:

    标签: amazon-s3 aws-lambda aws-sam aws-sam-cli


    【解决方案1】:

    有两种选择,

    您要么在同一个模板中定义了 S3 存储桶,然后您就可以使用逻辑 CFN ID 来引用它:

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Resources:
      S3Bucket:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: my-bucket-name
      CreateThumbnail:
        Type: AWS::Serverless::Function
        Properties:
          Handler: handler
          Runtime: runtime
          Timeout: 60
          Policies: AWSLambdaExecute
          Environment:
            Variables:
              BUCKET_NAME: !Ref S3Bucket
          Events:
            CreateThumbnailEvent:
              Type: S3
              Properties:
                Bucket: !Ref S3Bucket
                Events: s3:ObjectCreated:*
    

    或者您没有在同一个模板中定义它,在这种情况下,您必须在部署期间将其作为参数传递给堆栈,如下所示:

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Parameters:
      S3Bucket:
        Description: Name of the S3 Bucket
        Type: String
    Resources:
      CreateThumbnail:
        Type: AWS::Serverless::Function
        Properties:
          Handler: handler
          Runtime: runtime
          Timeout: 60
          Policies: AWSLambdaExecute
          Environment:
            Variables:
              BUCKET_NAME: !Ref S3Bucket
          Events:
            CreateThumbnailEvent:
              Type: S3
              Properties:
                Bucket: !Ref S3Bucket
                Events: s3:ObjectCreated:*
    

    【讨论】:

      猜你喜欢
      • 2020-01-17
      • 2020-08-11
      • 2021-11-17
      • 2019-06-18
      • 2020-09-04
      • 1970-01-01
      • 2021-08-02
      • 2020-03-22
      • 2020-07-04
      相关资源
      最近更新 更多