Programming
Getting started with Aws Lambda
Run and deploy your first AWS Lambda .Net Core 3.1 application
This article will give a brief explanation of Aws Lambda. Let’s get started.
What is Aws Lambda about?
Aws Lambda is an event-driven, serverless computing service provided by Amazon as a part of Aws. Lambda’s purpose is to simplify building small, on-demand applications that are responsive to events.
Node.js, Python, Java, Go, Ruby, and C# (through .NET Core) are all officially supported. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability.
You pay only for the compute time you consume.
Why do I need Aws Lambda?
- Do you struggle to manage your servers? Databases, web servers, scheduled tasks, batch files, logs, security, authentication, etc… Things get easier with Aws Lambda. Just write your code and upload it to Lambda.
- Do you want a scalable application? You can scale precisely with the size of your workload.
- With Aws Lambda you reach high performance. And you pay only for the compute time you consume.
What are the use cases?
Some of the use cases are listed below:
- Data processing (Perform data validation, filtering, sorting, or other transformations)
- Real-Time File Or Stream Processing
- Message Queue Operations (ActiveMQ, RabbitMQ, etc…)
- AWS Library Operations (Open/Close/Get EC2 Instances, S3 upload, etc…)
What are the limitations?
Unfortunately, resources are not unlimited. There are some limitations to Aws Lambda. There are some limitations listed below:
- Deployment package size: 50 MB (zipped) / 250 MB (unzipped)
- Function timeout: 900 seconds (15 minutes)
- Concurrent executions: 1,000
- Function memory allocation: 128 MB to 3,008 MB
How to use it?
- Start by downloading AWS Toolkit for Visual Studio.
- Create an ‘AWS Lambda Project’.
- Select ‘Empty Function’ from the Blueprints pop-up and then click finish.
- After creating your project, you can see a JSON, MD, and cs file. The JSON file is where you specify your Aws Lambda project configurations. The MD file is just for basic documentation. The cs file is where you will write your code. By default, it comes with the ‘FunctionHandler’ method. This method is like the main function we know from console applications.
- Let’s write a Hello World application.
- This is a simple function which switches according to your string input.
- Click the ‘Mock Lambda Test Tool’.
- Then you can see a web page.
- Config File, Function, AWS Credential Profile, and AWS Region are fields where you manage your application settings.
- From the Example Requests dropdown, you can choose from example blueprints.
- In the Function Input text area, you can give parameters for your FunctionHandler method in JSON format.
- Let’s send a request!
- Click ‘Execute Function’.
- That’s it! You have an Aws Lambda project now!
If you want to skip the installation process you can check the application from my GitHub account.
What else can we do?
- You can change ‘string input’ on FunctionHandler. For example, you can use a model named Human. Human has a name, age, sex, etc… Then you can send your input as a JSON.
- You can add logging (serilog, nlog, etc…).
- For example, you can use ‘Lambda Logging’.
LambdaLogger.Log(“Hello World”);
- You can connect to any database and do crud operations.
- You can use elastic search and Aws libraries.
- You can use ‘CloudWatch’ to create schedules and run your Aws Lambda functions in your specified intervals (I will mention CloudWatch in another article).
- You can create an appsettings.json file for more configuration.
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
How to deploy?
It is simple and easy. Follow these steps:
- Right-click to Aws Lambda Project and select ‘Publish to AWS Lambda…’
- Add a new Aws profile from the ‘Profile’ section.
- Select a region.
- Other settings should remain as default. Click the ‘Next’ button.
- On the following screen, you can edit the memory and timeout settings.
- Select a role.
- Other settings should remain as default. Click the ‘Upload’ button.
- On the following screen, you can test your deployed Aws Lambda project.
- You can also check your deployed Aws Lambda project on the ‘Aws Website’ by selecting ‘Lambda’ under the ‘Services’ section.
The strangest runtime error I ever encountered
If you use the ‘System.Data.SqlClient’ package, you will most likely get the following error:
The reason you got this error is that there exists an unresolved dependency on Aws Lambda .Net Core 3.1. If you add the code below to your project properties (double click to your Aws Lambda project) under the ‘PropertyGroup’ tag, the problem will be solved.
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
Conclusion
We gain basic knowledge about Aws Lambda, created a ‘Hello World’ project, deployed our project to Aws. Also, we learned what we can do with Aws Lambda in advance.
Best regards.
For more information
- About limitations
- About blueprints
- About the strange error
- About Aws Lambda
Special thanks to Ferhat Özkan