Skip to content

Run S3 Object Lambda only for certain keys?

0

Hello, is it possible to configure an Object Lambda to only run for certain keys? Example: only *.json files are processed through the lambda, the rest of the files are going directly to the bucket.

At what level is that configured (access point, the lambda or the lambda access point)?

I attempted to add a Condition to the Object Lambda Access Point policy, but I did not manage to craft a valid policy that way.

best regards Oskar

1 Answer
2
Accepted Answer

Hello.

  • To run an S3 Object Lambda for only certain keys such as *.json files, the best way to achieve this is by handling the filtering at the Lambda function level as S3 Object Lambda Access Points do not natively support complex key-based filtering in their policies.

Lambda Function Filtering

  • The most straightforward solution is to handle the logic inside the Lambda function, where it processes only specific files (e.g., .json) and passes other files through without modification.

  • Lambda Code You can check the key of the object inside your Lambda function, and only process .json files. For non-.json files, you return the object without any modifications.

import boto3

def lambda_handler(event, context):
    # Get the S3 key (file name)
    key = event['getObjectContext']['inputS3Url'].split('/')[-1]

    # Check if the key ends with .json
    if key.endswith('.json'):
        # Process the JSON file (perform transformations or return a modified response)
        return process_json_file(event)
    else:
        # Return the unmodified object for other file types
        s3 = boto3.client('s3')
        response = s3.get_object(Bucket='<your-bucket-name>', Key=key)
        return {
            'statusCode': 200,
            'body': response['Body'].read(),
            'headers': {
                'Content-Type': response['ContentType']
            }
        }

def process_json_file(event):
    # Your processing logic for JSON files here
    ...

Object Lambda Access Point Configuration

  • Object Lambda Access Points do not have direct support for filtering based on object keys (e.g., limiting operations only to .json files), and conditions such as s3:Key are not effective for this purpose in Object Lambda Access Point policies.

  • Access Point Routing Every request that goes through the S3 Object Lambda Access Point is routed to the Lambda function. So, you can't configure it at the access point level to restrict the Lambda execution to certain file types.

https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

EXPERT
answered a year ago
EXPERT
reviewed a year ago
EXPERT
reviewed a year ago

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.