Creating App Service Plans in Azure using CDK for Terraform

In this post, you learn how to use CDK for Terraform to create App Service Plans in Azure. If you are familiar with AWS, App Service Plan is very similar to Elastic Beanstalk. This tutorial might incure costs in your account, so don’t forget to run “cdktf destroy” at the end.

First thing’s first, let’s initialize an empty CDK typescript project:

cdktf init

This brings a couple of interview-like questions which enables you to initialize the project. First, it asks you whether you want your statefile locally or in the cloud. For this demo, we opt for local version, but if you work within a team, or use this production workload, cloud variant could be the choice. Then with arrow keys you can choose the programming language of your choice. You can choose Typescript, Csharp, Java, go and many more.

Slightly different to AWS CDK, we then go to cdktf.json to declare the providers for our project. You add azurrm to the terraformProviders:

{
  "language": "typescript",
  "app": "npm run --silent compile && node main.js",
  "terraformProviders": ["azurerm@~> 2.46.0"],
  "terraformModules": [],
  "context": {
    "excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
  }
}

This is an optional step, but go ahead and run “npm run get” which sets up some code for the providers, in our case, azure.

Open main.ts and add the following import statements to before class declaration:

import { Construct } from “constructs”; import { App, TerraformStack } from “cdktf”; import { AppService, AppServicePlan, AzurermProvider, ResourceGroup} from ‘./.gen/providers/azurerm’;

Then we specifiy our provider:

new AzurermProvider(this, "AzureRM", {
  features: [{}],
});

Initializing our resource group:

const rg = new ResourceGroup(this, "rg-eastus", {
  name: "rg-eastus",
  location: "eastus",
});

We begin by creating our App Service Plan (Windows). To save costs for the development, I opt for F1 which is the shared tier for the VMs and free. Of course, for production workload you might wanna have a look at vm sizes on microsoft website.

const plan = new AppServicePlan(this, "servicePlan", {
  kind: "Windows",
  resourceGroupName: rg.name,
  location: rg.location,
  name: "cdkforTerraformApp",
  sku: [{ size: "F1", tier: "Free" }],
  dependsOn: [rg],
});

Then the App Service itself:

const service = new AppService(this, "service", {
  name: "appservice-cdktf",
  appServicePlanId: `${plan.id}`,
  location: rg.location,
  resourceGroupName: rg.name,
  dependsOn: [plan],
});

Finally, I get the URL of the App Service we just created:

new TerraformOutput(this, "appweburl", {
  value: `https://${service.name}.azurewebsites.net/`,
});

Now we are ready to verify our cdk stack. First run ‘npm run build’ which compiles typescript to javascript. Then run cdktf synth. This should output an executable and an HCL lock on it ready for deployment. If you directly want to deploy just write “cdktf deploy”. This will use your Azure credentials in you configurations to deploy to azure.

The source code on github: https://github.com/pedramha/cdktf-azureappservice

This project is maintained by pedramha