- Newest
- Most votes
- Most comments
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
Relevant content
- asked 2 years ago
- asked 9 months ago
- asked 2 years ago
