0
0
AWScloud~5 mins

Lambda function concept in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to run small pieces of code without managing servers. AWS Lambda lets you do this by running your code only when needed and charging you only for the time it runs.
When you want to run code in response to events like file uploads or button clicks without setting up servers.
When you need to run small tasks that start quickly and finish fast.
When you want to save money by paying only for the exact time your code runs.
When you want to build parts of an app that automatically scale with demand.
When you want to connect different cloud services with simple code snippets.
Config File - lambda_function.py
lambda_function.py
def lambda_handler(event, context):
    message = 'Hello, ' + event.get('name', 'World') + '!'
    return {
        'statusCode': 200,
        'body': message
    }

This is a simple AWS Lambda function written in Python.

lambda_handler is the entry point AWS Lambda uses to run your code.

event contains input data, here it looks for a 'name' key.

context provides runtime info but is not used here.

The function returns a greeting message with a status code.

Commands
This command creates a new Lambda function named 'helloFunction' using Python 3.9. It specifies the execution role and the handler function inside the code package zipped as 'function.zip'.
Terminal
aws lambda create-function --function-name helloFunction --runtime python3.9 --role arn:aws:iam::123456789012:role/lambda-execution-role --handler lambda_function.lambda_handler --zip-file fileb://function.zip
Expected OutputExpected
{ "FunctionName": "helloFunction", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:helloFunction", "Runtime": "python3.9", "Role": "arn:aws:iam::123456789012:role/lambda-execution-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 123, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2024-06-01T12:00:00.000+0000", "CodeSha256": "abc123def456ghi789", "Version": "$LATEST", "TracingConfig": { "Mode": "PassThrough" }, "RevisionId": "xyz987uvw654" }
--function-name - Sets the name of the Lambda function.
--runtime - Specifies the language runtime environment.
--handler - Defines the entry point function in your code.
This command runs the Lambda function 'helloFunction' with input data where name is 'Alice'. The output is saved to 'response.json'.
Terminal
aws lambda invoke --function-name helloFunction --payload '{"name": "Alice"}' response.json
Expected OutputExpected
{ "StatusCode": 200 }
--payload - Sends input data to the Lambda function.
This command shows the output returned by the Lambda function stored in 'response.json'.
Terminal
cat response.json
Expected OutputExpected
{ "statusCode": 200, "body": "Hello, Alice!" }
Key Concept

If you remember nothing else from this pattern, remember: AWS Lambda runs your code only when triggered, so you don't need to manage servers or pay for idle time.

Common Mistakes
Using the wrong handler name in the create-function command.
AWS Lambda won't find the entry point and the function will fail to run.
Make sure the handler matches the filename and function name exactly, like 'lambda_function.lambda_handler'.
Not packaging the code correctly into a zip file before deployment.
AWS Lambda requires a zip file with your code; missing or wrong packaging causes deployment errors.
Zip your code file(s) properly and reference the zip file in the create-function command.
Not providing the correct IAM role ARN with necessary permissions.
Lambda needs permission to run; without the right role, the function creation or execution will fail.
Use an IAM role with the AWSLambdaBasicExecutionRole policy attached and provide its ARN.
Summary
Create a Lambda function by packaging your code and specifying runtime, handler, and execution role.
Invoke the Lambda function with input data to run your code on demand.
Check the output saved in a file to see the function's response.