How to Deploy ASP.NET Core Spreadsheet Web API Service to AWS Lambda?
This knowledge base article explains how to deploy a spreadsheet Web API service to AWS Lambda in the ASP.NET Core Spreadsheet. By following the steps below, you can achieve this.
Prerequisites:
- Web API project.
- AWS Account.
- .NET Core SDK.
Steps to deploy:
• First, add the ‘Amazon.Lambda.AspNetCoreServer’ NuGet package to the Web API. This package allows your Web API to run on AWS Lambda.
• Then, add a new class by right-clicking the project, selecting ‘Add’ > ‘Class…’, and name it ‘LambdaFunction’. Make this class extend from ‘Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction’. The code for this is provided below for your reference.
[LambdaFunction.cs]
using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace WebAPI
{
public class LambdaFunction : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
protected override void Init(IWebHostBuilder builder)
{
builder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseLambdaServer();
}
}
}
• Next, add the ‘Amazon.Lambda.Tools’ NuGet package to enable the deployment features. You can find the code for this below.
[WebAPI.csproj]
<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.2.0"/>
…………
…………
</ItemGroup>
• After that, add a serverless.template file to the project by right-clicking the project and selecting ‘Add’ > ‘AWS Serverless Template’. This file is used to define the Amazon API Gateway. Then, update the handler field in the template as shown in the provided code. The format for the handler field is
[serverless.template]
{
…………
…………
"Resources" : {
"DefaultFunction" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "WebAPI::WebAPI.LambdaFunction::FunctionHandlerAsync",
"Runtime": "dotnet6",
…………
…………
}
}
}
}
• Then, add the aws-lambda-tools-defaults.json file by right-clicking the project and selecting ‘Add’ > ‘New Item…’ > ‘JSON file’. Name it aws-lambda-tools-defaults.json. In this file, set the necessary settings as shown in the code snippets below to deploy the AWS Lambda function.
[aws-lambda-tools-defaults.json]
{
…………
…………
"profile": "default",
"region": "us-east-1",
"configuration": "Release",
"framework": "dotnet6",
"s3-prefix": "CoreAPI/",
"template": "serverless.template",
"template-parameters": "",
"s3-bucket": "********", // Your AWS bucket name.
"stack-name": "CoreAPIStack"
}
• Ensure that you have the AWS Toolkit for Visual Studio installed. You can download it from the Visual Studio Marketplace. After installing the toolkit, open the ‘AWS Explorer’ by going to View > AWS Explorer in Visual Studio. Use AWS Explorer to create an account profile, setting the profile name to ‘default’. When you first use AWS Explorer, you’ll be prompted to sign in with your AWS credentials.
• Next, right-click on the project and select ‘Publish to AWS Lambda’ to deploy it. In the Publish AWS Serverless Application dialog, click ‘Publish’ to complete the deployment.
• Once the publishing status changes from ‘CREATE_IN_PROGRESS’ to ‘CREATE_COMPLETE’ copy the AWS Serverless URL. Use this URL in the client-side sample as shown in the code snippets below.
'https://xxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/open'
'https://xxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/save'
You can find the Web API for Open/Save functionality at the location provided below.
Service sample Location: https://github.com/SyncfusionExamples/EJ2-Spreadsheet-WebServices/
For further details on opening and saving an Excel file using a hosted web service in AWS Lambda, please refer to the following links.
[https://ej2.syncfusion.com/aspnetcore/documentation/spreadsheet/open-save#open-an-excel-file-using-a-hosted-web-service-in-aws-lambda]
(https://ej2.syncfusion.com/aspnetcore/documentation/spreadsheet/open-save#open-an-excel-file-using-a-hosted-web-service-in-aws-lambda)
https://ej2.syncfusion.com/aspnetcore/documentation/spreadsheet/open-save#save-an-excel-file-using-a-hosted-web-service-in-aws-lambda
For more details on deploying an existing Web API to AWS Lambda, please refer to the link provided below.
https://aws.amazon.com/blogs/developer/deploy-an-existing-asp-net-core-web-api-to-aws-lambda/
Conclusion
I hope you enjoyed learning how to deploy ASP.NET Core Spreadsheet web API service to AWS Lambda.
You can refer to our ASP.NET Core Spreadsheet feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Spreadsheet example to understand how to present and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!