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
5 min readDec 13, 2021

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

In the previous part of my article, we created a Lambda function project using Visual Studio and published it on Aws services. In this section, we will create a new end-point using API Gateway.

  • Log into the AWS website.
  • Go to Aws Lambda service by typing “lambda” in the top search box and check the function we have created before.
  • If you want, it is possible to test your function from Visual Studio as you did before. For this;

- Click on the name above the function list.
- Click on the Test Tab.
- Select the “API Gateway AWS Proxy” option from the template list.
- Click on the test button,
- You can see your test results in the Execution Result section.

  • If your test result is successful, type “API Gateway” in the top search box again and go to the service.
  • Click the “Create API” button and select “REST API” on the “Choose an API Type” screen.
  • Select “REST” from “Choose the protocol” and “New API” from “Create new API”.
  • Give your API a name and click the “Create API” button.
  • After your API is created, go to its details and click “Resources”. This is where your API’s URL paths and HTTP methods are defined.
  • Click the “Actions” button and select “Create Resource”.
  • Enter your resource name.
    - Enter the name of the end-point you want to create.
    - Tick the box if you want your end-point to use the CORS feature.
    - Create your resource.
  • You have now created your “end-point”, but you have not determined with which HTTP methods you will access it. If you do not define any HTTP method, you cannot access this endpoint in any way.
  • Click the “Actions” button again and select “Create Method”. The method list will appear below your “end-point”. Choose the method that suits you best here.
  • The important part here is that you choose the method that is suitable for your code, which is also defined in your lambda function. If you write a lambda function for the HTTP Get method and choose the HTTP Post method, your lambda function may not work.
  • After choosing your HTTP method, you need to enter the necessary definitions from the detail window that opens on the right.
    - Select “Lambda Function” as “Integration Type”.
    - Check the “Use Lambda Proxy Integration” box.
    - From the “Lambda Function” list, select the name of the lambda function you created in the previous article.
    - Click the “Save” button.
  • A window will appear asking for authorization to access the lambda function you have created from the “API Gateway”. You must give the authorization by clicking the “Ok” button.
  • After making all these definitions, click the “Actions” button again and click the “Deploy API” option from the menu that opens. This option will enable you to activate the changes you have made and the new definitions. You make all kinds of changes, if you do not deploy these changes, you will not be able to reach them.
  • When you click on the “Deploy API” option, you will be greeted with a concept called the stage. Stage concept may change according to your project structure. If you have environments such as Test, UAT, and Production, it allows you to deploy the changes only for that environment.
  • Select your Stage or create a new one and click the deploy button.
  • To access the “end-point” you have created and the deployment you have made; Click “Stages” from the left menu. When you click on any of the listed stages, you will see a link named “Invoke URL” in the window that opens on the right. Here is the link you will use to access the “end-point” you have created. Please copy and save this value for use later.
  • The “API Gateway service” allows you to test the link you have created from outside as well as test it from outside with “Postman” or similar applications. For this;
  • Click on the HTTP method that you have created from the “Resource” you have created.

- Click on the test button in the window on the right.
- Copy the JSON object in the “API Gateway AWS Proxy” option that you used while testing your lambda function to the “Request Body” section.
- Click the “Test” button.

  • On the right, you can see the result returned from the lambda function you have created from your “end-point”.
  • So far we have created and tested our “end-point”. However, there is one last thing you need to do if we want to restrict access to this URL for other people from the outside. It is to create an “API Key”. For this;
  • Click on the “API Keys” option from the left menu.
    - Click the “Actions” button and select the “Create API Key” option.
    - In the window that opens on the right, enter the name “API Key”.
    - Select the “API KEY” type.
    - Click the “Save” button.
  • When you click on the “API Key” you have created, click the “Show” button in the detail window that opens on the right. Copy the visible value and save it for later access.

That’s all for now what we will do for this section, in the next section we will try to access this end-point using Amplify and Flutter.

Next Part : Accessing Api Gateway using Flutter and Amplify

--

--