Create a serverless microservice in aws to retrieve weather information for any location in the world based on pin code or zip

Pranav T P
5 min readJun 21, 2022

AWS Lambda function creation and configuration

Sign in to the AWS console and select the Lambda from the under the Compute services. Then, follow these instructions to create a Lambda function:

  1. Click on the Create a function button.
  2. Select the Blueprint option, and choose the microservice-http-endpoint-python3 blueprint.

3. Click on the Configure option, and then specify the following values. The following values will be used to configure the Amazon API Gateway endpoint for this microservice:

1. Set API Name to Serverless Weather Service.

2. Set Deployment Stage to prod.

4. In Create function, specify the following values:

1. Basic information section:

1. Enter a function name (for example, WeatherServiceFunction).

2. Select Python 3.6 from the Runtime list.

3. For Description, use cloud-native — Serverless Weather Service.

2. Lambda function code:

1. In the Code Entry Type field, select the Upload a .zip file option. Get the sample Python code ZIP file, Cloudnative-weather-service.zip, from the https:/​/​github.​com/​PacktPublishing/​Cloud-​Native- Architectures GitHub location and upload it.

3. Lambda function handler and role:

1. For the handler, specify the value as lambda_function.lambda_handler.

2. For the Role, select the option Create new role from template(s).

3. For the Role Name, specify WeatherServiceLambdaRole.

4. In the Policy Templates option, select the dropdown with the value Simple Microservice Permissions.

4. Tags:

1. Specify Key as Name and Value as Serverless Weather Microservice.

2. Advanced settings:
Set the Memory selection to 128 MB and Set Timeout value as 0 minutes and 30 seconds and Leave the rest of the settings with the default value.

5. After the preceding configurations are done, review the settings on the final screen and hit Create function. In a matter of a few minutes, your Lambda function will be created and you should be able to view it on the Functions list, as follows:

Configuring the Amazon API Gateway

After performing the preceding steps, go to the Amazon API Gateway service in the AWS Console and you should see an API like the following already created:

Now, let’s configure this API to match our needs:

  1. Click on the API’s name, Serverless Weather Service, to drill down into its configuration.
  2. Select the Resources option on the left-hand side and then on the right-hand side, expand the resources to show /WeatherServiceFunction/ANY and you should see its details as follows:

3. Click on Method Request and expand the URL Query String Parameters option. Under that, use the Add query string button to add the following parameters:

Set the name to zip and check the Required checkbox.

Add another parameter, set the name to appid, and check the Required checkbox.

4. In the preceding pane, select the Request Validator option and set its value to Validate body, query string parameters, and headers.

5. After completing the preceding settings, your Method Execution screen should look as follows:

Setting up a Weather Service Account

In order to get the latest weather information, our microservice will invoke APIs from a third-party weather service, OpenWeatherMap (https:/​/​openweathermap.​org). So, go to the URL and create a new, free account. Once you have created an account, go to your account settings and note down the API key from the following option:

Testing the service

Now that we have all the necessary pieces set up, let’s go back to the Amazon API Console and try testing the service. For that, follow these steps:

  1. Open the /WeatherServiceFunction/ANY file under the Resources option, and hit the Test option.
  2. Under the method, select Get as the option.
  3. For the query string, we need two inputs: the ZIP code of the city for which you want to query the current weather and the OpenWeatherMap’s API key. To do this, create a query string such as zip=<<zip-value>>&appid=<<app-id>>. In this, replace the italicized values with actual inputs and form a string such as zip=10001&appid=abcdefgh9876543qwerty.
  4. Hit the Test button at the bottom of the screen to invoke the API and the Lambda function.
  5. If the test is successful, you will see a response with Status: 200, and in the response body, you will get the weather information in a JSON format, as follows:

Deploying the API

After successfully testing the API, we have to deploy the API as well so that it can be active in the prod environment. For that, select the Deploy API option from the Actions drop- down menu within the API configuration screen, as follows:

Once the API is deployed, you can go to the Stages option on the left-hand side, select the prod stage, and under that, GET the HTTP method, as follows:

To make the testing process of the deployed API easier, you can also use the sample HTML page, which is available at the https:/​/​github.​com/​PacktPublishing/​Cloud-​Native- Architectures GitHub link, as follows:

1. Download the HTML page, edit the source to update your API endpoint on line 28, and save it:

<form method=”get” action=”https://<<updated- endpoint>>>/Prod/weatherservice”>

2. Open the web page in a browser, supply the required field values, and hit the Get Weather button to retrieve the weather for that location:

--

--

Pranav T P

I'm a Pranav T P. pursuing my Master (Mtech) at PES University, Banglore in a stream of Cloud Computing