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:
GetAverageTemperature
Lambda function.The function URL will appear as follows:
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: