-
Notifications
You must be signed in to change notification settings - Fork 880
/
FunctionsStack.cs
149 lines (128 loc) · 5.24 KB
/
FunctionsStack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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}");
});
}
}