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
gefragt vor 4 Monaten172 Aufrufe
Keine Antworten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen