AWS Lambda - sitapriyanka/aws GitHub Wiki
AWS Lambda is a computing service that runs developer code in response to events and manages compute resources for them.
- Execute Code Virtually
- Pay only for the compute time and memory you consume.
- No management headache of servers
- Scales automatically ';]']90 Pay per use pricing: First 1 million requests are free Request Charges- 1 cent for every 50000 requests Compute charges - 0.0000167 for every GB second free tire 4,00,000 gb-s
If you use a service specific to a region the service will have region-specific endpoint. for example, AWS Lambda in N.Virginia region has lambda.us-east-1.amazonaws.com
Example 1- Creating a simple lambda and executed it through management console. Example 2- Create a apigateway endpoint and invoking lambda from that gateway endpoint through management console. it is difficult to create complex lambda and apigateways using management console.
- ServerlessFramework.com can be used to create serverless frameworks.
Some Useful Commands
- npm install -g serverless if we use -g serverless command is available in any location
- serverless config credentials --provider=aws --key=AKIAU7KRRUPRJBUCKN56 --secret=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 To Configure AWS Credentials
- serverless create --template=HelloWorld To Create a new project using template
- serverless deploy this creates a cloudformation stack templates and execute them in aws
- serverless invoke helloworld this executes hello world lambda function in aws
- serverless invoke local helloworld this executes function hello world locally.
What is event, context, callback in AWSLambda function signature
- event is a object that we send during triggering. example context object is which has details about request and execution limits.
-
context is {
-
awsRequestId: 'id',
-
invokeid: 'id',
-
logGroupName: '/aws/lambda/lambda-dev-helloWorld',
-
logStreamName: '2015/09/22/[HEAD]13370a84ca4ed8b77c427af260',
-
functionVersion: 'HEAD',
-
isDefaultFunctionVersion: true,
-
functionName: 'lambda-dev-helloWorld',
-
memoryLimitInMB: '1024',
-
succeed: [Function: succeed],
-
fail: [Function: fail],
-
done: [Function: done],
-
getRemainingTimeInMillis: [Function: getRemainingTimeInMillis]
-
}
-
Default time allocation is 6 ms.
-
serverless invoke local --function=helloWorld --path=event.json this command is to pass event to lambda function
Debugging of AWS Lambda functions
- console.log() statement logs into CloudWatch while running a lambda function in aws.
- serverless logs --function=helloWorld To get the logs we can use serverless command.
- That's why if you observe while creating a lambda function it asks for a role to create and if you see the default role that it creates it has read and write policy on cloud watch. because lambda logs be default goes to cloudwatch. so cloudwatch needs access to write and read on cloud watch.
-
serverless info Display information about the service like endpoint, function name and all
-
API GateWay expects lambda function to return proper response JSON i.e it should contain response, body, status code. or else api gateway throws internal server error Malformed Lambda proxy response error.