|
| 1 | +--- |
| 2 | +description: 'Learn how Nitric provisions and manages websites with Terraform and Pulumi across AWS, GCP, and Azure.' |
| 3 | +--- |
| 4 | + |
| 5 | +# Websites |
| 6 | + |
| 7 | +## 1. System Context |
| 8 | + |
| 9 | +**Developers** use Nitric to define required websites within their application. |
| 10 | + |
| 11 | +- App code uses the [Website resource](/websites) defined in `nitric.yaml` to specify the websites and their configurations. |
| 12 | +- Developers can configure base path, index pages, error pages, and other settings in their `nitric.yaml` file. |
| 13 | +- When a website is defined, a single CDN endpoint is automatically created to serve the website content globally, including APIs as proxies. |
| 14 | + |
| 15 | +**Operations** use default or overridden IaC (e.g., Terraform modules) to provision the necessary resources for their target cloud. |
| 16 | + |
| 17 | +<details> |
| 18 | + <summary>Example AWS Provider</summary> |
| 19 | + |
| 20 | +- **AWS S3** serves as the storage backend for static files (e.g., HTML, CSS, JS, images). |
| 21 | +- **AWS CloudFront** is used to distribute the website globally and serve as a single entry point for the whole application. |
| 22 | +- **AWS IAM** providers roles for access to the Website bucket |
| 23 | + |
| 24 | +```mermaid |
| 25 | +flowchart TD |
| 26 | + Developer["Developer"] |
| 27 | + NitricUp["nitric up"] |
| 28 | + S3["AWS S3 Bucket"] |
| 29 | + CloudFront["AWS CloudFront"] |
| 30 | + Rewrite["AWS API Gateway"] |
| 31 | +
|
| 32 | + Developer -->|Deploy| NitricUp |
| 33 | + NitricUp -->|Upload Assets| S3 |
| 34 | + NitricUp -->|Create CDN| CloudFront |
| 35 | + CloudFront -->|Serve Static Files| S3 |
| 36 | + CloudFront -->|Rewrite /api/* to API| Rewrite |
| 37 | +
|
| 38 | +classDef default line-height:1; |
| 39 | +classDef edgeLabel line-height:2; |
| 40 | +``` |
| 41 | + |
| 42 | +</details> |
| 43 | + |
| 44 | +<details> |
| 45 | + <summary>Example Azure Provider</summary> |
| 46 | +- **Static Website in Azure Blob Storage** serves as the storage backend for static files (e.g., HTML, CSS, JS, images). |
| 47 | +- **Azure Front Door** is used to distribute the website globally and serve as a single entry point for the whole application. |
| 48 | + |
| 49 | +```mermaid |
| 50 | +flowchart TD |
| 51 | + Developer["Developer"] |
| 52 | + NitricUp["nitric up"] |
| 53 | + Storage["Azure Storage"] |
| 54 | + FrontDoor["Azure Front Door"] |
| 55 | + Rewrite["Azure API Management"] |
| 56 | +
|
| 57 | + Developer -->|Deploy| NitricUp |
| 58 | + NitricUp -->|Upload Assets| Storage |
| 59 | + NitricUp -->|Create CDN| FrontDoor |
| 60 | + FrontDoor -->|Serve Static Files| Storage |
| 61 | + FrontDoor -->|Rewrite /api/* to API| Rewrite |
| 62 | +
|
| 63 | +classDef default line-height:1; |
| 64 | +classDef edgeLabel line-height:2; |
| 65 | +``` |
| 66 | + |
| 67 | +</details> |
| 68 | + |
| 69 | +## 2. Sequence |
| 70 | + |
| 71 | +### Build Sequence |
| 72 | + |
| 73 | +Below is the sequence of events that occur when a developer registers a website with Nitric: |
| 74 | + |
| 75 | +```mermaid |
| 76 | +sequenceDiagram |
| 77 | + participant Config as nitric.yaml |
| 78 | + participant CLI as Nitric CLI |
| 79 | + participant Provider as Nitric Provider<br>(plugin) |
| 80 | + participant IAC as IaC (e.g. Terraform) |
| 81 | +
|
| 82 | + CLI->>Config: Parse website configuration |
| 83 | + CLI->>Provider: Forward Nitric Spec |
| 84 | + Provider->>IAC: Provision Website |
| 85 | + Provider->>IAC: Provision IAM |
| 86 | +``` |
| 87 | + |
| 88 | +## 3. Component |
| 89 | + |
| 90 | +### Website Module |
| 91 | + |
| 92 | +- Deploys website assets to a cloud-based storage solution for flexible and scalable hosting. |
| 93 | +- Configures a distribution layer to serve the site globally, rewriting API endpoints to `/api/{apiName}` for consistent routing. |
| 94 | +- Automatically invalidates the cache based on file changes, ensuring users always receive the latest content. |
| 95 | + |
| 96 | +## 4. Code |
| 97 | + |
| 98 | +**Developers** write yaml configuration to define the website and implement logic to serve static files. |
| 99 | + |
| 100 | +### Nitric website configuration - nitric.yaml |
| 101 | + |
| 102 | +```yaml |
| 103 | +name: service-name |
| 104 | +services: |
| 105 | + - match: ./services/*.js |
| 106 | + start: npm run dev:services $SERVICE_PATH |
| 107 | + runtime: node |
| 108 | +runtimes: |
| 109 | + node: |
| 110 | + dockerfile: ./node.dockerfile |
| 111 | + args: {} |
| 112 | +# The website configuration |
| 113 | +websites: |
| 114 | + - basedir: ./my-website |
| 115 | + index: index.html |
| 116 | + error: 404.html |
| 117 | + build: |
| 118 | + command: npm run build |
| 119 | + output: ./dist |
| 120 | + dev: |
| 121 | + command: npm run dev -- --port 4322 |
| 122 | + url: http://localhost:4322 |
| 123 | +``` |
| 124 | +
|
| 125 | +### Access an API from the website frontend |
| 126 | +
|
| 127 | +```javascript |
| 128 | +async function fetchData() { |
| 129 | + // due to apis being served from the same domain thanks to rewrites, no CORS is required |
| 130 | + const response = await fetch('/api/main/hello') |
| 131 | + const data = await response.json() |
| 132 | + console.log(data) |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +**Operations** will use or extend the Nitric infrastructure modules, including both Terraform and Pulumi: |
| 137 | + |
| 138 | +- Terraform Modules: |
| 139 | + - Not yet available |
| 140 | +- Pulumi Modules: |
| 141 | + - [AWS Website Pulumi Module](https://github.com/nitrictech/nitric/blob/main/cloud/aws/deploy/website.go) |
| 142 | + - [Azure Website Pulumi Module](https://github.com/nitrictech/nitric/blob/main/cloud/azure/deploy/website.go) |
0 commit comments