Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  2. Add this S3 bucket as a trigger to the current lambda function I'm using.

This is something which has to be done in 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.

It's pretty easy to set up in the AWS console but I just don't have much experience with 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

Wished end result with yaml:

enter image description here

like image 750
StrawHat Avatar asked Sep 30 '19 12:09

StrawHat


1 Answers

I have built a small template , tested.

The template are to

  • Create S3 bucket. It is trigger Lambda with all file ends with txt. If you don't want any Filter, please remove Filter from the template
  • Create Permission, so S3 can trigger 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 )

YML version

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

{
   "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 enter image description here enter image description here

Thanks,

like image 171
Nghia Do Avatar answered Oct 06 '22 10:10

Nghia Do