Azure API Management, Azure App Service

How to Get Started with Azure API Management : Part 1

In this post, we are discussing, how to Get Started with Azure API Management, How we can automate API subscription and management using Azure API Management.

This post is a part of the Azure Services series, in this series, we will discuss about Microsoft Cloud Application Architecture and common cloud computing patterns to make application resilient and scalable. We will be using Asp.net MVC as our framework for applicable examples and Azure as cloud services provider.

You can find all the Microsoft Azure Services; we have covered already at Azure series.

How to Get Started with Azure API Management : Part 1

The first question we get in our mind is, why to use Azure API management, what benefits it would give us, and why should we use it.
Azure API Management provide us the ability to create API gateway and developer portal, which is scalable, secure and can work with any host or API and it gives us insights of our APIs as well. The one thing most of us API developers struggle is, how to better manage on boarding process, documentation and provide easiest integration experience to our API’s end users /consumers/developers. This is the part, where Azure API management shines, it give us developer portal to manage the subscription of the services and provides a first-class developer experience. We will discuss all the functionality that Azure API Management provides and how to get most of it.

Let us create our Azure API management Instance first, usually it take a little bit longer around 30 mins to complete creation process.

Go to Azure portal dashboard, click create a new resource button | Search for API management | select API management | click create.

Now we need to fill the form, choose the pricing tier and click create. Let us look at pricing tier first; we have developer, basic, standard and premium tiers.

For Production, either go for basic or high tier since it will give you 99.9+ SLA uptime and options for scalability as well. Every tier support max request per second limitation, if you have only 1 API and 250 subscribers to the service then Basic tier will be sufficient if all 250 users send 2 request per second. From Developer to standard tier, it will only support single region, you can check which region is nearest to your customers/consumers by latency. If you have requirement to support multiple regions, then your only option is to go with Premium tier, and the price per month will increase drastically. Every tier also give us Redis cache as well and capacity is mentioned per instance, if you go for standard tier and you scale it up to 2 instances then you will have 1GB Redis Cache per instance totaling to 2 GB. Other things to consider is AAD integration and virtual network support, if you have need to connect with ADD then go for specific tier after evaluating price to value ratio and also consider virtual network support if you have API service hosted in VM in virtual network then consider it when you make any decision.

For our purpose, we are going with Developer tier, since we are not using it for production and it is giving us almost all the functionalities that other tiers give us except the scaling.

By default, the API management service will have a public URL yourdomain.azure-api.net but we can configure our own custom domain as well.

Creation process will take about 30+ minutes; it took around 38 mins to create in our case. Once the API Management Service is ready, you can see that

Click on developer portal link, will see the screen like below, currently developer/end users can sign up in this portal, the default sign in is configured by default to accept username and password. You can change this.

Go to your API Management Service in Azure Portal | Security | Identity, as you can see we have all the popular options such as Azure Active Directory, Facebook, Google, Microsoft as Identity provider. You may need to add or remove the Providers according to your business needs.

Go back to the Developer Portal and register with any secondary email, you will get confirmation email with link, click on the link to activate the user. Now login to the portal and navigate to the API tab. By Default, we have Echo API listed.

This window will show all the API that are available to consume, click on Echo API, you will see the window like below. By default, first resource is selected and in order to call this resource we need Ocp-Apim-Subscription-Key, you can get this key by subscribing to the Api. You can also click on Try it button to call the resource in the browser, we also have API Definition button if you want to download the API definition in WADL or Open API format.

Let us get our subscription ID, click on Products tabs, choose unlimited for now, we will talk about product later on and click subscribe and confirm the subscription.

Once you are done, a thank you email will be sent to you and you will see screen link below. Right now our unlimited product requires confirmation from Administrator side. So let us go to the Azure Portal and activate our subscription.

Back in the Azure Portal, click on Products | select Unlimited | subscriptions

We have two subscribers here, first one the API Management Service owner who have created this service, and second subscription entry is that we have just submitted earlier. Click on … button and choose Activate subscription and then click yes.

Now go back to Developer portal and refresh, you can see that subscription state is active now and you can also see the Primary key and secondary key.

Next step is to test the Echo service, click on APIS tab, select echo API and click on try. You can see now that the subscription key is automatically populated for you, you just need to click on send.

The response will be shown underneath of the send button.

Now let us create our own API and configure it with the Azure API Management, coming up next

Advertisements
Azure App Service, Azure Cloud, Azure SQL Database

How to use Azure SQL Database in Dot.net Applications: Part 5

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

5. How to use Azure SQL Database in Dot.net Applications

At this point, if you register with the app, which we have built earlier in this series, you will get an error, as we have not created or configured the Azure Database. It is time to get our hands on Azure SQL database.

Figure 1 Error while registering for new user.

Go to Azure Portal and Click on Add and search for SQL Database | select SQL Database and click on create.

Figure 2 Create SQL Database

