Creating Lambda Functions

Developer world
Developerworld
Published in
4 min readJul 7, 2018
Photo by Markus Spiske on Unsplash

In this Tutorial we are going to build a lambda function to process log files.

This function will be triggered when a new log file is added to an S3 bucket and the result of the Lambda function is to create an SNS notification if there is an error in the file

  1. Browse the following link: https://console.aws.amazon.com/lambda. Select Get Started Now to start creating a new function:

2. AWS offers many templates with sample configuration and code. For example, you could use a template of a Lambda function that processes bounced e-mails. This option is interesting for marketing campaigns to remove e-mail addresses that don’t exist. However, in this example, we will select a Blank Function template:

3. A Lambda function can be triggered by many different sources. In the next screen, you will see a list of all available options. Select S3:

4. After selecting S3, you will be presented with some configuration options. Select the bucket that we have created previously with the CLI. For the Event type, select Object Created (All) to trigger the function whenever a new file is created. For Prefix, type logs/ to consider only files in the logs folder and in the Suffix, type txt to consider only text files. Finally, check the Enable trigger option and click on Next:

5. Type a name for your Lambda function, for example, processLog, and select the Node.js 6.10 option for Runtime:

6. Now we need to implement the code that will be executed by the Lambda function using the Edit code inlineoption. In this example, we are using S3.getObject to retrieve the file created and SNS.publish to create a notification if there is the error word in this file. For the SNS topic ARN, you can use the same that was previously created with the CLI:

7. As we have used the inline option to write the code instead of uploading a ZIP file, the code will be placed inside an index.js file. Also, the module that we have created exports a function named handler. In this case, we need to configure the Lambda handler with the index.handler name. For the Role box, we need to create a new one because a Lambda function can't execute without proper access. Even if you create a Lambda using an administrator account, you must give explicit permissions for which kind of services and resources the Lambda will be able to access:

8. Type a role name for this role and click on Edit to modify the default policy document. Add the following JSON object and finish by clicking on Allow:

9. The last step is to configure the Advanced settings. Set the amount of RAM memory that should be allocated for this function and the timeout value that AWS must wait for this function to finish its execution. You may need to increase the timeout value depending on the size of the log files that you are going to use for testing:

10. Click on Next and you will be redirected to a Review page where you need to confirm the function creation

11. To test this function, we can use the Management Console, which lets us create custom input events, but, in this case, we can use the CLI to upload a new file and trigger the Lambda function. If the file has the word “error”, you should receive an e-mail message with the file name

aws s3 cp log1.txt s3://my-bucket-name/logs/

12. If you have any issues, you can try using the Management Console to see the error message that appears. In this case, use the following JSON object as the event trigger, replacing the bucket name

Follow the next story

--

--