How to call a Lambda Function via API Gateway on Aws Services from the mobile client by using Flutter and Amplify?

Ugur Can
DFDS Development Center Istanbul
6 min readDec 13, 2021

Part I: Defining a lambda function written using Visual Studio to Aws

What is Flutter?

Flutter is a UI Toolkit announced by Google to the technology world in December 2018. Although it was initially aimed only at native mobile devices, over time it has become the target of desktop applications, web applications, and IIoTtechnologies as well as Ios and Android operating systems.

In my opinion, the plan in the first release phase of Flutter is to support this entire environment natively with a single application project. This idea is not valid for web applications at the moment, but I think that web applications will be supported as native-like other applications shortly.

To talk about the technical features of Flutter;

  • It works natively on all devices.
  • Flutter works like a single-page application like Angular and React.
  • Dart language is used as a programming language.
  • You can develop beautiful interfaces thanks to Material Ui and Cupertino.
  • Thanks to the Hot Reload feature and Widget support, you can develop very quickly.
  • You can use the same interface even on old mobile devices.

What is Amplify?

Amplify, on the other hand, is a library written by Amazon in 2020 to enable Aws services to be accessed and used by mobile clients and web applications.

Flutter, Javascript, Ios and Android applications on Aws.

It provides native access to Auth, Api, Lambda, DynamoDb and S3 services.

One of the personal goals that I set for myself for 2021. It was to write an application that accesses API Gateway, Lambda, DynamoDb, and S3 services on Aws using Flutter and Amplify. In the meantime, I had various difficulties in accessing API Gateway and I decided to write this article so that others would not experience these difficulties.

This article consists of three parts;

  1. Defining a lambda function written using Visual Studio to Aws.
  2. Access this lambda function from outside using the API Gateway.
  3. Access this API Gateway with Amplify in Flutter and run the lambda function we created earlier.

While writing this article, I assume that you are an intermediate or advanced programmer and know how to use Aws services. If you are a beginner, it would be beneficial for you to learn what the technologies I have written are and how they work, by looking at the websites or by searching on the internet. You can also find useful links that you can use at the end of the article.

Creating and deploying a lambda function on Aws using Visual Studio.

  • Run Visual Studio and click “Create a new Project”.
  • Type “Aws Lambda” in the Project Type search box and select “Aws Lambda Project (.Net Core — C#)” from the list.
  • Specify Project Name, Location, and Solution Name.
Create-an-aws-lambda-project-step
  • Select “Empty Function” from the “Select Blueprint” screen and click the “Finish” button.
  • Create the “Handlers” folder under the project and let’s start modifying the file named “Function.cs” to define the dependency injection.
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Kralizek.Lambda;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace HelloWorld
{
public class Function : RequestResponseFunction<APIGatewayProxyRequest, APIGatewayProxyResponse>
{
protected override void Configure(IConfigurationBuilder builder)
{
builder.AddEnvironmentVariables();
builder.SetBasePath(Directory.GetCurrentDirectory());
}
protected override void ConfigureServices(IServiceCollection services,
IExecutionEnvironment executionEnvironment)
{
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
RegisterHandler<Handlers.HelloWorld>(services);
}
}
}
  • Install the following NuGet packages in order.
    - Amazon.Lambda.ApiGatewayEvents
    - Kralizek.Lambda.Template
    - Microsoft.Extensions.DependencyInjection
    - Microsoft.Extensions.Configuration
    - Microsoft.Extensions.Configuration.EnvironmentVariables
    - Microsoft.Extensions.Configuration.Json
    - Amazon.Lambda.RuntimeSupport
    - AWSSDK.Core
    - Amazon.Lambda.Serialization.SystemTextJson
    - AWSSDK.Extensions.NETCore.Setup
    -Newtonsoft.Json
  • Create a new service under the “Handlers” folder and register this service in “Function.cs”.
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Kralizek.Lambda;
using System.Net;
using System.Threading.Tasks;
namespace HelloWorld.Handlers
{
public class HelloWorld : IRequestResponseHandler<APIGatewayProxyRequest, APIGatewayProxyResponse>
{
public async Task<APIGatewayProxyResponse> HandleAsync(APIGatewayProxyRequest input, ILambdaContext context)
{
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = "Hello World"
};
}
}
}
  • Now everything is ready for the first test run, press F5 to run your project.
  • If “dotnet-lambda-test-tool” don’t installed on your machine before, you may encounter an error like the one below.
  • To get rid of the error, run a command prompt with “Administrator” rights and run the following command.

dotnet tool install -g Amazon.Lambda.TestTool-3.1

  • If your operating system is windows or MacOs, you must define the certificates installed with the above command as “trusted” to your operating system. For this;

Run the command “dotnet dev-certs https –trust”.

  • If your project is built and the Lambda test tool is running smoothly. You need to publish your project in Aws. For this, click the right mouse button on the project and click on the “Publish to Aws Lambda…” option from the menu that opens.
  • When the “Upload to Aws Lambda” window opens;

- Select the AWS user in the “credentials” file in the “%USER%/.aws” folder. (For this, you must have previously created a user on the Aws IAM service, received “Access Key Id” and “Secret Access Key”.)

- Select the “Region” where you want to publish the lambda function.
- Give your function a name.
- Enter the Handler information of your Lambda function in Aws.
- Click the Next button.

  • If you have previously created a User Role within the Aws IAM service, select it. If you haven’t, create a role with the “AWSLambda_FullAccess” policy.
    - Select User Role created as Role Name.
    - Publish your function Aws by pressing the Upload button.
  • If your Lambda function has been successfully published in Aws. A test page will appear in front of you.
  • Select the “API Gateway AWS Proxy” option from the Example Requests list.
    - Change query-string or body values ​​you will post according to the type of function you want to create from the request template of the view.
    - Click the Invoke button.
    - In this way, you can understand whether your function is working or not.

In the next section, we will define API Gateway Endpoint on Aws and publish it on Aws.

Next Part: Access this lambda function from outside using the API Gateway

--

--