0
0
AWScloud~5 mins

Lambda execution model in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
AWS Lambda lets you run code without managing servers. It automatically runs your code when triggered and handles scaling. This model explains how Lambda runs your code behind the scenes.
When you want to run code in response to events like file uploads or API calls without managing servers.
When you need to run short tasks that start quickly and finish fast.
When you want to pay only for the compute time your code uses, not for idle servers.
When you want automatic scaling to handle many requests without manual setup.
When you want to build small, focused functions that do one job well.
Commands
This command creates a new Lambda function named 'example-function' using Python 3.9 runtime. It specifies the execution role and the handler method inside the code package.
Terminal
aws lambda create-function --function-name example-function --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": "example-function", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:example-function", "Runtime": "python3.9", "Role": "arn:aws:iam::123456789012:role/lambda-execution-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 1234, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2024-06-01T12:00:00.000+0000", "CodeSha256": "abc123def456ghi789", "Version": "$LATEST", "TracingConfig": { "Mode": "PassThrough" }, "RevisionId": "1a2b3c4d-5678-90ab-cdef-EXAMPLE11111" }
--function-name - Sets the name of the Lambda function
--runtime - Specifies the language runtime environment
--handler - Defines the entry point method in your code
This command runs the Lambda function named 'example-function' and saves the output to a file named output.txt.
Terminal
aws lambda invoke --function-name example-function output.txt
Expected OutputExpected
{ "StatusCode": 200, "ExecutedVersion": "$LATEST" }
--function-name - Specifies which Lambda function to run
This command shows the content of the output file created by the Lambda invocation, displaying the function's response.
Terminal
cat output.txt
Expected OutputExpected
"Hello from Lambda!"
Key Concept

If you remember nothing else from this pattern, remember: Lambda runs your code in short-lived containers that start on demand, run your handler, then pause or stop to save resources.

Common Mistakes
Using a handler name that does not match the code file and function.
Lambda cannot find the entry point and fails to run your code.
Ensure the handler flag matches the filename and function name exactly, like 'lambda_function.lambda_handler'.
Not assigning the correct IAM role with execution permissions.
Lambda cannot access resources or run without proper permissions, causing errors.
Create and assign an IAM role with the necessary Lambda execution permissions.
Expecting Lambda to keep running continuously like a server.
Lambda functions are short-lived and stop after execution, so persistent state is lost.
Design functions to be stateless and use external storage for persistent data.
Summary
Create a Lambda function with the correct runtime, handler, and execution role using the AWS CLI.
Invoke the Lambda function to run your code and capture its output.
Understand that Lambda runs your code in short-lived containers that start on demand and stop after execution.