Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


Add question

You must login to ask a question.

Login

Register Now

Lorem ipsum dolor sit amet, consectetur adipiscing elit.Morbi adipiscing gravdio, sit amet suscipit risus ultrices eu.Fusce viverra neque at purus laoreet consequa.Vivamus vulputate posuere nisl quis consequat.

AWS Lambda S3 Trigger Configuration using AWS CLI : A Step-by-Step Guide

AWS Lambda S3 Trigger Configuration using AWS CLI : A Step-by-Step Guide

To set up an Amazon S3 trigger for an AWS Lambda function using the AWS CLI, you can use the aws lambda add-permission and aws s3api put-bucket-notification-configuration commands.

First, you will need to grant permission for the S3 bucket to invoke the Lambda function using the aws lambda add-permission command:

aws lambda add-permission \
  --function-name MyFunction \
  --action "lambda:InvokeFunction" \
  --principal s3.amazonaws.com \
  --source-arn arn:aws:s3:::my-bucket \
  --statement-id s3-trigger

This will grant permission for the my-bucket S3 bucket to invoke the MyFunction Lambda function. The statement-id is a unique identifier for this permission statement, and can be any string that you choose.

Next, you can use the aws s3api put-bucket-notification-configuration command to set up the S3 trigger:

aws s3api put-bucket-notification-configuration \
  --bucket my-bucket \
  --notification-configuration '{
    "LambdaFunctionConfigurations": [
      {
        "LambdaFunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:MyFunction",
        "Events": ["s3:ObjectCreated:*"],
        "Filter": {
          "Key": {
            "FilterRules": [
              {
                "Name": "prefix",
                "Value": "test-"
              },
              {
                "Name": "suffix",
                "Value": ".txt"
              }
            ]
          }
        }
      }
    ]
  }'

This will add a permission to the MyFunction Lambda function to allow it to be invoked by the s3.amazonaws.com principal (i.e., S3). It will also configure the my-bucket S3 bucket to send a notification to the

About Abhay Singh

I'm Abhay Singh, an Architect with 9 Years of It experience. AWS Certified Solutions Architect.

Follow Me

Leave a reply