Thursday, October 1, 2020

Good resources to get started with AWS Lambda (python)

This blog is the collection of the articles I have referred to get myself hands dirty with AWS Lambda. I have meticulously referred and personally tried each of these articles, before listing them here.

I have personally focused on articles dealing Lambda functions in Python programming language, since Python is my first preference programming language.

Begin with ....

The following list of articles enables to get a quick introduction to AWS Lambda 
  • The AWS Lambda Tutorial gives a broader introduction to Lambda, particularly the need for it, the building blocks associated with it (Lambda function, Event Source, Log streams), comparison of Lambda with EC2 and Elastic Beanstalk, benefits, limitation, pricing, use-cases and finally a simple Hands-on example. But it's better to follow the hands-on example from AWS documentation given in the next bullet point, since the instructions in this page is not up-to date.
  • The Create a Lambda function with the console is a quick tutorial from AWS, to get hands dirty. It takes less than 5 mins to complete the instructions. This is very basic and you'll not have much fun at the end of it.
  • The AWS Lambda with Python: A Complete Getting Started Guide is a better example to start. It demos the use of environment variables and the process to encrypt secret values using AWS KMS and then decrypting the encrypted values in lambda function. The code sample to decrypt didn't work for me, the below code worked, which is generated by AWS itself while encrypting the env variable.

import os
import boto3
from base64 import b64decode

DB_HOST = os.environ["DB_HOST"]
DB_USER = os.environ["DB_USER"]
 
ENCRYPTED = os.environ['DB_PASS']
DECRYPTED = boto3.client('kms').decrypt(
    CiphertextBlob=b64decode(ENCRYPTED),
    EncryptionContext={'LambdaFunctionName': os.environ['AWS_LAMBDA_FUNCTION_NAME']}
)['Plaintext'].decode('utf-8')


def lambda_handler(event, context):
    print("Connected to %s as %s" % (DB_HOST, DB_USER))
    print("and the secret is %s" % DECRYPTED)
    return None

Step-up ... 

The tutorials in the previous section just dealt with creating Lambda functions from AWS console UI and triggering them from the 'Test' interface from the same console UI. The next set of tutorials guides about triggering the Lambda function with an http endpoint provided by api gateway.

Articles to refer further