0
0
AWScloud~5 mins

Why serverless architecture matters in AWS - Why It Works

Choose your learning style9 modes available
Introduction
Serverless architecture lets you run applications without managing servers. It solves the problem of spending time and money on server setup and maintenance by letting cloud providers handle it for you.
When you want to build a website that can handle sudden traffic spikes without manual scaling.
When you need to run small pieces of code in response to events like file uploads or database changes.
When you want to save costs by paying only for the exact compute time your code uses.
When you want to focus on writing code instead of managing infrastructure.
When you want automatic scaling without configuring servers or load balancers.
Commands
This command creates a new AWS Lambda function named 'my-function' using Python 3.9 runtime. It specifies the execution role and the code package to run. This step deploys your serverless code to AWS.
Terminal
aws lambda create-function --function-name my-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-function", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-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 - Sets the name of the Lambda function
--runtime - Specifies the programming language runtime
--role - Defines the IAM role Lambda assumes to run
This command runs the Lambda function 'my-function' and saves the output to a file named output.txt. It tests that your serverless function works as expected.
Terminal
aws lambda invoke --function-name my-function output.txt
Expected OutputExpected
{ "StatusCode": 200, "ExecutedVersion": "$LATEST" }
--function-name - Specifies which Lambda function to invoke
This command fetches the last 5 log events from the Lambda function's log group. It helps you see what happened during function execution for debugging or monitoring.
Terminal
aws logs filter-log-events --log-group-name /aws/lambda/my-function --limit 5
Expected OutputExpected
{ "events": [ { "timestamp": 1685616000000, "message": "START RequestId: abcdef12-3456-7890-abcd-ef1234567890 Version: $LATEST", "ingestionTime": 1685616001000 }, { "timestamp": 1685616000100, "message": "END RequestId: abcdef12-3456-7890-abcd-ef1234567890", "ingestionTime": 1685616001100 } ] }
--log-group-name - Specifies the CloudWatch log group to query
--limit - Limits the number of log events returned
Key Concept

If you remember nothing else from this pattern, remember: serverless lets you run code without managing servers, automatically scaling and charging only for actual use.

Common Mistakes
Not assigning the correct IAM role to the Lambda function
The function will not have permission to run or access resources, causing failures.
Create and assign an IAM role with the necessary permissions before deploying the function.
Trying to run Lambda functions without packaging the code properly
AWS Lambda requires code to be zipped or uploaded correctly; otherwise, deployment fails.
Zip your function code and dependencies into a single package before deployment.
Ignoring logs and not checking CloudWatch for errors
Without logs, you cannot troubleshoot why your function failed or behaved unexpectedly.
Always check CloudWatch logs after invoking your Lambda to verify execution and debug issues.
Summary
Create a Lambda function with the AWS CLI by specifying the function name, runtime, role, handler, and code package.
Invoke the Lambda function to test that it runs correctly and returns expected results.
Use CloudWatch logs to monitor and debug your serverless function executions.