Serverless Architecture with AWS Lambda

Serverless Architecture with AWS Lambda

It’s been an exciting past few years watching cloud services evolve into what they are current day. Starting from the early years, companies were only looking to utilize unused computing resources; to now, where you can run platforms such as SQL Server as a service (PaaS) or use Salesforce in the cloud (SaaS). Serverless architecture is the next new technology to be adopted and introduced by the major cloud providers.

Serverless architecture enables developers to write code without worrying about server configuration or platform. The server is essentially abstracted away from the developers, so they only have to focus on writing their code. Additionally, the user only pays for the time that their code is running, which eliminates paying for infrastructure that sits idle or underutilized. On the flip side, it allows code to easily scale to meet demands during high load periods.

Today, most major cloud providers offer a serverless architecture service. Amazon Web Services with Lambda, Microsoft with Azure Functions, and Google with Cloud Functions. For this post, we’re going to demonstrate how to use the AWS Lambda service. Each service supports writing functions using most major languages (C#, Java, Python, Node.js). We used .NET Core with C#, a cross platform version of .NET, for our implementation.

Our Sample Function

For our example, let’s say that you’re a user of Slack and love sending messages with custom emojis 💻💯. You have a great picture that you would like to turn into a Slack emoji, but the problem is that Slack only allows images with a max width or height of 128 pixels. Your image, however, is 800 x 600, and you don’t have a program to resize your image to meet Slack’s requirements. This is a perfect scenario to write a Lambda function that will resize the image for you. Here is how our process will flow:

  • A image is uploaded to AWS S3 storage
  • A built-in trigger notifies the function that a new image has been uploaded
  • The function executes, resizing the image, then persisting it back into a S3 bucket
  • The image can now be download or can be automatically added into Slack via an API request

Note: An AWS account is required to perform the following steps.

Install .NET Core

First, start off by installing .NET Core here, so we can write our function. We used a Mac, but this also works just fine on both Windows and Linux as well. As a part of the installation, the .NET Core CLI (Command Line Interface) is installed, which provisions and publishes our project from the command line.

Install AWS Lambda Templates & Create Project

Using the CLI, install the AWS .NET project templates, which contain projects templates for different types of Lambda events.

dotnet new -i Amazon.Lambda.Templates::*

Create a new .NET project using the Lambda Simple S3 Function template, so events are triggered when an image is added into S3 storage. Run the following command to create the project, where region is the AWS region where you would like your service to reside.

dotnet new lambda.S3 --name SlackEmoji.Lambda --iam-profile default --region region

This process creates a new SlackEmoji.Function directory. Navigate to the src sub-directory to see the source code generated.

Build Your Function

Use an IDE such as Visual Studio or VS Code and open the SlackEmoji.Lambda.csproj project. Examine all the files that are generated. The Function.cs file contains the FunctionHandler method, which is the entry point into the Lambda function.

public async Task FunctionHandler(S3Event evnt, ILambdaContext context)
  var s3Event = evnt.Records?[0].S3;
  context.Logger.LogLine($"Received S3 Event: 
                  {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}");
  // get image from S3
  var image = await S3Client.GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key);
  // resize image
  ImageResizer imgResizer = new ImageResizer();
  var outputImage = imgResizer.ResizeImage(image);
  // save image back to S3
  await S3Client.UploadObjectFromStreamAsync("slack-images", "myslackimage.png", 
                                                 outputImage, null);

You can see that an instance of the S3Event type is passed into the function, which contains the information about the S3 object that was updated. The full source code can be downloaded from GitHub –

Publish Your Function

Once you’ve written and tested your function, deploy the function with the following command via the .NET CLI. This will restore all external packages, build the solution, and package and send it to AWS for deployment.

dotnet restore
dotnet lambda deploy-function slack-emoji

Configure Function

Before invoking the Lambda function, input the required S3 container and account settings that we’re going to use within the AWS portal. As shown in the screenshot, select an S3 trigger event and then configure which account & container we’re going to use. All other AWS services that the function utilizes require explicit access granted.  Grant access to S3, in order to save the newly resized image. For monitoring function invocations, grant access to the CloudWatch service.

aws lambda portal

Configuring a Lambda function using the AWS portal

Run Function

To trigger the function, either upload an image to S3 manually, or use a test trigger to simulate an S3 event by configuring some JSON that represents an S3Event. Use The AWS portal  to trigger these events.

Now that you’ve seen a Lamda function in action, go out and build your own, so you can truly see the flexibility that Serverless Architecture can offer you. Contact us to discuss serverless architecture or cloud technologies.

Your email address will not be published. Required fields are marked *

Phone: 312-602-4000
222 W. Adams
Chicago, IL 60606
Show Buttons
Share On Facebook
Share On Twitter
Share on LinkedIn
Hide Buttons