How to Create AWS Lambda Function: Simple Steps
To create a
AWS Lambda function, you define a function with a runtime, handler, and permissions, then deploy your code. You can create it via the AWS Management Console or AWS CLI by specifying the function name, runtime, role, and code source.Syntax
Creating a Lambda function requires specifying key parts:
- FunctionName: The name you give your function.
- Runtime: The language environment (e.g.,
python3.9,nodejs18.x). - Role: The AWS IAM role that grants permissions.
- Handler: The entry point in your code (file and function name).
- Code: The actual function code or location (zip file or S3 bucket).
bash
aws lambda create-function \ --function-name MyFunction \ --runtime python3.9 \ --role arn:aws:iam::123456789012:role/lambda-execution-role \ --handler lambda_function.lambda_handler \ --zip-file fileb://function.zip
Example
This example shows how to create a simple Python Lambda function using AWS CLI. The function prints a greeting message.
python
import json def lambda_handler(event, context): return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }
Example
Use this AWS CLI command to create the Lambda function after packaging the code into function.zip:
bash
aws lambda create-function \ --function-name HelloLambda \ --runtime python3.9 \ --role arn:aws:iam::123456789012:role/lambda-execution-role \ --handler lambda_function.lambda_handler \ --zip-file fileb://function.zip
Output
{
"FunctionName": "HelloLambda",
"FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:HelloLambda",
"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": "...",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "..."
}
Common Pitfalls
Common mistakes when creating Lambda functions include:
- Not assigning the correct IAM role with necessary permissions.
- Incorrect handler name format (should be
filename.functionname). - Uploading code in the wrong format or forgetting to zip files.
- Using unsupported runtimes or outdated versions.
- Not setting environment variables or timeout properly.
Always check AWS Lambda logs in CloudWatch if the function does not behave as expected.
bash
Wrong handler example: aws lambda create-function \ --function-name BadHandler \ --runtime python3.9 \ --role arn:aws:iam::123456789012:role/lambda-execution-role \ --handler wronghandler \ --zip-file fileb://function.zip Correct handler example: aws lambda create-function \ --function-name GoodHandler \ --runtime python3.9 \ --role arn:aws:iam::123456789012:role/lambda-execution-role \ --handler lambda_function.lambda_handler \ --zip-file fileb://function.zip
Quick Reference
Tips for creating Lambda functions:
- Use the AWS Console for a simple UI-based creation.
- Use AWS CLI or SDKs for automation and scripting.
- Always assign a minimal IAM role with only needed permissions.
- Package your code correctly as a zip file or use container images.
- Test your function with sample events before production use.
Key Takeaways
Create Lambda functions by specifying name, runtime, role, handler, and code.
Use AWS CLI or Console to deploy your function easily.
Ensure the IAM role has correct permissions for Lambda execution.
Package your code properly as a zip file with the correct handler path.
Test and monitor your Lambda function using CloudWatch logs.