AWS Lambda - Just give me your code

AWS lambda is a serverless compute service, that runs your code without you worrying about the compute resources used.

How things are done

Traditionally, if you want to deploy your applications, you need to

  • Launch a server.

  • Install runtime and dependencies required for your application.

  • Run your application.

But with AWS lambdas, you don’t need to do all these things, instead focus on development, and push your code and just run it!

Write your first lambda function

  1. Select Runtime (Python / Node Js) → Write your code / upload or refer a zip file from s3.

  2. If your code interacts with any other AWS services like EC2, S3, then you need to give it permissions using an IAM role. Attach that Iam role to the lambda function.

  3. Specify lambda handler. (Entry Point for lambda function)

def lambda_handler(event, context):
    print("Hello!")
  1. Change the by-default timeout to run lambda from 3 seconds to 15 minutes. (In general configuration)

  2. Check its logs in Cloud-watch log group. (name and region of log-group is specified in policy of the IAM role attached to lambda function)

Trigger Point

You can trigger a lambda function at a specific time using Event-bridge triggers.
Its like a cron, you can schedule your lambda function to run.

Even for specific AWS api calls, you can trigger a lambda.

Eg. You want a slack alert whenever, someone creates a new instance, you just specify event-bridge event with RunInstance api call → call lambda function → sends slack alert.

Cold start

It takes few milliseconds, to start your lambda function whenever its triggered. This is because it prepares its execution environment during that time. After execution, it saves this execution environment for a non-deterministic period of time, so that the continuous incoming requests can be processed faster, this is called warm start.