0
0
AWScloud~5 mins

Why serverless matters in AWS - Why It Works

Choose your learning style9 modes available
Introduction
Serverless computing lets you run code without managing servers. It solves the problem of spending time and money on server setup and maintenance.
When you want to run a small app feature without setting up a whole server.
When your app usage changes a lot and you want to pay only for what you use.
When you want to focus on writing code, not managing infrastructure.
When you need to quickly scale your app up or down based on demand.
When you want to build event-driven apps that react to user actions or data changes.
Commands
This command creates a new AWS Lambda function named 'my-serverless-function' using Python 3.9. It uses a role that allows Lambda to run and a zipped file with your code.
Terminal
aws lambda create-function --function-name my-serverless-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": "my-serverless-function", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-serverless-function", "Runtime": "python3.9", "Role": "arn:aws:iam::123456789012:role/lambda-execution-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 12345, "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-1234567890ab" }
--function-name - Names your Lambda function
--runtime - Specifies the programming language environment
--role - Grants permissions for Lambda to run
This command runs your Lambda function and saves the output to a file named output.txt.
Terminal
aws lambda invoke --function-name my-serverless-function output.txt
Expected OutputExpected
{ "StatusCode": 200, "ExecutedVersion": "$LATEST" }
--function-name - Specifies which Lambda function to run
This command lists all your Lambda functions so you can see what serverless functions you have.
Terminal
aws lambda list-functions
Expected OutputExpected
{ "Functions": [ { "FunctionName": "my-serverless-function", "Runtime": "python3.9", "Role": "arn:aws:iam::123456789012:role/lambda-execution-role", "Handler": "lambda_function.lambda_handler", "CodeSize": 12345, "Description": "", "Timeout": 3, "MemorySize": 128, "LastModified": "2024-06-01T12:00:00.000+0000", "CodeSha256": "abc123def456ghi789", "Version": "$LATEST" } ] }
Key Concept

If you remember nothing else from this pattern, remember: serverless lets you run code without managing servers, saving time and cost.

Common Mistakes
Not assigning the correct IAM role with permissions to the Lambda function
The function will fail to run because it lacks permission to access resources or execute.
Create and assign an IAM role with the minimum required permissions for your Lambda function.
Uploading code without packaging dependencies properly
The function will error out if it cannot find required libraries at runtime.
Package your code and dependencies into a zip file before uploading.
Not specifying the correct handler name in the create-function command
Lambda won't know which function to run and will fail with an error.
Ensure the handler matches your code's entry point, e.g., 'lambda_function.lambda_handler'.
Summary
Create a Lambda function with the right runtime, role, and handler using the AWS CLI.
Invoke the Lambda function to run your code and check the output.
List your Lambda functions to manage and verify your serverless apps.