简体   繁体   English

使用 cloudformation yaml 在 Lambda function 上添加 S3 触发器

[英]add S3 trigger on a Lambda function with cloudformation yaml

So I have this issue that I would love to:所以我有这个问题,我很想:

  1. Create an S3-bucket创建 S3 存储桶
  2. Add this S3 bucket as a trigger to the current lambda function I'm using.将此 S3 存储桶作为触发器添加到我正在使用的当前 lambda function 中。

This is something which has to be done in YAML这是必须在 YAML 中完成的事情

I have no clue how to set this up... What I've managed to so far, is to create an s3 bucket which worked perfectly, now I just need to attach it as a trigger to the lambda function.我不知道如何设置它......到目前为止,我已经设法创建一个完美运行的 s3 存储桶,现在我只需将它作为触发器附加到 lambda function 即可。

It's pretty easy to set up in the AWS console but I just don't have much experience with YAML.在 AWS 控制台中设置非常容易,但我对 YAML 没有太多经验。 So I have no idea how to set this up correctly, and the file is really sensitive so Its a pain in the ass:-).所以我不知道如何正确设置它,而且文件非常敏感,所以它很痛苦:-)。

  • Runtime: node.js 10.x运行时:node.js 10.x

Wished end result with yaml: yaml 的希望最终结果:

在此处输入图像描述

I have built a small template, tested.我已经建立了一个小模板,经过测试。

The template are to该模板是

  • Create S3 bucket.创建 S3 存储桶。 It is trigger Lambda with all file ends with txt.它是触发器 Lambda,所有文件都以 txt 结尾。 If you don't want any Filter, please remove Filter from the template如果您不想要任何过滤器,请从模板中删除过滤器
  • Create Permission, so S3 can trigger Lambda function.创建Permission,这样S3就可以触发Lambda function。 (Note: I just fake name, please change accordingly) (注:我只是假名,请相应更改)
  • Create a Lambda (Note: I'm using an existing role arn:aws:iam::057351434671:role/lambda_sqs but you can create or use another role from your organization )创建 Lambda (注意:我使用的是现有角色 arn:aws:iam::057351434671:role/lambda_sqs 但您可以创建或使用组织中的其他角色)

YML version 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 version 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"
      }
   }
}

After the template ran, the output will be模板运行后,output 将是在此处输入图像描述 在此处输入图像描述

Thanks,谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在CloudFormation中添加带有S3触发器的Lambda函数? - How do I add a Lambda Function with an S3 Trigger in CloudFormation? 如何在lambda函数中添加s3触发器? - How to add s3 trigger to lambda function? Cloudformation 模板在 S3 事件上触发 Lambda - Cloudformation template to trigger Lambda on S3 event aws lambda - 如果手动创建 s3 存储桶,如何使用 cloudformation 添加 s3 触发器 - aws lambda - How to add s3 trigger using cloudformation if the s3 bucket is created manually CloudFormation 为现有 s3 存储桶添加触发器 - CloudFormation add trigger for existing s3 bucket 如何在lambda function中添加s3触发器function读取文件 - How to add s3 trigger function in the lambda function to read the file 使用 Java ZF20E3C5E54C0AB3D375D660B3F896F 将触发器添加到 AWS Lambda Function - Add trigger to AWS Lambda Function using Java SDK for s3 Cloudformation - S3 事件上的 Lambda 触发器? (桶已经存在) - Cloudformation - Lambda Trigger on S3 Event? (Bucket Already Exists) 使用 Cloudformation 创建触发器以启动 lambda function 并在我将文件上传到 S3 存储桶时验证哪个区域可用 - Create trigger using Cloudformation to launch a lambda function and verify in what region is available when I upload a file in S3 bucket 使用 cloudformation 为 S3 存储桶启用 Lambda 函数 - Enable Lambda function to an S3 bucket using cloudformation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM