About AWS Lambda is a containerized self-managed server which runs a function. Invoiced for compute time only, not for idle time Scaled & shrunk automatically Code which is run on AWS Lambda is called a lambda function Create function Follow getting-started guide to create a hello world function Log in into AWS Go to lambda functions Create own function from scratch or use blueprint template Function name is hello-world-blueprint-func
console.log('Loading function');
exports.handler = async (event, context) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
console.log('value2 =', event.key2);
console.log('value3 =', event.key3);
return event.key1; // Echo back the first key value
// throw new Error('Something went wrong');
};
Invoke Lambda invokes the function when an event occurs. Create a test in console with the name test-hello-world
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Result "value1" Additional info is displayed.
Test Event Name
test-hello-world
Response
"value1"
Function Logs
START RequestId: 878679b1-87aa-4013-87a9-bf2ecdfa1a1c Version: $LATEST
2022-04-14T11:24:06.298Z undefined INFO Loading function
2022-04-14T11:24:06.304Z 878679b1-87aa-4013-87a9-bf2ecdfa1a1c INFO value1 = value1
2022-04-14T11:24:06.304Z 878679b1-87aa-4013-87a9-bf2ecdfa1a1c INFO value2 = value2
2022-04-14T11:24:06.318Z 878679b1-87aa-4013-87a9-bf2ecdfa1a1c INFO value3 = value3
END RequestId: 878679b1-87aa-4013-87a9-bf2ecdfa1a1c
REPORT RequestId: 878679b1-87aa-4013-87a9-bf2ecdfa1a1c Duration: 17.14 ms Billed Duration: 18 ms Memory Size: 128 MB Max Memory Used: 55 MB Init Duration: 188.00 ms
Request ID
878679b1-87aa-4013-87a9-bf2ecdfa1a1c
Invoke test multiple times and you can see logs & statistics in chart view. Terminology Function - a resource that you can run your code in Lambda. A function has code to process the events that is passed Trigger - something that invoke the function, AWS services or event source mapping Event - JSON document, which is converted into an object and passed into your function Execution environment - runtime environment for your Lambda function Runtime - language-specific environment that runs in an execution environment Instruction set architecture - arm64 or x86_64 processor Deployment package - the way you deploy your code, via .zip or container Layer - .zip file archive that can contain additional content, libraries, a custom runtime, data, or configuration files. Content is executed into the /opt folder. Concurrency - number of requests that your function is serving at any given time Function URLs - you can assign a dedicated HTTP endpoint to your Lambda function and it will be invoked through a web browser, curl, Postman, or any HTTP client. Lambda with url lambda is usually associated with api gateway api gateway has 30 sec timeout restriction sometimes it is not an option we can call lambda function directly assigning a URL to it lambda function itself has 15 min timeout restriction, which is a lot with this approach we convert a lambda function into an api Return a value from lambda function with url API to return smth from an api we just need to return a value from the exports.handler await is not needed
exports.handler = async (event, context) => {
return someFunc()
}
Hints Always return smth from the lambda function, otherwise it may be unstable acc. to Jari