AWS Lambda: Create a Function from CLI
Create a new AWS Lambda function using aws lambda create-function with runtime, handler, role, timeout, memory, and deployment package.
Lambda Operations
Detailed Explanation
Creating Lambda Functions from the CLI
The aws lambda create-function command deploys a new Lambda function with a specified runtime, handler, IAM role, and code package.
Basic Function Creation
aws lambda create-function \
--function-name my-api-handler \
--runtime nodejs20.x \
--handler index.handler \
--role arn:aws:iam::123456789012:role/lambda-exec-role \
--zip-file fileb://function.zip
Note: Use fileb:// (binary file) for zip archives, not file:// (text file).
With Memory and Timeout
aws lambda create-function \
--function-name data-processor \
--runtime python3.12 \
--handler app.lambda_handler \
--role arn:aws:iam::123456789012:role/lambda-exec-role \
--zip-file fileb://deployment.zip \
--timeout 300 \
--memory-size 512
Key Parameters
| Flag | Default | Range | Notes |
|---|---|---|---|
--timeout |
3 seconds | 1-900 | Maximum execution time |
--memory-size |
128 MB | 128-10240 | Also scales CPU proportionally |
--runtime |
— | See list | nodejs20.x, python3.12, java21, etc. |
--handler |
— | — | Entry point: filename.function_name |
With Environment Variables
aws lambda create-function \
--function-name my-function \
--runtime python3.12 \
--handler app.handler \
--role arn:aws:iam::123456789012:role/lambda-role \
--zip-file fileb://function.zip \
--environment "Variables={DB_HOST=mydb.cluster.rds.amazonaws.com,LOG_LEVEL=info}"
With VPC Configuration
aws lambda create-function \
--function-name vpc-function \
--runtime nodejs20.x \
--handler index.handler \
--role arn:aws:iam::123456789012:role/lambda-vpc-role \
--zip-file fileb://function.zip \
--vpc-config SubnetIds=subnet-abc123,subnet-def456,SecurityGroupIds=sg-abc123
Deployment Workflow
- Write your function code
- Create the IAM execution role with necessary permissions
- Package the code:
zip -r function.zip . - Run
create-function - Test with
aws lambda invoke
Use Case
Deploying serverless functions as part of CI/CD pipelines, creating utility functions for data processing, setting up API handlers, or provisioning event-driven microservices.