Skip to content

Commit

Permalink
Add azure-nextgen-py-static-website example (pulumi#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Feb 18, 2021
1 parent 39ac717 commit b2208bd
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 0 deletions.
6 changes: 6 additions & 0 deletions azure-nextgen-py-static-website/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: azure-nextgen-py-static-website
runtime:
name: python
options:
virtualenv: venv
description: An example of a static website hosted on Azure Blob Storage + Azure CDN.
62 changes: 62 additions & 0 deletions azure-nextgen-py-static-website/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![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. Restore NPM dependencies:

```
$ npm install
```

1. Set the Azure region location to use:

```
$ pulumi config set azure-nextgen:location westus
```

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>
```
109 changes: 109 additions & 0 deletions azure-nextgen-py-static-website/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2016-2021, Pulumi Corporation. All rights reserved.

import pulumi
import pulumi_azure_nextgen as azure_nextgen
import pulumi_random as random

# TODO: Remove after autonaming support is added.
random_suffix = random.RandomString("randomSuffix",
length=10,
special=False,
upper=False)

config = pulumi.Config()
storage_account_name = config.get("storageAccountName")
if storage_account_name is None:
storage_account_name = random_suffix.result.apply(lambda result: f"site{result}")
cdn_endpoint_name = config.get("cdnEndpointName")
if cdn_endpoint_name is None:
cdn_endpoint_name = storage_account_name.apply(lambda result: f"cdn-endpnt-{result}")
cdn_profile_name = config.get("cdnProfileName")
if cdn_profile_name is None:
cdn_profile_name = storage_account_name.apply(lambda result: f"cdn-profile-{result}")

resource_group = azure_nextgen.resources.latest.ResourceGroup("resourceGroup",
resource_group_name=random_suffix.result.apply(lambda result: f"rg{result}"))

profile = azure_nextgen.cdn.latest.Profile("profile",
profile_name=cdn_profile_name,
resource_group_name=resource_group.name,
sku=azure_nextgen.cdn.latest.SkuArgs(
name=azure_nextgen.cdn.latest.SkuName.STANDARD_MICROSOFT,
))

storage_account = azure_nextgen.storage.latest.StorageAccount("storageAccount",
access_tier=azure_nextgen.storage.latest.AccessTier.HOT,
account_name=storage_account_name,
enable_https_traffic_only=True,
encryption=azure_nextgen.storage.latest.EncryptionArgs(
key_source=azure_nextgen.storage.latest.KeySource.MICROSOFT_STORAGE,
services=azure_nextgen.storage.latest.EncryptionServicesArgs(
blob=azure_nextgen.storage.latest.EncryptionServiceArgs(
enabled=True,
),
file=azure_nextgen.storage.latest.EncryptionServiceArgs(
enabled=True,
),
),
),
kind=azure_nextgen.storage.latest.Kind.STORAGE_V2,
network_rule_set=azure_nextgen.storage.latest.NetworkRuleSetArgs(
bypass=azure_nextgen.storage.latest.Bypass.AZURE_SERVICES,
default_action=azure_nextgen.storage.latest.DefaultAction.ALLOW,
),
resource_group_name=resource_group.name,
sku=azure_nextgen.storage.latest.SkuArgs(
name=azure_nextgen.storage.latest.SkuName.STANDARD_LRS,
))

endpoint_origin = storage_account.primary_endpoints.apply(
lambda primary_endpoints: primary_endpoints.web.replace("https://", "").replace("/", ""))

endpoint = azure_nextgen.cdn.latest.Endpoint("endpoint",
content_types_to_compress=[],
endpoint_name=cdn_endpoint_name,
is_compression_enabled=False,
is_http_allowed=False,
is_https_allowed=True,
origin_host_header=endpoint_origin,
origins=[azure_nextgen.cdn.latest.DeepCreatedOriginArgs(
host_name=endpoint_origin,
https_port=443,
name=pulumi.Output.all(cdn_endpoint_name, random_suffix.result).apply(
lambda result: f"{result[0]}-origin-{result[1]}"),
)],
profile_name=profile.name,
query_string_caching_behavior=azure_nextgen.cdn.latest.QueryStringCachingBehavior.NOT_SET,
resource_group_name=resource_group.name)

# Enable static website support
static_website = azure_nextgen.storage.latest.StorageAccountStaticWebsite("staticWebsite",
account_name=storage_account.name,
resource_group_name=resource_group.name,
index_document="index.html",
error404_document="404.html")

# Upload the files
index_html = azure_nextgen.storage.latest.Blob("index_html",
blob_name="index.html",
resource_group_name=resource_group.name,
account_name=storage_account.name,
container_name=static_website.container_name,
type=azure_nextgen.storage.latest.BlobType.BLOCK,
source=pulumi.FileAsset("./wwwroot/index.html"),
content_type="text/html")
notfound_html = azure_nextgen.storage.latest.Blob("notfound_html",
blob_name="404.html",
resource_group_name=resource_group.name,
account_name=storage_account.name,
container_name=static_website.container_name,
type=azure_nextgen.storage.latest.BlobType.BLOCK,
source=pulumi.FileAsset("./wwwroot/404.html"),
content_type="text/html")

# Web endpoint to the website
pulumi.export("staticEndpoint", storage_account.primary_endpoints.web)

# CDN endpoint to the website.
# Allow it some time after the deployment to get ready.
pulumi.export("cdnEndpoint", endpoint.host_name.apply(lambda host_name: f"https://{host_name}"))
4 changes: 4 additions & 0 deletions azure-nextgen-py-static-website/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pulumi>=2.0.0,<3.0.0
pulumi-random>=3.0.0
pulumi-azure-nextgen>=0.6.1
typing_extensions>=3.7.4
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions azure-nextgen-ts-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-ts-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 b2208bd

Please sign in to comment.