This project sets up an AWS Lambda function to automatically rename files uploaded to an S3 bucket. The Lambda function is triggered by S3 events and renames files by prepending "rename-" to the new object's key. This serves as a straightforward POC project.
- Lambda Function: Deployed as
s3_rename_function, listens for S3 events, renames files by adding "renamed-" prefix, and deletes the original file. - S3 Bucket: Created with a prefix (e.g., "rename-test-"); stores files to be renamed.
- S3 Event Notification: Configured on the S3 bucket to trigger the Lambda function when a new object is created.
- IAM Roles and Policies: An IAM role is assumed by the Lambda function with an attached policy that grants permissions to interact with S3 and write logs to CloudWatch.
- Terraform: Used for infrastructure as code (IaC) deployment.
- Python: Used to write the Lambda function
- Boto3: The AWS Python SDK and client library to wrap API calls
- AWS CLI Utility: Used for quicker interactions and streamlining workflow
Open tabs for the S3, Lambda, and IAM dashboards.
- The Lambda function needs permissions to access CloudWatch Logs and S3.
- Since Lambda does not have a persistent identity, it assumes a role using AWS STS (Security Token Service).
- Open the IAM dashboard.
- Navigate to Roles → Create role.
- Leave the default selection for Trusted entity type as AWS Service (since Lambda will assume this role).
- Under Use case, select Lambda, then click Next.
- Attach the following policies:
- AWSLambdaBasicExecutionRole (for CloudWatch logging).
- AmazonS3FullAccess (consider a more restrictive policy if needed).
- Click Next, name the role, review the settings, and click Create Role.
- Copy the required code from here.
- Open the Lambda dashboard and click Create function.
- Enter a Function name and select Python 3.11 as the runtime.
- Under Execution role:
- Click Change default execution role.
- Select Use an existing role.
- From the Existing Role dropdown, choose the role created in Step 2.
- Click Create function.
- Scroll down to the code editor, paste the Python code, and click Deploy.
- Open the S3 dashboard.
- Click Create bucket and proceed with the default settings.
- Open the newly created bucket and navigate to the Properties tab.
- In the Event notifications section:
- Click Create event notification.
- Enter a Notification name (e.g.,
"on-upload"). - Under Event types, select s3:ObjectCreated:Put (triggers on new file uploads).
- Under Destination, select Choose your Lambda function and pick the function created in Step 3.
- Click Save changes.
- Upload an object to the new S3 bucket.
- Verify that the Lambda function is triggered by checking CloudWatch Logs for execution details.
Move into your projects folder inside the TheoWAF directory on your computer for example.
git clone https://github.com/aaron-dm-mcdonald/s3-object-renamer.git lambda-rename-s3
cd lambda-rename-s3terraform init
terraform apply -auto-approveNote the bucket name during terraform runtime or execute:
terraform output s3_bucket_nameaws s3 cp <LOCAL-FILE-PATH> s3://<YOUR-BUCKET-NAME>/<YOUR-FILE-KEY>aws s3 ls s3://<YOUR-BUCKET-NAME>/To test without an actual S3 event, use AWS CLI:
-
Edit the
tests/event.jsonfile with current info. This is a JSON formatted file: -
Use jq to verify formatting (optional):
jq . tests/event.json -
Use the aws CLI to invoke the lambda while passing in the JSON payload to simulate an S3 upload trigger:
aws lambda invoke --function-name s3_rename_function \
--payload file://tests/event.json tests/response.json \
--cli-binary-format raw-in-base64-out >> tests/output.txt-
- Run
cat tests/response.jsonto view contents. Expected results are "null" - Run
cat tests/output.txtto view output of the command. Expected results are a HTTP status code of 200.
- Run
- Ensure that your IAM role allows S3 read/write access for Lambda.
- The Lambda function runs in response to S3 events, so any file upload matching the event filter will trigger it.
This Lambda function listens for new file uploads to an S3 bucket and renames them by adding a "renamed-" prefix.
- Imports AWS SDK (boto3) and urllib.parse – Used for interacting with S3 and decoding filenames.
- Extracts the bucket name and file name from the event payload.
- Creates a new file name by adding the
"renamed-"prefix. - Copies the original file to the new name in the same bucket.
- Deletes the original file after copying.
