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

ALSO READ  AWS CLI: Commands-

Abhay Singh

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

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *