In order to create a new project, we would first create a tutorials folder so that we can create our Pulumi project inside. To do so, run these commands in your terminal:
mkdir tutorials && cd tutorials && pulumi new azure-typescript
As Pulumi creates a project for you, the CLI will walk you through a few steps. You can choose to set your project name, description, stack name, and azure location, or you can use the defaults that Pulumi provides by hitting the Enter key. It’s totally up to you.
Once you have this set up, you should have an index.ts
, Pulumi.yaml
, & Pulumi.dev.yaml
(If you didn’t change the stack name).
Let’s go through what each of these files actually does:
To create our Azure function app, we would need to create a few resources:
The code snippet below allows us to use Pulumi and Typescript to declare these Azure resources.
import * as pulumi from "@pulumi/pulumi";import * as azure from "@pulumi/azure";const environment = pulumi.getStack();const location = "WestEurope";const resourceGroupName = "tutorial-rg";const appservicePlanName = "tutorial-plan";const storageAccountName = "tutorialstore";const functionAppName = "tutorial-app";// Create Resource Groupexport const resourceGroup = new azure.core.ResourceGroup(resourceGroupName,{location,name: resourceGroupName,tags: {environment}});// Create app service planexport const asp = new azure.appservice.Plan(appservicePlanName, {kind: "App",location,name: appservicePlanName,resourceGroupName: resourceGroup.name,sku: {size: "S1",tier: "Premium V2"},maximumElasticWorkerCount: 1});// Storage account for the function appexport const store = new azure.storage.Account(storageAccountName, {name: storageAccountName,accountReplicationType: "LRS",accountTier: "Standard",location: resourceGroup.location,accountKind: "StorageV2",resourceGroupName: resourceGroup.name,tags: {environment}});// Create function appexport const app = new azure.appservice.FunctionApp(functionAppName, {appServicePlanId: asp.id,location: resourceGroup.location,resourceGroupName: resourceGroup.name,storageAccountName: store.name,storageAccountAccessKey: store.primaryAccessKey,identity: {type: "SystemAssigned"},appSettings: {CLOUD_SERVICE_CONFIG: environment},siteConfig: {alwaysOn: true,use32BitWorkerProcess: false,websocketsEnabled: false},version: "~3",tags: {environment}});
To create the resources you just declared, run the command below in your terminal (make sure you are still logged in to pulumi and you are in your pulumi project directory):
pulumi up
It will do a preview of all the resources to be created, and ask if you would like to continue. You can select yes.
You have finally created Azure resources using Pulumi. You can go to your Azure portal to confirm that they are there.
Don’t forget to delete those Azure resources when you are done playing around with your new shiny toy 😊😊. You can do this by running:
pulumi destroy
If you like this tutorial and you want me to do more, please let me know.