Test an S3 file Lambda Invocation in 3 easy steps
# Test an S3 file Lambda Invocation in 3 easy steps
One great example of using serverless is to trigger a Lambda event when a file is created in the S3 bucket. To do this we will use a couple of different tools. Ansible, Serverless, and Commandeer. (Note: You can do step 1 and 2 using CloudFormation, Terraform, or directly in the AWS Console. We chose one route here, but you can do it a number of different ways.)
- Create the S3 Bucket (Ansible)
- Connect the S3 bucket to Lambda (Serverless)
- Test out the Bucket -> lambda invocation (Commandeer)
# Create the S3 Bucket
In this example, we are going to create a tank bucket to handle the saving of tanks to our data lake. Below you can see Ansible code to create the bucket.
# Create commandeer-tanks s3 bucket for saving tanks
- s3_bucket:
name: commandeer-tanks
versioning: yes
tags:
DEPARTMENT: MACHINERY
ENVIRONMENT: DEV
# Set up the connection between S3 and Lambda
# tank.yml
Serverless configuration (GitHub Example)
tankS3FileCreatedEvent:
handler: tankS3FileCreatedEvent.handleFile
timeout: 900
tags:
DEPARTMENT: MACHINERY
ENVIRONMENT: DEV
events:
- existingS3:
bucket: ${self:custom.commandeer-tanks}
event: s3:ObjectCreated:*
rules:
- suffix: .json
# tank.ts
Tanks Lambda handler (GitHub Example)
import { Context, Handler, S3CreateEvent } from 'aws-lambda';
/**
* @description handle receiving a tank file from s3
* @param {S3CreateEvent} event event object
* @param {Context} context context object
* @returns {boolean} status
*/
const handleFile: Handler = async (event: S3CreateEvent, context: Context) => {
console.log('tankS3FileCreatedEvent.handleFile', { event, context });
try {
const fileName: string = decodeURIComponent(event.Records[0].s3.object.key);
console.log(fileName);
context.succeed(status);
} catch (exception) {
context.fail(exception);
}
};
export { handleFile };
# Test it Out
You can test it out using the S3 Lambda Tester inside of Commandeer.