AWS Lambda

You will now use the AWS Lambda console to retrieve the AWS Lambda endpoint for the static website. Before you begin make sure to prepare the GetAverageTemperature AWS Lambda function as explained in this Learning Path . Then proceed as follows:

  1. Go to the AWS Lambda console, and click the GetAverageTemperature Lambda function.
  2. In the Lambda function dashboard, click the Configuration tab and then the Function URL as shown below:

Image Alt Text:fig2

  1. Under the Function URL, click the Create Function URL button.
  2. In the window that appears select NONE, scroll down to Additional Settings, and check Configure cross-origin resource sharing (CORS).
  3. Click the Save button

The function URL will appear as follows:

Image Alt Text:fig3

Copy the link, and use it to replace the YOUR_API_GATEWAY_ENDPOINT_URL placeholder in the index.js file as follows:

    

        
        
            document.getElementById('fetchTemperatureButton').addEventListener('click', function() {
    fetch('YOUR_API_GATEWAY_ENDPOINT_URL') 
        .then(response => response.json())
        .then(data => {
            const temperature = data.average.toFixed(2);
            document.getElementById('temperatureDisplay').innerText = `Average Temperature: ${temperature} °C`;
        })
        .catch(error => {
            console.error('Error fetching temperature:', error);
            document.getElementById('temperatureDisplay').innerText = 'Error fetching temperature';
        });
});
        
    

Save the file, and open index.html. Then, click the Get temperature button and the average temperature will appear as shown below:

Image Alt Text:fig4

Back
Next