How can I add an existing s3 bucket as a lambda trigger using AWS Cloudformation template? Is it even possible to do so using only CloudFormation template?

0

I want to add a existing S3 bucket as a trigger for a lambda function that i am creating in the stack. I do not want to use management console to import the s3 bucket resource as i have to repeat this process multiple times with different s3 buckets.

Here is a sample of code that i am using for adding a s3 trigger for lambda whenever a txt file is uploaded in the bucket :

AWSTemplateFormatVersion: 2010-09-09
Description:  Adding Lambda trigger using existing s3 bucket
Parameters:
  LambdaS3Bucket:
    Type: String
    Description: S3 Lambda bucket
    Default: my-lambda-code-bucket
  TestTriggerBucketName:
    Type: String
    Description: S3 bucket that will work as trigger for the lambda
    Default: my-test-trigger-bucket
  

Resources:
  LambdaFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Action:
              - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
      Policies:
        - PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: "*"
              - Effect: Allow
                Action:
                  - s3:GetObject
                Resource: "arn:aws:s3:::*/*"
          PolicyName: !Sub "lambda-policy-role"
 
  TestLambdaTrigger:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: !Ref LambdaS3Bucket
        S3Key: lambda-code.zip # lambda code
      FunctionName: "lambda-function-trigger-test"
      Handler: lambda_function.lambda_handler
      Role: !GetAtt LambdaFunctionRole.Arn
      Runtime: python3.11
      Description: "Testing trigger"

  TestTriggerS3:
    Type: AWS::S3::Bucket
    DependsOn: TestTriggerInvokePermission
    Properties:
      BucketName: !Ref TestBucketName
      NotificationConfiguration:
        LambdaConfigurations:
          - Event: s3:ObjectCreated:Put
            Filter:
              S3Key:
                Rules:
                  - Name: suffix
                    Value: txt
            Function: !GetAtt TestLambdaTrigger.Arn
  
  TestTriggerInvokePermission:
    Type: AWS::Lambda::Permission
    DependsOn: TestLambdaTrigger
    Properties:
      FunctionName: !GetAtt TestLambdaTrigger.Arn
      Action: lambda:InvokeFunction
      Principal: s3.amazonaws.com
      SourceArn: !Sub arn:aws:s3:::${TestTriggerBucketName}

The above code creates the s3 bucket and then adds the trigger. I require to do the same without creating the s3 bucket. Basically i want to remove the creation of this part:

 TestTriggerS3:
    Type: AWS::S3::Bucket
    DependsOn: TestTriggerInvokePermission
    Properties:
      BucketName: !Ref TestBucketName
      NotificationConfiguration:
        LambdaConfigurations:
          - Event: s3:ObjectCreated:Put
            Filter:
              S3Key:
                Rules:
                  - Name: suffix
                    Value: txt
            Function: !GetAtt TestLambdaTrigger.Arn
asked 4 months ago169 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions