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 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
- The AWS LAMBDA TUTORIAL: A GUIDE TO CREATING YOUR FIRST FUNCTION provides another simple example, which creates Lambda function using a 'Blueprint' (sample code provided by AWS), while other previous examples used 'Author from scratch' to create Lambda function.
- The How to Create Your First Python 3.6 AWS Lambda Function from fullstackpython.com provides another simple example using environment variables in Lambda function. The example function takes a string to print and number of times it needs to be printed from environment variables and print them accordingly.
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.
- The Code Evaluation With AWS Lambda and API Gateway guides us to setup an api gateway for Lambda function and to invoke Lambda function via curl or from browser.
Articles to refer further
- Serverless Slash Commands with Python
- Deploying to AWS Lambda: Python + Layers + CloudWatch
- Serverless Phone Number Validation with AWS Lambda, Python and Twilio
- Sharing Code Dependencies with AWS Lambda Layers
- How to Handle your Python packaging in Lambda with Serverless plugins
- 10 Practical Examples of AWS Lambda
- 10 AWS Lambda Use Cases to Start Your Serverless Journey