Provisions Azure SQL Database and Azure Application Insights to be used with web application hosted in App Service.
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
using Pulumi;
using Pulumi.AzureNative.Insights;
using Pulumi.AzureNative.OperationalInsights;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Sql;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;
using Pulumi.AzureNative.Web;
using Pulumi.AzureNative.Web.Inputs;
class AppServiceStack : Stack
{
public AppServiceStack()
{
var resourceGroup = new ResourceGroup("appservice-rg");
var storageAccount = new StorageAccount("sa", new StorageAccountArgs
{
ResourceGroupName = resourceGroup.Name,
Kind = "StorageV2",
Sku = new SkuArgs
{
Name = SkuName.Standard_LRS,
},
});
var appServicePlan = new AppServicePlan("asp", new AppServicePlanArgs
{
ResourceGroupName = resourceGroup.Name,
Kind = "App",
Sku = new SkuDescriptionArgs
{
Tier = "Basic",
Name = "B1",
},
});
var container = new BlobContainer("zips", new BlobContainerArgs
{
AccountName = storageAccount.Name,
PublicAccess = PublicAccess.None,
ResourceGroupName = resourceGroup.Name,
});
var blob = new Blob("appservice-blob", new BlobArgs
{
ResourceGroupName = resourceGroup.Name,
AccountName = storageAccount.Name,
ContainerName = container.Name,
Type = BlobType.Block,
Source = new FileArchive("wwwroot"),
});
var codeBlobUrl = SignedBlobReadUrl(blob, container, storageAccount, resourceGroup);
var workspace = new Workspace("loganalyticsWorkspace", new WorkspaceArgs{
ResourceGroupName = resourceGroup.Name,
});
var appInsights = new Component("appInsights", new ComponentArgs
{
ApplicationType = "web",
Kind = "web",
ResourceGroupName = resourceGroup.Name,
WorkspaceResourceId = workspace.Id
});
var config = new Config();
var username = config.Get("sqlAdmin") ?? "pulumi";
var password = config.RequireSecret("sqlPassword");
var sqlServer = new Server("sqlserver", new ServerArgs
{
ResourceGroupName = resourceGroup.Name,
AdministratorLogin = username,
AdministratorLoginPassword = password,
Version = "12.0",
});
var database = new Database("db", new DatabaseArgs
{
ResourceGroupName = resourceGroup.Name,
ServerName = sqlServer.Name,
Sku = new Pulumi.AzureNative.Sql.Inputs.SkuArgs
{
Name = "S0"
}
});
var app = new WebApp("app", new WebAppArgs
{
ResourceGroupName = resourceGroup.Name,
ServerFarmId = appServicePlan.Id,
SiteConfig = new SiteConfigArgs
{
AppSettings = {
new NameValuePairArgs{
Name = "WEBSITE_RUN_FROM_PACKAGE",
Value = codeBlobUrl,
},
new NameValuePairArgs{
Name = "APPINSIGHTS_INSTRUMENTATIONKEY",
Value = appInsights.InstrumentationKey
},
new NameValuePairArgs{
Name = "APPLICATIONINSIGHTS_CONNECTION_STRING",
Value = appInsights.InstrumentationKey.Apply(key => $"InstrumentationKey={key}"),
},
new NameValuePairArgs{
Name = "ApplicationInsightsAgent_EXTENSION_VERSION",
Value = "~2",
},
},
ConnectionStrings = {
new ConnStringInfoArgs
{
Name = "db",
Type = ConnectionStringType.SQLAzure,
ConnectionString = Output.Tuple<string, string, string>(sqlServer.Name, database.Name, password).Apply(t =>
{
(string server, string database, string pwd) = t;
return
$"Server= tcp:{server}.database.windows.net;initial catalog={database};userID={username};password={pwd};Min Pool Size=0;Max Pool Size=30;Persist Security Info=true;";
}),
},
},
}
});
this.Endpoint = app.DefaultHostName;
}
private static Output<string> SignedBlobReadUrl(Blob blob, BlobContainer container, StorageAccount account, ResourceGroup resourceGroup)
{
var serviceSasToken = ListStorageAccountServiceSAS.Invoke(new ListStorageAccountServiceSASInvokeArgs
{
AccountName = account.Name,
Protocols = HttpProtocol.Https,
SharedAccessStartTime = "2021-01-01",
SharedAccessExpiryTime = "2030-01-01",
Resource = SignedResource.C,
ResourceGroupName = resourceGroup.Name,
Permissions = Permissions.R,
CanonicalizedResource = Output.Format($"/blob/{account.Name}/{container.Name}"),
ContentType = "application/json",
CacheControl = "max-age=5",
ContentDisposition = "inline",
ContentEncoding = "deflate",
}).Apply(blobSAS => blobSAS.ServiceSasToken);
return Output.Format($"https://{account.Name}.blob.core.windows.net/{container.Name}/{blob.Name}?{serviceSasToken}");
}
[Output] public Output<string> Endpoint { get; set; }
}
git clone https://github.com/pulumi/examples
cd examples/azure-cs-appservice