AWS Lambda
23.04.2024
10 min
What is it?
AWS Lambda is a server-side function that is provided in the cloud environment. You can write code (in some supported programming languages such as Node.js, Python, Java, and C# and Go) and upload it to Lambda. AWS then automatically manages the launch and scaling of this code based on current demand.
Why is this necessary?
AWS Lambda has many benefits for developers and businesses:
- Cost: You only pay for the actual execution time of your code, which can significantly reduce infrastructure costs.
- Scalability: AWS automatically scales your code based on load, allowing you to handle large volumes of work without the need for infrastructure management.
- Simplicity: You can quickly write and deploy new code without searching for servers or virtual machines.
The cost indicated on AWS:
How is it used?
Using AWS Lambda is quite simple. All you have to do is write your code, upload it to AWS Lambda, choose execution parameters (such as memory size and execution time), and set up triggers that call your function. After that, AWS is responsible for executing your code.
What results do we get?
Using AWS Lambda can lead to significant improvements in your projects:
- Cost Effectiveness: The cost of executing code can be significantly reduced by paying for actual execution time.
- Speed of deployment: You can quickly make changes to your code and deploy them without worrying about infrastructure.
- Scalability: You can handle large amounts of work by scaling your code based on your current needs.
AWS Lambda is a powerful tool that allows developers to quickly and efficiently deploy their code in the cloud environment, reducing costs and simplifying infrastructure management.
Practice
Now let’s figure out how to work with lambda in practice:
For this, we will create a new lambda (function):
We will leave all settings at default:
After creation, let’s try to test:
And as you can see, everything worked as it should:
If you need to use your code, you can download it in the corresponding section, which we will show next.
Let’s create a maven project in which we will write our implementation:
Let’s add maven dependencies:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
Also, you need to add a plugin for the build with dependencies, in our case we chose assembly:
Let’s write the implementation of the lambda in the code:
After that, we build our program using mvn package and get our jar file in the target directory.
We will upload this jar file to aws lambda, as shown in this screenshot:
After that, go to Runtime Settings and click the edit button:
Here we change the path to our method to the required one:
And also change the JSON:
{
“firstKey”: “firstValue”,
“secondKey”: “secondValue”,
“thirdKey”: “thirdValue”
}
Now let’s test and make sure that everything works:
As you can see in the logs there is all the information that we transmitted.