From b2208bd281d5d466628306ddb30c4cb2036f1101 Mon Sep 17 00:00:00 2001 From: Levi Blackstone Date: Thu, 18 Feb 2021 14:02:06 -0700 Subject: [PATCH] Add azure-nextgen-py-static-website example (#916) --- azure-nextgen-py-static-website/Pulumi.yaml | 6 + azure-nextgen-py-static-website/README.md | 62 ++++++++++ azure-nextgen-py-static-website/__main__.py | 109 ++++++++++++++++++ .../requirements.txt | 4 + .../wwwroot/404.html | 0 .../wwwroot/index.html | 0 .../Pulumi.yaml | 0 .../README.md | 0 .../index.ts | 0 .../package.json | 0 .../tsconfig.json | 0 .../wwwroot/404.html | 5 + .../wwwroot/index.html | 5 + 13 files changed, 191 insertions(+) create mode 100644 azure-nextgen-py-static-website/Pulumi.yaml create mode 100644 azure-nextgen-py-static-website/README.md create mode 100644 azure-nextgen-py-static-website/__main__.py create mode 100644 azure-nextgen-py-static-website/requirements.txt rename {azure-nextgen-static-website => azure-nextgen-py-static-website}/wwwroot/404.html (100%) rename {azure-nextgen-static-website => azure-nextgen-py-static-website}/wwwroot/index.html (100%) rename {azure-nextgen-static-website => azure-nextgen-ts-static-website}/Pulumi.yaml (100%) rename {azure-nextgen-static-website => azure-nextgen-ts-static-website}/README.md (100%) rename {azure-nextgen-static-website => azure-nextgen-ts-static-website}/index.ts (100%) rename {azure-nextgen-static-website => azure-nextgen-ts-static-website}/package.json (100%) rename {azure-nextgen-static-website => azure-nextgen-ts-static-website}/tsconfig.json (100%) create mode 100644 azure-nextgen-ts-static-website/wwwroot/404.html create mode 100644 azure-nextgen-ts-static-website/wwwroot/index.html diff --git a/azure-nextgen-py-static-website/Pulumi.yaml b/azure-nextgen-py-static-website/Pulumi.yaml new file mode 100644 index 000000000..60f15a4c2 --- /dev/null +++ b/azure-nextgen-py-static-website/Pulumi.yaml @@ -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. diff --git a/azure-nextgen-py-static-website/README.md b/azure-nextgen-py-static-website/README.md new file mode 100644 index 000000000..a8d5b4896 --- /dev/null +++ b/azure-nextgen-py-static-website/README.md @@ -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)" + + +

This file is served from Blob Storage (courtesy of Pulumi!)

+ + + ``` diff --git a/azure-nextgen-py-static-website/__main__.py b/azure-nextgen-py-static-website/__main__.py new file mode 100644 index 000000000..4a13a613a --- /dev/null +++ b/azure-nextgen-py-static-website/__main__.py @@ -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}")) diff --git a/azure-nextgen-py-static-website/requirements.txt b/azure-nextgen-py-static-website/requirements.txt new file mode 100644 index 000000000..fec2dd1e6 --- /dev/null +++ b/azure-nextgen-py-static-website/requirements.txt @@ -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 diff --git a/azure-nextgen-static-website/wwwroot/404.html b/azure-nextgen-py-static-website/wwwroot/404.html similarity index 100% rename from azure-nextgen-static-website/wwwroot/404.html rename to azure-nextgen-py-static-website/wwwroot/404.html diff --git a/azure-nextgen-static-website/wwwroot/index.html b/azure-nextgen-py-static-website/wwwroot/index.html similarity index 100% rename from azure-nextgen-static-website/wwwroot/index.html rename to azure-nextgen-py-static-website/wwwroot/index.html diff --git a/azure-nextgen-static-website/Pulumi.yaml b/azure-nextgen-ts-static-website/Pulumi.yaml similarity index 100% rename from azure-nextgen-static-website/Pulumi.yaml rename to azure-nextgen-ts-static-website/Pulumi.yaml diff --git a/azure-nextgen-static-website/README.md b/azure-nextgen-ts-static-website/README.md similarity index 100% rename from azure-nextgen-static-website/README.md rename to azure-nextgen-ts-static-website/README.md diff --git a/azure-nextgen-static-website/index.ts b/azure-nextgen-ts-static-website/index.ts similarity index 100% rename from azure-nextgen-static-website/index.ts rename to azure-nextgen-ts-static-website/index.ts diff --git a/azure-nextgen-static-website/package.json b/azure-nextgen-ts-static-website/package.json similarity index 100% rename from azure-nextgen-static-website/package.json rename to azure-nextgen-ts-static-website/package.json diff --git a/azure-nextgen-static-website/tsconfig.json b/azure-nextgen-ts-static-website/tsconfig.json similarity index 100% rename from azure-nextgen-static-website/tsconfig.json rename to azure-nextgen-ts-static-website/tsconfig.json diff --git a/azure-nextgen-ts-static-website/wwwroot/404.html b/azure-nextgen-ts-static-website/wwwroot/404.html new file mode 100644 index 000000000..bf51abeb0 --- /dev/null +++ b/azure-nextgen-ts-static-website/wwwroot/404.html @@ -0,0 +1,5 @@ + + +

That's a 404! Still, from the Blob Storage.

+ + diff --git a/azure-nextgen-ts-static-website/wwwroot/index.html b/azure-nextgen-ts-static-website/wwwroot/index.html new file mode 100644 index 000000000..0e175a3a4 --- /dev/null +++ b/azure-nextgen-ts-static-website/wwwroot/index.html @@ -0,0 +1,5 @@ + + +

This file is served from Blob Storage (courtesy of Pulumi!)

+ +