Azure Functions created from deployment packages in Python and deployed to an App Service Plan on Linux.
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
using Pulumi;
using Pulumi.AzureNative.Insights;
using Pulumi.AzureNative.Web;
using Pulumi.AzureNative.Web.Inputs;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;
using Pulumi.AzureNative.Resources;
class FunctionsStack : Stack
{
public FunctionsStack()
{
var resourceGroup = new ResourceGroup("functions-rg");
var storageAccount = new StorageAccount("sa", new StorageAccountArgs
{
ResourceGroupName = resourceGroup.Name,
Sku = new SkuArgs
{
Name = SkuName.Standard_LRS,
},
Kind = Pulumi.AzureNative.Storage.Kind.StorageV2,
});
var appServicePlan = new AppServicePlan("functions-linux-asp", new AppServicePlanArgs
{
ResourceGroupName = resourceGroup.Name,
// Run on Linux
Kind = "Linux",
// Consumption plan SKU
Sku = new SkuDescriptionArgs
{
Tier = "Dynamic",
Name = "Y1"
},
// For Linux, you need to change the plan to have Reserved = true property.
Reserved = true
});
var container = new BlobContainer("zips-container", new BlobContainerArgs
{
AccountName = storageAccount.Name,
PublicAccess = PublicAccess.None,
ResourceGroupName = resourceGroup.Name,
});
var blob = new Blob("zip", new BlobArgs
{
AccountName = storageAccount.Name,
ContainerName = container.Name,
ResourceGroupName = resourceGroup.Name,
Type = BlobType.Block,
Source = new FileArchive("./functions")
});
var codeBlobUrl = SignedBlobReadUrl(blob, container, storageAccount, resourceGroup);
// Application insights
var appInsights = new Component("appInsights", new ComponentArgs
{
ApplicationType = ApplicationType.Web,
Kind = "web",
ResourceGroupName = resourceGroup.Name,
});
var app = new WebApp("app", new WebAppArgs
{
Kind = "FunctionApp",
ResourceGroupName = resourceGroup.Name,
ServerFarmId = appServicePlan.Id,
SiteConfig = new SiteConfigArgs
{
AppSettings = new[]
{
new NameValuePairArgs{
Name = "AzureWebJobsStorage",
Value = GetConnectionString(resourceGroup.Name, storageAccount.Name),
},
new NameValuePairArgs{
Name = "runtime",
Value = "python",
},
new NameValuePairArgs{
Name = "FUNCTIONS_WORKER_RUNTIME",
Value = "python",
},
new NameValuePairArgs{
Name = "WEBSITE_RUN_FROM_PACKAGE",
Value = codeBlobUrl,
},
new NameValuePairArgs{
Name = "APPLICATIONINSIGHTS_CONNECTION_STRING",
Value = Output.Format($"InstrumentationKey={appInsights.InstrumentationKey}"),
},
},
},
});
this.Endpoint = Output.Format($"https://{app.DefaultHostName}/api/Hello?name=Pulumi");
}
[Output] public Output<string> Endpoint { get; set; }
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}");
}
private static Output<string> GetConnectionString(Input<string> resourceGroupName, Input<string> accountName)
{
// Retrieve the primary storage account key.
var storageAccountKeys = ListStorageAccountKeys.Invoke(new ListStorageAccountKeysInvokeArgs
{
ResourceGroupName = resourceGroupName,
AccountName = accountName
});
return storageAccountKeys.Apply(keys =>
{
var primaryStorageKey = keys.Keys[0].Value;
// Build the connection string to the storage account.
return Output.Format($"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={primaryStorageKey}");
});
}
}
git clone https://github.com/pulumi/examples
cd examples/azure-cs-functions