top of page

Infrastructure as Code - Bootstrapping Application Infrastructure

It has been projected that the IT budget for both cloud applications and cloud infrastructure will see an increase this year as their financial benefits become clearer. To stay ahead of the curve and reap the benefits of this movement, it is crucial that the proper investments are made in a company’s custom software. Improvements on the automation of the provisioning of infrastructure will allow employees to put their time elsewhere.

For years, developers have bootstrapped their applications to embrace the features of technologies that automate application enhancements and deploy software. A good example of this is the use of database migrations that can be taken advantage of by using an Object-Relational Mapping (ORM) platform like Entity Framework. Migrations are system generated code based on changes made by the developer to domain or model classes in the application (aka “Code First”). Those changes are maintained and added to source control with the application, then propagated to your database schema. Done right, migrations can alleviate time spent by DBAs and database professionals in writing and maintaining SQL scripts that are, in many cases, maintained separately from the application.

With the coming of the cloud and application containers, the infrastructure that is provisioned to host applications can too be bootstrapped, reversing the relationship between server resources and the software that runs on them. The example below uses the Azure cloud and the Microsoft.Azure.Management.Fluent library (found in NuGet) to provision resources to the cloud on demand when the application needs them and can be maintained and checked in within the application itself.

A database is often a necessary component of an application along with the server hosting the application. In the cloud world, this could be done by resource templating, Powershell or Azure CLI scripts but we will be including its creation within our application code. Below is creating a server using Microsoft.Azure.Management.Fluent in C# language. It is worth noting that the library is available in other languages such as Java.

// Create the Azure server instance ISqlServer sqlServer = azure.SqlServers.Define("AzureServer") .WithRegion(Region.USEast2) .WithExistingResourceGroup("RGName") .WithAdministratorLogin("UserName") .WithAdministratorPassword("Password") .Create();

A great way to keep cloud cost predictable when hosting applications that use a group of databases on Azure is by using an Elastic Pool. An elastic pool shares allotted server resources measured in DTUs (Database Transactional Units) among all the databases in the pool. SaaS applications often use this method due to the number of databases (or tenants) created. Below is our example again with a highlighted addition for creating the elastic pool.

// Create the Azure server instance with elastic pool ISqlServer sqlServer = azure.SqlServers.Define("AzureServer") .WithRegion(Region.USEast2) .WithExistingResourceGroup("RGName") .WithAdministratorLogin("UserName") .WithAdministratorPassword("Password") .WithNewElasticPool(“DBElasticPool”, ElasticPoolEdition.Standard) .Create();

Now that our hosting resources are created, databases can be provisioned. Below is an example of adding a database to the pool.

// Adding Database to pool var elasticPool = await sqlServer.ElasticPools.GetAsync(“DBElasticPool”); ISqlDatabase newDatabase = elasticPool?.AddNewDatabase(“DB1”);

In many cases, developers would like to dovetail the creation of these resources with deploying the database schema. The example below demonstrates this using a DataContext from Microsoft’s Entity Framework.

// Executing migrations to newly created database SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder(); sqlConnBuilder.UserID = "UserName"; sqlConnBuilder.Password = "Password"; sqlConnBuilder.InitialCatalog = "DB1"; sqlConnBuilder.DataSource = ""; sqlConnBuilder.ConnectTimeout = 30; DataContext context = new DataContext(sqlConnBuilder.ConnectionString); await context.Database.MigrateAsync();

Bootstrapping infrastructure has many advantages, especially in today’s cloud environment. As always, a developer must weigh their options in deriving a solution for the application that is being created. In the meantime, if you are looking for an agency to do this work for you, reach out to us or check out places like DesignRush.

bottom of page