my first lambda - part 1

(this post if part of the material I cover in my devops course)

What we're going to do

Prerequisites

  • you should have an aws account
  • Inside this account you should have several permissions (e.g: create and run a lambda function..)
  • You should be familiar with the Python programming language

Basics steps

  • Open the lambda console main page
  • Choose Create function.
  • Select Author from scratch.
  • In the Basic information page, for Function name enter isTriangle.
  • For Runtime, choose Python 3.12
  • Leave architecture set to x86_64 and choose Create function.
  • Lambda is going to create your function and show you the main working page for it.
  • It is also going to create an execution role in IAM, that allow your function access some other aws services we'll need later.

What do we see in the main Lambda page

(this is just an overview, we'll go depper in further posts)

  • At the top you can see the Function overview pane/
    • The overview is displayes as Diagram by default, but you can view the SAM verion.
      The aws SAM can help you later manage everything Lambda using IaC principles.
      (We are not going to discuss SAM here)
    • In the middle you can see the Lambda function name (isTriangle in our example), and below it the list of lambda layers currently in use (there are no layers in our example).
    • In the left side there is an Add triger button, which we'll use to add a trigger that'll run our function.
    • In the right side there's an Add destination button.
      We'll discuss Configuring destinations for asynchronous invocation in a later post (and will not use it in this walkthrough).
  • Below, there's a pane with several tabs.
    We'll focus now on the Code tab

Lambda function code

  • Please paste the following code into the code editor:
 1import json
 2
 3def lambda_handler(event, context):
 4    edgeA = event['A']
 5    edgeB = event['B']
 6    edgeC = event['C']
 7    
 8    err_msg=''
 9    if edgeA + edgeB < edgeC:
10        err_msg= 'bad triangle: A+B < C'
11        print(err_msg)
12    elif edgeB + edgeC < edgeA:
13        err_msg='bad triangle: B+C < A'
14        print(err_msg)
15    elif edgeA + edgeC < edgeB:
16        err_msg='bad triangle: A+C < B'
17        print(err_msg)
18    
19    if err_msg:
20        return {
21            'statusCode': 200,
22            'body': json.dumps('bad triangle (see logs for details)')
23        }
24    else:
25        return {
26            'statusCode': 200,
27            'body': json.dumps('good triangle')
28        }
  • Note that print messages are sent to Amazon CloudWatch service (specificaly to CloudWatch logs).
  • Note that invoking this function is a web thing (using HTTP/HTTPs) , so the function responds with a 200 status code.

Triggering our function

  • This is going to be a simple test, so we trigger our code manually (that is, from the Console)
  • IMPORTANT: Your code is not deployed yet!!!!!
    The fact that you see it in your console editor does not mean it is already deployed to the Lambda service.
    Hit the Deploy button above your code.
  • Attached to the Test button there's a drop-down arrow.
    • It will open a small menu where you can edit and save a test event object.
    • This the event you'll receive as an argument to your function.
    • If you find the Test tab, that's a complete editor where you can create multiple test events.
      Use it to create 2 of them (a valid triangle and an invalid one)
    • Here's an invalid triangle object:
    1{
    2"A": 3,
    3"B": 4,
    4"C": 9
    5}
    
    • Here's a valid one:
    1{
    2"A": 3,
    3"B": 4,
    4"C": 9
    5}
    
  • Hit the Test button to see what happens.
    You should be able to see the print messages in CloudWatch log.
    (we'll cover CloudWatch in further posts)
  • One last (but important) note:
    This is not the usual case for Lambda function invocation. They should be triggered by events happening in your cloud network, or by external services, not manually.
    I hope to demonstrate this in other posts.