amazon-web-services - 使用 cloudformation yaml 在 Lambda 函数上添加 S3 触发器

标签 amazon-web-services aws-lambda yaml aws-sdk aws-cloudformation

所以我有一个我很想解决的问题:

  1. 创建 S3 存储桶
  2. 将此 S3 存储桶添加为我当前使用的 lambda 函数的触发器。

这是必须在 YAML 中完成的事情

我不知道如何设置... 到目前为止,我已经成功创建了一个运行完美的 s3 存储桶,现在我只需要将它作为触发器附加到 lambda 函数即可。

在 AWS 控制台中设置非常简单,但我对 YAML 没有太多经验。所以我不知道如何正确设置它,而且该文件非常敏感,所以它很痛苦:-)。

  • 运行时:node.js 10.x

希望的 yaml 最终结果:

enter image description here

最佳答案

我构建了一个小模板,并进行了测试。

模板是

  • 创建 S3 存储桶。它是触发器 Lambda,所有文件都以 txt 结尾。 如果您不需要任何过滤器,请从模板中删除过滤器
  • 创建权限,以便S3可以触发Lambda函数。 (注:我只是 假名,请相应更改)
  • 创建 Lambda(注意:我使用的是现有角色 arn:aws:iam::057351434671:role/lambda_sqs,但您可以创建或使用组织中的其他角色)

YML版本

---
AWSTemplateFormatVersion: '2010-09-09'
Description: This template is to create all resources for Config Service Api
Parameters:
  LambdaArtifactBucketName:
    Type: String
    Default: befit-artifact
  S3BucketName:
    Type: String
    Default: befit-test-s3
Resources:
  ExampleS3:
    Type: AWS::S3::Bucket
    DependsOn: ExampleInvokePermission
    Properties:
      BucketName: !Ref S3BucketName
      NotificationConfiguration:
        LambdaConfigurations:
          - Event: s3:ObjectCreated:Put
            Filter:
              S3Key:
                Rules:
                  - Name: suffix
                    Value: txt
            Function: !GetAtt [ ExampleLambdaFunction, Arn]
  ExampleInvokePermission:
    Type: AWS::Lambda::Permission
    DependsOn: ExampleLambdaFunction
    Properties:
      FunctionName:
        Fn::GetAtt:
          - ExampleLambdaFunction
          - Arn
      Action: lambda:InvokeFunction
      Principal: s3.amazonaws.com
      SourceArn:
        Fn::Sub: arn:aws:s3:::${S3BucketName}
  ExampleLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket:
          Ref: LambdaArtifactBucketName
        S3Key: emailnotification-1.0.0.jar
      FunctionName: example-lambda-function
      Handler: com.xxx.Example::handleRequest
      Role: arn:aws:iam::057351434671:role/lambda_sqs
      Runtime: java8
      Timeout: '300'
      MemorySize: 512

Outputs:
  S3BucketSecureURL:
    Value: !Join ['', ['https://', !GetAtt [ExampleS3, DomainName]]]
    Description: Name of S3 bucket

Json版本

{
   "AWSTemplateFormatVersion": "2010-09-09",
   "Description": "This template is to create all resources for Config Service Api",
   "Parameters": {
      "LambdaArtifactBucketName": {
         "Type": "String",
         "Default": "befit-artifact"
      },
      "S3BucketName": {
         "Type": "String",
         "Default": "befit-test-s3"
      }
   },
   "Resources": {
      "ExampleS3": {
         "Type": "AWS::S3::Bucket",
         "DependsOn": "ExampleInvokePermission",
         "Properties": {
            "BucketName": null,
            "NotificationConfiguration": {
               "LambdaConfigurations": [
                  {
                     "Event": "s3:ObjectCreated:Put",
                     "Filter": {
                        "S3Key": {
                           "Rules": [
                              {
                                 "Name": "suffix",
                                 "Value": "txt"
                              }
                           ]
                        }
                     },
                     "Function": null
                  }
               ]
            }
         }
      },
      "ExampleInvokePermission": {
         "Type": "AWS::Lambda::Permission",
         "DependsOn": "ExampleLambdaFunction",
         "Properties": {
            "FunctionName": {
               "Fn::GetAtt": [
                  "ExampleLambdaFunction",
                  "Arn"
               ]
            },
            "Action": "lambda:InvokeFunction",
            "Principal": "s3.amazonaws.com",
            "SourceArn": {
               "Fn::Sub": "arn:aws:s3:::${S3BucketName}"
            }
         }
      },
      "ExampleLambdaFunction": {
         "Type": "AWS::Lambda::Function",
         "Properties": {
            "Code": {
               "S3Bucket": {
                  "Ref": "LambdaArtifactBucketName"
               },
               "S3Key": "emailnotification-1.0.0.jar"
            },
            "FunctionName": "example-lambda-function",
            "Handler": "com.xxx.Example::handleRequest",
            "Role": "arn:aws:iam::057351434671:role/lambda_sqs",
            "Runtime": "java8",
            "Timeout": "300",
            "MemorySize": 512
         }
      }
   },
   "Outputs": {
      "S3BucketSecureURL": {
         "Value": null,
         "Description": "Name of S3 bucket"
      }
   }
}

模板运行后,输出将是 enter image description here enter image description here

谢谢

关于amazon-web-services - 使用 cloudformation yaml 在 Lambda 函数上添加 S3 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58167514/

相关文章:

mysql - 需要 SQL 中的一组字段中的一个

database - 启动具有许多交互的社交应用程序时要避免的代码和服务器陷阱?

postgresql - 几个小时后无法连接 AWS RDS for PostgreSQL

amazon-web-services - S3 : How to grant access to multiple buckets?

将Json文件实时存储到amazon S3的Python脚本

javascript - http.get 从尝试到达公共(public) EC2 的异步 lambda 超时

javascript - 用于获取私有(private)文件的 AWS S3 签名 url

amazon-web-services - AWS lambda 何时支持 python 3.9?

dns - Kubernetes 上的多代理 Kafka 如何设置 KAFKA_ADVERTISED_HOST_NAME

azure - Azure Pipeline 模板中的动态变量