Serverless
Learn Azure Functions with Seasons of Serverless
November 30, 2020
Hello and happy December! This month, I am learning more about Serverless with the Seasons of Serverless challenge.
In this article, I'll cover my solution to Challenge 1.
Table of Contents
Prerequisites
1. Challenge outline
2. My solution
3. Check the results
4. More resources
Prerequisites
- Visual Studio Code
- Azure Functions Extension Installed
- An Azure account
1. Challenge outline
In this challenge, we will be entering a turkey's weight to determine it's brine recipe, brine time, and roast time.
Input
For this challenge, our input will be a turkey's weight.
Output
There are three distinct parts to our output:
1. The brine recipe
- Salt (in cups) = 0.05 * lbs of turkey
- Water (gallons) = 0.66 * lbs of turkey
- Brown sugar (cups) = 0.13 * lbs of turkey
- Shallots = 0.2 * lbs of turkey
- Cloves of garlic = 0.4 * lbs of turkey
- Whole peppercorns (tablespoons) = 0.13 * lbs of turkey
- Dried juniper berries (tablespoons) = 0.13 * lbs of turkey
- Fresh rosemary (tablespoons) = 0.13 * lbs of turkey
- Thyme (tablespoons) = 0.06 * lbs of turkey
2. The brining time (in hours)
- Brine time (in hours) = 2.4 * lbs of turkey
3. The rost time (in minutes)
- Roast time (in minutes) = 15 * lbs of turkey
Using the following information, we'll create an Azure Function to develop a solution.
2. My solution
When creating my Azure Function, I selected the HTTP trigger, named the function turkeyTime, snd gave it an Anonymous authorization level. I used JavaScript for my solution but there are lots of different languages you can use. I edited one file to include the following code: index.js.
// turkeyTime/index.js
module.exports = async function (context, req) {
context.log("JavaScript HTTP trigger function processed a request.")
const weight = req.query.weight || (req.body && req.body.weight)
const responseMessage = weight
? `Your turkey weighs ${weight} lbs.
Brine Instructions:
Salt: ${(0.05 * weight).toFixed(2)} cups
Water: ${(0.66 * weight).toFixed(2)} gallons
Brown Sugar: ${(0.13 * weight).toFixed(2)} cups
Shallots: ${(0.2 * weight).toFixed(2)}
Cloves of garlic: ${(0.4 * weight).toFixed(2)}
Whole peppercorns: ${(0.13 * weight).toFixed(2)} tablespoons
Dried juniper berries: ${(0.13 * weight).toFixed(2)} tablespoons
Fresh rosemary: ${(0.13 * weight).toFixed(2)} tablespoons
Thyme: ${(0.06 * weight).toFixed(2)} tablespoons
Brine Time: ${(2.4 * weight).toFixed(2)} hours
Roast Time: ${(15 * weight).toFixed(2)} minutes`
: "Please provide a weight for your turkey!"
context.res = {
// status: 200, /* Defaults to 200 */
body: responseMessage,
}
}
A note on my solution
- I used
.toFixed(2)
to ensure solutions did not have infinite decimal points. That would have not been the best user experience!
3. Check the results
After deploying my code to Azure's Function App, this is what I got:
Input
Output
4. More resources
- Join this week's Seasons of Serverless challenge
- Learn more about Azure Functions on Microsoft Learn