Skip to content

Commit

Permalink
Add azure-nextgen-cs-static-website example (pulumi#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Feb 23, 2021
1 parent fdcac6f commit 0dce22c
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 0 deletions.
140 changes: 140 additions & 0 deletions azure-nextgen-cs-static-website/MyStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using Pulumi;
using AzureNextGen = Pulumi.AzureNextGen;
using Cdn = Pulumi.AzureNextGen.Cdn.Latest;
using Resources = Pulumi.AzureNextGen.Resources.Latest;
using Storage = Pulumi.AzureNextGen.Storage.Latest;
using Pulumi.Random;

class MyStack : Stack
{
public MyStack()
{
// TODO: Remove after autonaming support is added.
var randomSuffix = new RandomString("randomSuffix", new RandomStringArgs
{
Length = 10,
Special = false,
Upper = false,
});

var config = new Config();
var storageAccountName = config.Get("storageAccountName") != null ? Output.Create(config.Get("storageAccountName")!) : randomSuffix.Result.Apply(result => $"site{result}");
var cdnEndpointName = config.Get("cdnEndpointName") != null ? Output.Create(config.Get("cdnEndpointName")!) : storageAccountName.Apply(result => $"cdn-endpnt-{result}");
var cdnProfileName = config.Get("cdnProfileName") != null ? Output.Create(config.Get("cdnProfileName")!) : storageAccountName.Apply(result => $"cdn-profile-{result}");

var resourceGroup = new Resources.ResourceGroup("resourceGroup", new Resources.ResourceGroupArgs
{
ResourceGroupName = randomSuffix.Result.Apply(result => $"rg{result}"),
});

var profile = new Cdn.Profile("profile", new Cdn.ProfileArgs
{
ProfileName = cdnProfileName,
ResourceGroupName = resourceGroup.Name,
Sku = new Cdn.Inputs.SkuArgs
{
Name = Cdn.SkuName.Standard_Microsoft,
},
});

var storageAccount = new Storage.StorageAccount("storageAccount", new Storage.StorageAccountArgs
{
AccessTier = Storage.AccessTier.Hot,
AccountName = storageAccountName,
EnableHttpsTrafficOnly = true,
Encryption = new Storage.Inputs.EncryptionArgs
{
KeySource = Storage.KeySource.Microsoft_Storage,
Services = new Storage.Inputs.EncryptionServicesArgs
{
Blob = new Storage.Inputs.EncryptionServiceArgs
{
Enabled = true,
},
File = new Storage.Inputs.EncryptionServiceArgs
{
Enabled = true,
},
},
},
Kind = Storage.Kind.StorageV2,
NetworkRuleSet = new Storage.Inputs.NetworkRuleSetArgs
{
Bypass = Storage.Bypass.AzureServices,
DefaultAction = Storage.DefaultAction.Allow,
},
ResourceGroupName = resourceGroup.Name,
Sku = new Storage.Inputs.SkuArgs
{
Name = Storage.SkuName.Standard_LRS,
},
});

var endpointOrigin = storageAccount.PrimaryEndpoints.Apply(pe => pe.Web.Replace("https://", "").Replace("/", ""));

var endpoint = new Cdn.Endpoint("endpoint", new Cdn.EndpointArgs
{
ContentTypesToCompress = { },
EndpointName = cdnEndpointName,
IsCompressionEnabled = false,
IsHttpAllowed = false,
IsHttpsAllowed = true,
OriginHostHeader = endpointOrigin,
Origins =
{
new Cdn.Inputs.DeepCreatedOriginArgs
{
HostName = endpointOrigin,
HttpsPort = 443,
Name = Output.Tuple(randomSuffix.Result, cdnEndpointName).Apply(t => $"{t.Item2}-origin-{t.Item1}"),
},
},
ProfileName = profile.Name,
QueryStringCachingBehavior = Cdn.QueryStringCachingBehavior.NotSet,
ResourceGroupName = resourceGroup.Name,
});

// Enable static website support
var staticWebsite = new Storage.StorageAccountStaticWebsite("staticWebsite", new Storage.StorageAccountStaticWebsiteArgs
{
AccountName = storageAccount.Name,
ResourceGroupName = resourceGroup.Name,
IndexDocument = "index.html",
Error404Document = "404.html",
});

var index_html = new Storage.Blob("index_html", new Storage.BlobArgs
{
BlobName = "index.html",
ResourceGroupName = resourceGroup.Name,
AccountName = storageAccount.Name,
ContainerName = staticWebsite.ContainerName,
Type = Storage.BlobType.Block,
Source = new FileAsset("./wwwroot/index.html"),
ContentType = "text/html",
});
var notfound_html = new Storage.Blob("notfound_html", new Storage.BlobArgs
{
BlobName = "404.html",
ResourceGroupName = resourceGroup.Name,
AccountName = storageAccount.Name,
ContainerName = staticWebsite.ContainerName,
Type = Storage.BlobType.Block,
Source = new FileAsset("./wwwroot/404.html"),
ContentType = "text/html",
});

// Web endpoint to the website
this.StaticEndpoint = storageAccount.PrimaryEndpoints.Apply(primaryEndpoints => primaryEndpoints.Web);

// CDN endpoint to the website.
// Allow it some time after the deployment to get ready.
this.CdnEndpoint = endpoint.HostName.Apply(hostName => $"https://{hostName}");
}

[Output("staticEndpoint")]
public Output<string> StaticEndpoint { get; set; }
[Output("cdnEndpoint")]
public Output<string> CdnEndpoint { get; set; }
}
8 changes: 8 additions & 0 deletions azure-nextgen-cs-static-website/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
using System.Threading.Tasks;
using Pulumi;

class Program
{
static Task<int> Main() => Deployment.RunAsync<MyStack>();
}
3 changes: 3 additions & 0 deletions azure-nextgen-cs-static-website/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: azure-nextgen-cs-static-website
runtime: dotnet
description: An example of a static website hosted on Azure Blob Storage + Azure CDN.
50 changes: 50 additions & 0 deletions azure-nextgen-cs-static-website/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Static Website Using Azure Blob Storage and CDN
Based on https://github.com/zemien/static-website-ARM-template


This example configures [Static website hosting in Azure Storage](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website).

In addition to the Storage itself, a CDN is configured to serve files from the Blob container origin. This may be useful if you need to serve files via HTTPS from a custom domain (not shown in the example).

## Running the App

1. Create a new stack:

```
$ pulumi stack init dev
```

1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step):

```
$ az login
```

1. Run `pulumi up` to preview and deploy changes:

```
$ pulumi up
Previewing changes:
...
Performing changes:
...
Resources:
+ 9 created
Duration: 2m52s
```

1. Check the deployed website endpoint:

```
$ pulumi stack output staticEndpoint
https://websitesbc90978a1.z20.web.core.windows.net/
$ curl "$(pulumi stack output staticEndpoint)"
<html>
<body>
<h1>This file is served from Blob Storage (courtesy of Pulumi!)</h1>
</body>
</html>
```
16 changes: 16 additions & 0 deletions azure-nextgen-cs-static-website/StaticWebsite.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pulumi" Version="2.*" />
<PackageReference Include="Pulumi.AzureNextGen" Version="0.*" />
<PackageReference Include="Pulumi.Docker" Version="2.*" />
<PackageReference Include="Pulumi.Random" Version="3.*" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions azure-nextgen-cs-static-website/wwwroot/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>That's a 404! Still, from the Blob Storage.</h1>
</body>
</html>
5 changes: 5 additions & 0 deletions azure-nextgen-cs-static-website/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>This file is served from Blob Storage (courtesy of Pulumi!)</h1>
</body>
</html>

0 comments on commit 0dce22c

Please sign in to comment.