This BLOG would be dedicated in learning things around AWS Serverless: Lambda and DynamoDB.
This Page would be covering HOP and theoretical part to understand the concepts of AWS Serverless.
What is Serverless?
- Serverless is something new as name suggests it’s like without a server, where Developers with serverless services don’t manage the server anymore.
- So, it doesn’t mean that Developers don’t have the server anymore it’s just they don’t have to manage the server instead they just deploy the code or run their application code without provisioning the servers.
- Serverless was originally pioneered by AWS Lambda (Compute Service) but now it includes anything that is remotely managed.
- So, now we have serverless for database, messaging, storage, etc.
Serverless in AWS
AWS users would be able to get services like Lambda, DynamoDB, Cognito, API Gateway, Amazon S3, etc with serverless.
The User gets invoked their REST API through the API gateway and the API gateway would invoke Lambda functions, and Lambda functions would store and retrieve data from DynamoDB and for S3 the user would get a static content from S3 buckets delivered as CloudFront plus S3 (would be explaining it in brief later).
AWS Lambda
Why AWS Lambda?
If we need to manage our own compute resources, AWS has compute services like Amazon EC2 that lets to customize as per needs (operating system, security settings, Network) but what are the drawbacks?
- User is responsible for provisioning capacity, monitoring performance, and using availability zones for fault tolerance.
- We would be limited by the amount of memory and CPU we’re going to provision.
- They have to be continuously running, else we got to stop them and start them by ourselves to use it efficiently when it’s not used.
- For Scaling we need to use auto scaling groups.
Now comes AWS Lambda into picture.
What is AWS Lambda?
So, with Lambda these are virtual functions we don’t need to provision or manage the server. Yes, that’s correct what you read “No servers to manage “. We just provision the code and run the functions.
AWS Lambda is becoming one of the most widely used and popular services within AWS. It Revolutionizes the way applications are built, deployed, and scaled. Lambda is a compute service that lets developer to run code without managing servers.
- They run on-demand (When you don’t use Lambda, your Lambda function is not running, and you would be only getting billed when the function is running, and it will run when it gets invoked).
- The Scaling is Automated.
- The Pricing is super easy (The user pays for the number of requests lambda receives and for the compute time).
- Easy to get more resources per functions (up to 10GB of RAM and by this the Quality & Performance of CPU and Network is improved)
- Easy to monitor with AWS CloudWatch.
With Lambda you can run code for virtually in any type of application or backend service. So, all you need is to supply the code in one of the languages that Lambda supports.
Good part is AWS lambda is integrated with many programming languages.
node.js for JavaScript, Python, Java which is Java 8 compatible or Java 11, C# for .NET Core, Golang, C# for Powershell, Ruby, etc. So, Lambda has a lot of language support.
AWS Lambda Integrations:
API gateway is to create a Rest API and they will invoke out lambda functions.
DynamoDB will be used to create some triggers. So, whenever something happens in our database the lambda function will be triggered.
There are few more list of services that’s integrated with Lambda, but this blog would be more relevant to only Lambda and DynamoDB
HOP
Guess what? the Free tier allows 1,000,000 AWS Lambda requests and 400,000 GBs of Compute time.
So, get Ready for Hand’s on.
HOP -1 AWS serverless: Lambda
- Click on the console go the AWS lambda.
- Click on Create a function. There are four Options, for the hands- on we will going with “Use a blueprint” and in the search bar you could type any keyword you wanted it will give you a demo code of that keyword like a blueprint. You could type hello-world-python.
- Learning: Also, there’s a demo where you would be able to see the editor where AWS lambda supports different type of programming Languages. You can create lambda functions and configure them to respond to events from a variety of sources. So, if there’s more load from either Streaming data or placing a file in a S3 bucket or any event associate to a source, if there’s a repeated process of a file loading which in turns to more events are created and then more loads the lambda functions running will be scaling as well. If there’s more load, then automatically lambda will scale with the load.
- Click on the option below to configure the blueprint that says hello-world python
- Give the function name, default for execution role. Below you find the code (the blueprint) that would be imported to the function, you can modify the code as per your wish.
- Click on the create function option and then you will be able to run the function.
- Scroll down and in code source section click on your function on left side and editor you would be able to see the code.
- You can test the function by clicking on “Test” option on top and then select Create new test event.
- Give an Event name and leave the event template as default that was selected earlier and then click on create.
- Now click on the “Test” option again it will run the demo event and you would get a response in accordance with the code in response tab beside to your code function tab that was created by blueprint.
- Learning: From a developer perspective we deployed a code, and had it run we could see it runs seamlessly, if the code was big or memory consuming it will scale automatically and it’s fully serverless.
- In the same tab you would be able to see the duration it took for execution and Billed duration, Memory size used.
- Exploring other options
- Go to the Configuration, you can edit he settings over there and in Basic Settings you could provide the Memory that could be set from 128 MB to 10.24 GB, you could set the timeout as well.
- Click on the Monitor section and under metrics you could see details like Invocations, Duration and Error count and Success rate and so on of the Lambda function.
- In the same section under logs, you could find cloud watch that’s the integration of lambda, you can find a log stream if not refresh it before doing that you had to test the code at least once for the log to generate and in the log, you could see all the log values of the invocation of the function.
- You could now play around with the code, change the code, and try getting some errors and include exception logic in your code. When you change the code click on deploy and then test it, in result tab of the code section you would see some error and the comment of your exception logic. Try checking the same error is logged in cloud watch logs in Monitor section.
In Part 2 we would be seeing few more hands on Serverless Lambda with CloudWatch Events and S3 Events and also Detailed instructions on DynamoDB.