34,79 €
Microsoft provides a solution for easily running small segments of code in the cloud with Azure Functions. The second edition of Azure Serverless Computing Cookbook starts with intermediate-level recipes on serverless computing along with some use cases demonstrating the benefits and key features of Azure Functions. You’ll explore the core aspects of Azure Functions, such as the services it provides, how you can develop and write Azure Functions, and how to monitor and troubleshoot them.
As you make your way through the chapters, you’ll get practical recipes on integrating DevOps with Azure Functions, and providing continuous integration and continuous deployment with Azure DevOps. This book also provides hands-on, step-by-step tutorials based on real-world serverless use cases to guide you through configuring and setting up your serverless environments with ease. You will also learn how to build solutions for complex, real-world, workflow-based scenarios quickly and with minimal code using Durable Functions. In the concluding chapters, you will ensure enterprise-level security within your serverless environment. The most common tips and tricks that you need to be aware of when working with Azure Functions on production environments will also be covered in this book.
By the end of this book, you will have all the skills required for working with serverless code architecture, providing continuous delivery to your users.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 277
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Vijin BorichaAcquisition Editor: Shrilekha InaniContent Development Editor: Nithin George VargheseTechnical Editor: Komal KarneCopy Editor: Safis EditingProject Coordinator: Drashti PanchalProofreader: Safis EditingIndexer: Mariammal ChettiyarProduction Designer: Aparna Bhagat
First published: August 2017 Second edition: November 2018
Production reference: 2171019
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78961-526-5
www.packt.com
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Praveen Kumar Sreeram works as an Azure architect, trainer in a leading MNC. He has over 14 years of experience in the field of development, analysis, design, and delivery of applications, including custom web development using ASP.NET and MVC to building mobile apps using Xamarin for domains such as insurance, telecom, and wireless expense management. He has been recognized twice as the MVP by one of the leading social community websites, CSharpCorner. He is an avid blogger who writes about his learning at his personal blog, called Praveen Kumar Sreeram. His current focus is on analyzing business problems and providing technical solutions for various projects related to Microsoft Azure and .NET Core. His twitter ID is @PrawinSreeram.
Kasam Shaikh, Microsoft Azure enthusiast, is a seasoned professional with a "Could be" attitude, with 10 years of industry experience working as a cloud architect with one of the leading IT companies in Mumbai, India. He is a certified Azure architect, and has been recognized as an MVP by a leading online community, and is also a global AI speaker. He has authored books on Azure Cognitive, Azure Bots, and Microsoft Bot Frameworks. He leads the Azure India (AZINDIA) community, the fastest growing online community for learning Azure. He is also a founder of the Dear Azure website.
Michael Sync (Soe Htike) is a senior engineer working at Readify in Australia. He was an MVP (Microsoft Valuable Professional) for 7 years. He participated as a speaker, mentor, and helper at several community events. He is also an author and a tech book reviewer. He has published two books with Manning and has reviewed several books for a number of publishers. He has been working in the software industry for more than 16 years.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Azure Serverless Computing Cookbook Second Edition
Dedication
About Packt
Why subscribe?
Contributors
About the author
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Sections
Getting ready
How to do it...
How it works...
There's more...
See also
Get in touch
Reviews
Developing Cloud Applications Using Function Triggers and Bindings
Introduction
Building a backend Web API using HTTP triggers
Getting ready
How to do it...
How it works...
See also
Persisting employee details using Azure Storage table output bindings
Getting ready
How to do it...
How it works...
Understanding storage connection
What is the Azure Table storage service?
Partition key and row key
There's more...
Saving the profile images to Queues using Queue output bindings
Getting ready
How to do it...
How it works...
Storing the image in Azure Blob Storage
Getting ready
How to do it...
How it works...
There's more...
Working with Notifications Using the SendGrid and Twilio Services
Introduction
Sending an email notification to the administrator of a website using the SendGrid service
Getting ready
Creating a SendGrid account
Generating an API key from the SendGrid portal
Configuring the SendGrid API key with the Azure Function app
How to do it...
Create Storage Queue binding to the HTTP Trigger
Creating Queue Trigger to process the message of the HTTP Trigger
Creating SendGrid output binding to the Queue Trigger
How it works...
There's more
Sending an email notification dynamically to the end user
Getting ready
How to do it...
Accepting the new email parameter in the RegisterUser function
Retrieving the UserProfile information in the SendNotifications trigger
How it works...
There's more...
Implementing email logging in Azure Blob Storage
How to do it...
How it works...
Modifying the email content to include an attachment
Getting ready
How to do it...
Customizing the log file name using IBinder interface
Adding an attachment to the email
Sending an SMS notification to the end user using the Twilio service
Getting ready
How to do it...
How it works...
Seamless Integration of Azure Functions with Azure Services
Introduction
Using Cognitive Services to locate faces in images
Getting ready
Creating a new Computer Vision API account
Configuring application settings
How to do it...
How it works...
There's more...
Azure SQL Database interactions using Azure Functions
Getting ready
How to do it...
How it works...
Monitoring tweets using Logic Apps and notifying users when a popular user tweets
Getting ready
How to do it...
Creating a new Logic App
Designing the Logic App with Twitter and Gmail connectors
Testing the Logic App functionality
How it works...
Integrating Logic Apps with serverless functions
Getting ready
How to do it...
There's more...
See also
Auditing Cosmos DB data using change feed triggers
Getting ready
Creating a new Cosmos DB account
Creating a new Cosmos DB collection
How to do it...
How it works...
There's more...
Understanding the Integrated Developer Experience of Visual Studio Tools
Introduction
Creating a function app using Visual Studio 2017
Getting ready
How to do it...
How it works...
There's more...
Debugging C# Azure Functions on a local staged environment using Visual Studio 2017
Getting ready
How to do it...
How it works...
There's more...
Connecting to the Azure Storage cloud from the local Visual Studio environment
Getting ready
How to do it...
How it works...
There's more...
Deploying the Azure Function app to Azure Cloud using Visual Studio
How to do it...
There's more...
Debugging a live C# Azure Function, hosted on the Microsoft Azure Cloud environment, using Visual Studio
Getting ready
How to do it...
Deploying Azure Functions in a container
Getting ready
Creating an ACR
How to do it...
Creating a Docker image for the function app
Pushing the Docker image to the ACR
Creating a new function app with Docker
How it works...
Exploring Testing Tools for the Validation of Azure Functions
Introduction
Testing Azure Functions
Getting ready
How to do it...
Testing HTTP triggers using Postman
Testing a Blob trigger using Microsoft Storage Explorer
Testing the Queue trigger using the Azure Management portal
There's more...
Testing an Azure Function on a staged environment using deployment slots
How to do it...
There's more...
Load testing Azure Functions using Azure DevOps
Getting ready
How to do it...
There's more...
See also
Creating and testing Azure Functions locally using the Azure CLI tools
Getting ready
How to do it...
Testing and validating Azure Function responsiveness using Application Insights
Getting ready
How to do it...
How it works...
There's more...
Developing unit tests for Azure Functions with HTTP triggers
Getting ready
How to do it...
Monitoring and Troubleshooting Azure Serverless Services
Introduction
Troubleshooting your Azure Functions
How to do it...
Viewing real-time application logs
Diagnosing the entire function app
There's more...
Integrating Azure Functions with Application Insights
Getting ready
How to do it...
How it works...
There's more...
Monitoring your Azure Functions
How to do it...
How it works...
Pushing custom telemetry details to Application Insights Analytics
Getting ready
How to do it...
Creating an Application Insights function
Configuring access keys
Integrating and testing an Application Insights query
Configuring the custom derived metric report
How it works...
Sending application telemetry details via email
Getting ready
How to do it...
How it works...
There's more...
See also
Integrating real-time Application Insights monitoring data with Power BI using Azure Functions
Getting ready
How to do it...
Configuring Power BI with a dashboard, a dataset, and the push URI
Creating an Azure Application Insights real-time Power BI – C# function
How it works...
There's more...
Developing Reliable Serverless Applications Using Durable Functions
Introduction
Configuring Durable Functions in the Azure Management portal
Getting ready
How to do it...
There's more...
Creating a Durable Function hello world app
Getting ready
How to do it...
Creating an HttpStart function in the Orchestrator client
Creating the Orchestrator function
Creating an activity function
How it works...
There's more...
Testing and troubleshooting Durable Functions
Getting ready
How to do it...
Implementing multithreaded reliable applications using Durable Functions
Getting ready
How to do it...
Creating the Orchestrator function
Creating a GetAllCustomers activity function
Creating a CreateBARCodeImagesPerCustomer activity function
How it works...
There's more...
Bulk Import of Data Using Azure Durable Functions and Cosmos DB
Introduction
Business problem
Durable serverless way of implementing an Excel import
Uploading employee data into Blob Storage
Getting ready
How to do it...
How it works...
There's more...
Creating a Blob trigger
Getting ready
How to do it...
There's more...
Creating the Durable Orchestrator and triggering it for each Excel import
How to do it...
How it works...
There's more...
Reading Excel data using activity functions
Getting ready
How to do it...
Reading data from Blob Storage
Reading Excel data from the stream
Creating the activity function
There's more...
Auto-scaling Cosmos DB throughput
Getting ready
How to do it...
There's more...
Bulk inserting data into Cosmos DB
How to do it...
There's more...
Implementing Best Practices for Azure Functions
Adding multiple messages to a queue using the IAsyncCollector function
Getting ready
How to do it...
How it works...
There's more...
Implementing defensive applications using Azure Functions and queue triggers
Getting ready
How to do it...
CreateQueueMessage – C# console application
Developing the Azure Function – queue trigger
Running tests using the console application
How it works...
There's more...
Handling massive ingress using Event Hubs for IoT and other similar scenarios
Getting ready
How to do it...
Creating an Azure Function event hub trigger
Developing a console application that simulates IoT data
Avoiding cold starts by warming the app at regular intervals
Getting ready
How to do it...
Creating an HTTP trigger
Creating a timer trigger
There's more...
See also
Enabling authorization for function apps
Getting ready
How to do it...
How it works...
There's more...
Controlling access to Azure Functions using function keys
How to do it...
Configuring the function key for each application
Configuring one host key for all the functions in a single function app
There's more...
Securing Azure Functions using Azure Active Directory
Getting ready
How to do it...
Configuring Azure AD to the function app
Registering the client app in Azure AD
Granting the client app access to the backend app
Testing the authentication functionality using a JWT token
Configuring the throttling of Azure Functions using API Management
Getting ready
How to do it...
Integrating Azure Functions with API Management
Configuring request throttling using inbound policies
Testing the rate limit inbound policy configuration
How it works...
Securely accessing SQL Database from Azure Functions using Managed Service Identity
Getting ready
How to do it...
Creating a function app using Visual Studio 2017 with V1 runtime
Creating a Logical SQL Server and a SQL Database
Enabling the managed service identity
Retrieving Managed Service Identity information
Allowing SQL Server access to the new Managed Identity Service
Executing the HTTP trigger and testing it
There's more...
See also
Shared code across Azure Functions using class libraries
How to do it...
How it works...
There's more...
Using strongly typed classes in Azure Functions
Getting ready
How to do it...
How it works...
There's more...
Configuring of Serverless Applications in the Production Environment
Introduction
Deploying Azure Functions using Run From Package
Getting ready
How to do it...
How it works...
There's more...
Deploying Azure Function using ARM templates
Getting ready
How to do it...
There's more...
Configuring custom domains to Azure Functions
Getting ready
How to do it...
Configuring a function app with an existing domain
Techniques to access Application Settings
Getting ready
How to do it...
Accessing Application Settings and connection strings in Azure Function code
Application setting – binding expressions
Creating and generating open API specifications using Swagger
Getting ready
How to do it...
Breaking down large APIs into small subsets of APIs using proxies
Getting ready
How to do it...
Creating microservices
Creating the gateway proxies
Testing proxy URLs
There's more...
See also
Moving configuration items from one environment to another using resources
Getting ready
How to do it...
Implementing and Deploying Continuous Integration Using Azure DevOps
Introduction
Prerequisites
Continuous integration – creating a build definition
Getting ready
How to do it...
How it works...
There's more...
Continuous integration – queuing a build and triggering it manually
Getting ready
How to do it...
Configuring and triggering an automated build
How to do it...
How it works...
There's more...
Continuous integration – executing unit test cases in the pipeline
How to do it...
There's more...
Creating a release definition
Getting ready
How to do it...
How it works...
There's more...
See also
Triggering the release automatically
Getting ready
How to do it...
How it works...
There's more...
Other Books You May Enjoy
Leave a review - let other readers know what you think
Microsoft provides a solution to easily run small segments of code in the cloud with Azure Functions. Azure Functions provides solutions for processing data, integrating systems, and building simple APIs and microservices.
The book starts with intermediate-level recipes on serverless computing, along with some use cases on the benefits and key features of Azure Functions. Then, we'll deep dive into the core aspects of Azure Functions, such as the services it provides, how you can develop and write Azure Functions, and how to monitor and troubleshoot Azure Functions.
Moving on, you'll get practical recipes on integrating DevOps with Azure Functions, and providing continuous deployment with Azure DevOps (formerly Visual Studio Team Services). The book also provides hands-on steps and tutorials based on real-world serverless use cases to guide you through configuring and setting up your serverless environments with ease. Finally, you'll see how to manage Azure Functions, providing enterprise-level security and compliance to your serverless code architecture.
You will also learn how to quickly build applications that are reliable and durable using Durable Functions, with an example of a very common real-time use case.
By the end of this book, you will have all the skills required to work with serverless code architectures, providing continuous delivery to your users.
If you are a cloud administrator, architect, or developer who wants to build scalable systems and deploy serverless applications with Azure Functions, then the Azure Serverless Computing Cookbook is for you.
Chapter 1, Developing Cloud Applications Using Function Triggers and Bindings, goes through how the Azure Functions runtime provides templates that can be used to quickly integrate different Azure services for your application needs. It reduces all of the plumbing code so that you can focus on just your application logic. In this chapter, you will learn how to build web APIs and bindings related to Azure Storage Services.
Chapter 2, Working with Notifications Using the SendGrid and Twilio Services, deals with how communication is one of the most critical aspects of any business requirement. In this chapter, you will learn how easy it is to connect your business requirements written in Azure Functions with the most popular communication services, such as SendGrid (for email) and Twilio (for SMS).
Chapter 3, Seamless Integration of Azure Functions with Azure Services, discusses how Azure provides many connectors that you could leverage to integrate your business applications with other systems pretty easily. In this chapter, you will learn how to integrate Azure Functions with cognitive services and Logic Apps.
Chapter 4, Understanding the Integrated Developer Experience of Visual Studio Tools for Azure Functions, teaches you how to develop Azure Functions using Visual Studio, which provides many features, such as IntelliSense, local and remote debugging, and most of the regular development features.
Chapter 5, Exploring Testing Tools for the Validation of Azure Functions, helps you to understand different tools and processes that help you streamline your development and quality control processes. You will also learn how to create loads using Azure DevOps (formerly VSTS) load testing, and you'll look at how to monitor the performance of Azure Functions using the reports provided by Application Insights. Finally, you will also learn how to configure alerts that notify you when your apps are not responsive.
Chapter 6, Monitoring and Troubleshooting Azure Serverless Services, teaches you how to continuously monitor applications, analyze the performance, and review the logs to understand whether there are any issues that end users are facing. Azure provides us with multiple tools to achieve all the monitoring requirements, right from the development and maintenance stages of the application.
Chapter 7, Developing Reliable Serverless Applications Using Durable Functions, shows you how to develop long-running, stateful solutions in serverless environments using Durable Functions, which has advanced features that have been released as an extension to Azure Functions.
Chapter 8, Bulk Import of Data Using Azure Durable Functions and Cosmos DB, teaches you how to leverage Azure Durable Functions to read and import the data from the Blob storage and dump the data into Cosmos DB.
Chapter 9, Implementing Best Practices for Azure Functions, teaches a few of the best practices that you should follow in order to improve performance and security while working in Azure Functions.
Chapter 10, Configuring of Serverless Applications in the Production Environment, demonstrates how to deploy a function app in an efficient way and copy/move the configurations in a smarter way so as to avoid human error. You will also learn how to configure a custom domain that you could share with your customers or end users instead of the default domain that is created as part of provisioning the function app.
Chapter 11, Implementing and Deploying Continuous Integration Using Azure DevOps, helps you learn how to implement continuous integration and delivery of your Azure Functions code with the help of Visual Studio and Azure DevOps.
Prior knowledge and hands-on experience with the core services of Microsoft Azure is required.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
Support
tab.
Click on
Code Downloads
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Azure-Serverless-Computing-Cookbook-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781789615265_ColorImages.pdf.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."
A block of code is set as follows:
using System.Net;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Primitives;using Newtonsoft.Json;
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
using System.Net;using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
Any command-line input or output is written as follows:
docker tag functionsindocker cookbookregistry.azurecr.io/functionsindocker:v1
Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "During the installation, choose Azure development in the Workloads section."
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to help you increase your knowledge of it.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
In this chapter, we will cover the following recipes:
Building a backend Web API using HTTP triggers
Persisting employee details using Azure Storage table output bindings
Saving the profile images to Queues using Queue output bindings
Storing the image in Azure Blob Storage
Every software application needs backend components that are responsible for taking care of business logic and storing the data in some kind of storage, such as databases and filesystems. Each of these backend components could be developed using different technologies. Azure serverless technology also allows us to develop these backend APIs using Azure Functions.
Azure Functions provide many out-of-the-box templates that solve most common problems, such as connecting to storage, building Web APIs, and cropping images. In this chapter, we will learn how to use these built-in templates. Apart from learning about the concepts related to Azure serverless computing, we will also try to implement a solution to a basic domain problem of creating components, which is required for any organization who wants to manage internal employee information.
The following is a simple diagram that will help you understand what we will achieve in this chapter:
We will use the Azure serverless architecture to build a Web API using HTTP triggers. These HTTP triggers can be consumed by any frontend application that is capable of making HTTP calls.
Let's start our journey of understanding Azure serverless computing using Azure Functions by creating a basic backend Web API that responds to HTTP requests:
Refer to
https://azure.microsoft.com/en-in/free/?&wt.mc_id=AID607363_SEM_8y6Q27AS
for creating a free Azure Account.
Visit
https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-app-portal
to understand the step-by-step process of creating a function app, and
https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function
to create a function. While creating a function, a Storage Account is also created for storing all the files. Remember the name of the Storage Account, as it will be used later in other chapters.
Once you have created the Function App, please go through the basic concepts of Triggers and Bindings, which are the core concepts of how Azure Functions work. I highly recommend that you to go through the
https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings
article before you proceed.