When you create a SQL Database it will ask you to create Server as well, but you should not think this as a physical installation of the SQL server, in fact it is not even a virtual database server on azure but a logical container, this is just a container which will have databases you create. This databases you create in same container/Database server, do not mean that they will be in the same database server physically, Microsoft manages the infrastructure and although this logical container/database server will behave the same as if, you are using SQL management tools to manage it on premises installed MS SQL Server.

Figure 3 Create SQL Database and SQL Database Server container

Next in our list is elastic Pool, it is to manage the performance level and pricing etc. on a group of databases rather than selecting the performance level etc. in pricing tier for single database, which we do in case of single database creation mode. Be noted the pricing plan we are choosing, is per database basis and is not per container/database server as we are not going for elastic pool. If you have multiple databases, then choosing elastic pool will save you a lot of money.

For more information

Click on pricing tier and select the appropriate tier, we will choose the basic one as this is developer guide but in case of production workload, please go for at least standard pricing tier.

Figure 4 SQL Database pricing tier

As you can see, we have DTU as compared to previous metric, where we had CPU and RAM etc. DTU is just representation of the relative resources assigned to database, which include CPU, memory and read-write rates.

For more information on DTU

Select Basic | apply | create, to create database. It may take some time to create, do not worry if it takes time to complete. Once the database is created, Click on database and you will see screen like below:

Figure 5 SQL Database dashboard

By default, the SQL Database is accessible inside the Azure data center by other azure services and is not accessible outside, luckily, there is an option to add your current IP address and that way it is could  be accessible to you outside azure as well. Note that your current IP address may change over time, and it not your true static IP address, may be latter you connect to Azure your public IP change or can be assigned to other users, so it is best to remove it when you are not accessing database outside azure. You have an option of connecting this database in virtual network as well, in case you have some VM(s) or other services to communicate with each other.

Figure 6 Firewall settings SQL Server

Click Add client IP and save.

Now it is time to connect to the database using SQL Management Studio, if you do not have it, download it and install or you can even use visual Studio to connect to the database server. We are going to use SQL Management studio, open SQL Management Studio and put your credentials and connect, you will notice that the ICON of the server is changed that is because it is connecting to Azure SQL database server, also notice that the database is listed under database heading, which we had created earlier.

Figure 7 Connecting to Azure SQL Server using SQL Management Studio

Please note that the User we have used is admin user and it have access to all the databases that we have in our container/Database server, in order to limit the access you need to create new user. If it asks for you to connect with your Azure Subscription account while connecting to database server then do it, because by default, the Azure SQL Server will not be accessible to any one and by logging with your Subscription Account will add your IP address to white list for you automatically.

Let us create a new user for our application, after login go to Object Explorer | Security | Login | New Login, enter your credentials and Press F5 to execute the query.

CREATE
LOGIN myWebAppUser

    WITH
PASSWORD
=
YRY:)Q5t[{VB

GO

Figure 8 Create New User using SQL Management Studio

After that we need to create the user on database level and map this to Login we created in previous step and give the database role, for us we gave “db_owner” role, you can coordinate with your DBA to get the appropriate roles for your application user.

CREATE
USER myWebAppUser

    FOR
LOGIN myWebAppUser

    WITH
DEFAULT_SCHEMA
= dbo

GO

— Add user to the database owner role

EXEC
sp_addrolemember
N’db_owner’,
N’myWebAppUser’

GO

Figure 9 Mapping login user to Database user and giving database owner role

Now let us create our connection string and add it to our portal, you can also add it to release web.config in visual studio but to make it secure, it is best practice to not mention the staging or production connection string in source code.

Figure 10 Web.config

Go to Azure Portal| database | connection strings | Ado.net copy the connection string and update it with your username and password in any text editor.

Figure 11 Get Azure SQL Database connection string

<add
name=DefaultConnection
connectionString=Server=tcp:scientistzdbserver.database.windows.net,1433;Initial Catalog=scientistzDB;Persist Security Info=False;User ID=myWebAppUser;Password=YRY:)Q5t[{VB;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;/>

Now we will configure our App Service with the above connection string, for this we need to know the connection string name that is being used in our application. Go to Web.config and Connection string section, as we can see it is “DefaultConnection”

Figure 12 Get name of the connection string

Go to your App Service | Application Settings | Connection strings | add connection string you have created in previous steps | check Slot setting check box and click save.

Figure 13 Add Connection string to App Service

Now try to register again on your main application.

Figure 14 register new user in application

It will take some time to complete this first request, when the website automatically refresh you will see now you are successfully registered in the application.

Figure 15 Successfully Registered on App Service

If you go back to SQL Management Studio and refresh the tables you will see that our application have created new tables, when we registered for first time, that is why it took time for the very first time to complete the request. All the later requests for registrations will happen immediately. Right click on “dbo.AspnetUsers” and select “Select Top 1000 rows” and you will see the record created by entity framework and you can see the “_MigrationHistory”, which mean code first entity framework model is used.

Figure 16 New Tables are created after first registration request

With this congratulation, you know now, how to use Azure SQL database in our dot.net Applications

You can also visit official page of Azure SQL Database

Coming up next How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications