Update Azure Functions example to HttpEventSubscription
MODIFY
azure-ts-functions/README.md
azure-ts-functions/README.md
@@ -24,6 +24,12 @@ An example Pulumi component that deploys a TypeScript function to Azure Function
$ npm install
```
+1. Configure the location to deploy the example to:
+
+ ```
+ $ pulumi config set azure:location <location>
+ ```
+
1. Run `pulumi up` to preview and deploy changes:
```

DELETE
azure-ts-functions/azureFunction.ts
-import * as pulumi from "@pulumi/pulumi";
-import * as azure from "@pulumi/azure";
-
-import * as azurefunctions from "azure-functions-ts-essentials";
-
-export type Context = azurefunctions.Context;
-export type HttpRequest = azurefunctions.HttpRequest;
-export * from "azure-functions-ts-essentials";
-
-export type AzureFunctionHandler = (context: Context, request: HttpRequest) => void;
-
-
-async function blobContent(name: string, handler: AzureFunctionHandler): Promise<pulumi.asset.AssetMap> {
- let serializedHandler = await pulumi.runtime.serializeFunction(handler);
- let map: pulumi.asset.AssetMap = {};
-
- map["host.json"] = new pulumi.asset.StringAsset(JSON.stringify({}));
- map[`${name}/function.json`] = new pulumi.asset.StringAsset(JSON.stringify({
- "disabled": false,
- "bindings": [
- {
- "authLevel": "anonymous",
- "type": "httpTrigger",
- "direction": "in",
- "name": "req"
- },
- {
- "type": "http",
- "direction": "out",
- "name": "$return"
- }
- ]
- }));
- map[`${name}/index.js`] = new pulumi.asset.StringAsset(`module.exports = require("./handler").handler`),
- map[`${name}/handler.js`] = new pulumi.asset.StringAsset(serializedHandler.text);
-
- return map;
-}
-
-
-// An Azure function exposed via an HTTP endpoint.
-
-export class HttpFunction extends pulumi.ComponentResource {
- readonly resourceGroup: azure.core.ResourceGroup;
-
- readonly storageAccount: azure.storage.Account;
- readonly storageContainer: azure.storage.Container;
- readonly blob: azure.storage.ZipBlob;
-
- readonly appServicePlan: azure.appservice.Plan;
- readonly functionApp: azure.appservice.FunctionApp;
-
- readonly codeBlobUrl: pulumi.Output<string>;
- readonly endpoint: pulumi.Output<string>;
-
- constructor(name: string, handler: AzureFunctionHandler, options?: pulumi.ResourceOptions) {
- super("azure:HttpFunction", name, {}, options);
-
- let parentArgs = { parent: this };
-
- this.resourceGroup = new azure.core.ResourceGroup(`${name}-rg`, {
- location: "West US 2",
- }, parentArgs);
-
- let resourceGroupArgs = {
- resourceGroupName: this.resourceGroup.name,
- location: this.resourceGroup.location,
- };
-
- this.storageAccount = new azure.storage.Account(`${name}sa`, {
- ...resourceGroupArgs,
-
- accountKind: "StorageV2",
- accountTier: "Standard",
- accountReplicationType: "LRS",
- }, parentArgs);
-
- this.storageContainer = new azure.storage.Container(`${name}-c`, {
- resourceGroupName: this.resourceGroup.name,
- storageAccountName: this.storageAccount.name,
- containerAccessType: "private",
- }, parentArgs);
-
- this.blob = new azure.storage.ZipBlob(`${name}-b`, {
- resourceGroupName: this.resourceGroup.name,
- storageAccountName: this.storageAccount.name,
- storageContainerName: this.storageContainer.name,
- type: "block",
-
- content: new pulumi.asset.AssetArchive(blobContent(name, handler)),
- }, parentArgs);
-
- this.codeBlobUrl = azure.storage.signedBlobReadUrl(this.blob, this.storageAccount);
-
- this.appServicePlan = new azure.appservice.Plan(`${name}-p`, {
- ...resourceGroupArgs,
-
- kind: "FunctionApp",
-
- // https://social.msdn.microsoft.com/Forums/azure/en-US/665c365d-2b86-4a77-8cea-72ccffef216c
- sku: {
- tier: "Dynamic",
- size: "Y1",
- },
- }, parentArgs);
-
- this.functionApp = new azure.appservice.FunctionApp(`${name}-app`, {
- ...resourceGroupArgs,
-
- appServicePlanId: this.appServicePlan.id,
- storageConnectionString: this.storageAccount.primaryConnectionString,
-
- appSettings: {
- "WEBSITE_RUN_FROM_ZIP": this.codeBlobUrl,
- },
- }, parentArgs);
-
- this.endpoint = this.functionApp.defaultHostname.apply(h => {
- return `https://${h}/api/${name}`;
- });
- }
-};

MODIFY
azure-ts-functions/index.ts
azure-ts-functions/index.ts
-import * as azureFunction from "./azureFunction";
+import * as azure from "@pulumi/azure";
-// Create an Azure function that prints a message and the request headers.
+const resourceGroup = new azure.core.ResourceGroup('example');
-function handler(context: azureFunction.Context, request: azureFunction.HttpRequest) {
+// Create an Azure function that prints a message and the request headers.
+async function handler(context: azure.appservice.Context<azure.appservice.HttpResponse>, request: azure.appservice.HttpRequest) {
let body = "";
- let headers = context.req!.headers!;
- for (let h in headers) {
+ const headers = request.headers;
+ for (const h in request.headers) {
body = body + `${h} = ${headers[h]}\n`;
}
- let res: azureFunction.HttpResponse = {
- status: azureFunction.HttpStatusCode.OK,
+ return {
+ status: 200,
headers: {
"content-type": "text/plain",
},
body: `Greetings from Azure Functions!\n\n===\n\n${body}`,
};
-
- context.done(undefined, res);
}
-let fn = new azureFunction.HttpFunction("fn", handler);
-export let endpoint = fn.endpoint;
+let fn = new azure.appservice.HttpEventSubscription("fn", {
+ resourceGroup,
+ callback: handler
+});
+
+export let endpoint = fn.url;

MODIFY
azure-ts-functions/package.json
azure-ts-functions/package.json
"name": "azure-ts-functions",
"version": "1.0.0",
"devDependencies": {
- "@types/node": "^10.3.3",
- "azure-functions-ts-essentials": "^1.3.2"
+ "@types/node": "^10.3.3"
},
"dependencies": {
"@pulumi/azure": "latest",

MODIFY
azure-ts-functions/tsconfig.json
azure-ts-functions/tsconfig.json
"strictNullChecks": true
},
"files": [
- "azureFunction.ts",
"index.ts"
]
}

Update Azure Functions example to HttpEventSubscription