|
| 1 | +--- |
| 2 | +description: 'Learn how Nitric provisions and manages APIs across AWS, GCP, and Azure using Terraform and Pulumi.' |
| 3 | +--- |
| 4 | + |
| 5 | +# APIs |
| 6 | + |
| 7 | +## 1. System Context |
| 8 | + |
| 9 | +**Developers** use Nitric to define required APIs and routes/methods within their application. |
| 10 | + |
| 11 | +- App code uses the [API resource](/apis) to define APIs and their routes/methods. |
| 12 | +- Developers define the API(s) their application requires, including the specifications and implement logic to handle HTTP requests. |
| 13 | +- Authentication, authorization, and middleware can be added to API routes to secure and enhance functionality. |
| 14 | + |
| 15 | +**Operations** use default or overridden IaC (e.g Terraform modules) to provision the necessary resources for their target cloud(s), such as API Gateways. |
| 16 | + |
| 17 | +<details> |
| 18 | + <summary>Example AWS Provider</summary> |
| 19 | + |
| 20 | +- **AWS API Gateway v2** serves as the HTTP API management service. |
| 21 | +- **AWS Lambda** functions are deployed to handle API requests. |
| 22 | +- **AWS IAM** (automated using IaC) provides roles and policies for secure interaction between API Gateway and Lambda functions. |
| 23 | +- **AWS ACM** manages TLS certificates for custom domain names. |
| 24 | + |
| 25 | +```mermaid |
| 26 | +flowchart TD |
| 27 | + Developer["Developer"] |
| 28 | + Operations["Operations"] |
| 29 | + App["nitric up"] |
| 30 | + APIGateway["AWS API Gateway v2<br>(HTTP API)"] |
| 31 | + Lambda["AWS Lambda Functions"] |
| 32 | + IAM["AWS IAM"] |
| 33 | + ACM["AWS ACM<br>(Certificates)"] |
| 34 | +
|
| 35 | + Developer -->|Define API| App |
| 36 | + Operations -->|Terraform| App |
| 37 | + App -->|Create API Gateway| APIGateway |
| 38 | + App -->|Deploy Lambda Functions| Lambda |
| 39 | + App -->|Configure Permissions| IAM |
| 40 | + APIGateway -->|Invoke| Lambda |
| 41 | + ACM -->|Provide Certificates| APIGateway |
| 42 | + IAM -->|Manage Access| APIGateway |
| 43 | + App -->ACM |
| 44 | +
|
| 45 | +classDef default line-height:1; |
| 46 | +classDef edgeLabel line-height:2; |
| 47 | +``` |
| 48 | + |
| 49 | +</details> |
| 50 | +<details> |
| 51 | + <summary>Example GCP Provider</summary> |
| 52 | + |
| 53 | +- **Google API Gateway** serves as the HTTP API management service, routing requests to backend services. |
| 54 | +- **Google Cloud Run** services are deployed to handle API requests with serverless execution. |
| 55 | +- **Google IAM** provides roles and policies to secure interactions between API Gateway, Cloud Run, and other GCP services. |
| 56 | +- **Certificates** (Google-managed or custom) ensure secure HTTPS communication for custom domain names. |
| 57 | +- **Google Service Account** is created and configured to allow API Gateway to invoke the Cloud Run backend securely. |
| 58 | + |
| 59 | +```mermaid |
| 60 | +flowchart TD |
| 61 | + Developer["Developer"] |
| 62 | + Operations["Operations"] |
| 63 | + App["nitric up"] |
| 64 | + APIGateway["Google API Gateway"] |
| 65 | + CloudRun["Google Cloud Run"] |
| 66 | + IAM["Google IAM"] |
| 67 | + Certificates["Certificates<br>(Google Managed or Custom)"] |
| 68 | + ServiceAccount["Google Service Account"] |
| 69 | +
|
| 70 | + Developer -->|Define API| App |
| 71 | + Operations -->|Terraform| App |
| 72 | + App -->|Create API Gateway| APIGateway |
| 73 | + App -->|Deploy Cloud Run Services| CloudRun |
| 74 | + App -->|Configure Permissions| IAM |
| 75 | + APIGateway -->|Invoke| CloudRun |
| 76 | + Certificates -->|Provide Certificates| APIGateway |
| 77 | + IAM -->|Manage Access| APIGateway |
| 78 | + App -->ServiceAccount |
| 79 | + App -->Certificates |
| 80 | +
|
| 81 | +classDef default line-height:1; |
| 82 | +classDef edgeLabel line-height:2; |
| 83 | +``` |
| 84 | + |
| 85 | +</details> |
| 86 | + |
| 87 | +## 2. Sequence |
| 88 | + |
| 89 | +### Build Sequence |
| 90 | + |
| 91 | +Here is the sequence of events that occur when a developer registers an API with Nitric, including the registration of routes, security, and middleware. |
| 92 | + |
| 93 | +```mermaid |
| 94 | +sequenceDiagram |
| 95 | + participant Worker as App Worker(s) |
| 96 | + participant SDK as Nitric SDK |
| 97 | + participant Nitric as Nitric CLI |
| 98 | + participant Provider as Nitric Provider <br> (plugin) |
| 99 | + participant IAC as IaC <br> (e.g. Terraform) |
| 100 | +
|
| 101 | + Worker->>SDK: Register API(s) |
| 102 | + SDK->>Nitric: Register API(s) |
| 103 | +
|
| 104 | + opt Authentication |
| 105 | + Worker->>SDK: Register Security |
| 106 | + SDK->>Nitric: Register Security |
| 107 | + end |
| 108 | +
|
| 109 | + Worker->>SDK: Register Route Callback(s) |
| 110 | + SDK->>Nitric: Register Route(s) |
| 111 | + Worker->>SDK: Register Middleware(s) |
| 112 | +
|
| 113 | + Nitric->>Nitric: Generate OpenAPI Spec |
| 114 | + Nitric->>Provider: Forward Nitric Spec |
| 115 | + Provider->>IAC: Provision API Gateway |
| 116 | + Provider->>IAC: Provision IAM |
| 117 | +``` |
| 118 | + |
| 119 | +### Runtime Sequence |
| 120 | + |
| 121 | +Here is the sequence of events that occur at runtime when a client makes an HTTP request to an API registered and deployed using Nitric. |
| 122 | + |
| 123 | +```mermaid |
| 124 | +sequenceDiagram |
| 125 | + participant Client as Client |
| 126 | + participant APIGateway as API Gateway <br> (e.g. AWS API Gateway) |
| 127 | + participant Auth as Auth Provider |
| 128 | + participant Nitric as Nitric Runtime <br> (plugin) |
| 129 | + participant SDK as Nitric SDK |
| 130 | + participant Worker as App Worker |
| 131 | +
|
| 132 | + Client->>APIGateway: HTTP Request |
| 133 | + opt Authentication |
| 134 | + APIGateway->>Auth: Verify Token |
| 135 | + APIGateway-->>Client: Unauthorized |
| 136 | + end |
| 137 | + APIGateway->>Nitric: Forward Request |
| 138 | + Nitric->>Nitric: Convert Request Format |
| 139 | + Nitric->>SDK: Route HTTP Event |
| 140 | + SDK->>Worker: Execute Route Callback |
| 141 | +
|
| 142 | + Worker->>Worker: Process Request |
| 143 | + Worker-->>Client: Response |
| 144 | +``` |
| 145 | + |
| 146 | +## 3. Component |
| 147 | + |
| 148 | +### API Module |
| 149 | + |
| 150 | +- Dynamically creates and manages API gateways to expose application functionality through HTTP endpoints and routes. |
| 151 | +- Configures API properties, including protocol type, API specifications (e.g., OpenAPI), and metadata for identification and lifecycle management. |
| 152 | +- Provisions and associates backend integrations with API routes, supporting multiple targets such as serverless functions, containers, or application services. |
| 153 | +- Automates the setup of custom domain names with secure certificates, abstracting provider-specific configurations for HTTPS communication. |
| 154 | +- Grants least privilege permissions to enable secure communication between the API gateway and backend services, ensuring robust security practices. |
| 155 | +- Supports versioning and staging of APIs, enabling seamless updates and rollbacks without disrupting existing deployments. |
| 156 | +- Abstracts the complexities of cloud-native API gateway services, providing a unified interface for developing and deploying HTTP APIs across different providers. |
| 157 | + |
| 158 | +## 4. Code |
| 159 | + |
| 160 | +**Developers** write application code that uses the [API resource](/apis) from the SDK, defining the APIs routes, methods, middleware and auth. |
| 161 | + |
| 162 | +SDK Reference by language: |
| 163 | + |
| 164 | +- [NodeJS SDK](/reference/nodejs/api/api) |
| 165 | +- [Python SDK](/reference/python/api/api) |
| 166 | +- [Go SDK](/reference/go/api/api) |
| 167 | +- [Dart SDK](/reference/dart/api/api) |
| 168 | + |
| 169 | +**Operations** will use or extend the Nitric infrastructure modules, including both Terraform and Pulumi: |
| 170 | + |
| 171 | +- Terraform Modules: |
| 172 | + - [AWS API Terraform Module](https://github.com/nitrictech/nitric/blob/main/cloud/aws/deploytf/.nitric/modules/api/main.tf) |
| 173 | + - [GCP API Terraform Module](https://github.com/nitrictech/nitric/blob/main/cloud/gcp/deploytf/.nitric/modules/api/main.tf) |
| 174 | +- Pulumi Modules: |
| 175 | + - [AWS API Pulumi Module](https://github.com/nitrictech/nitric/blob/main/cloud/aws/deploy/api.go) |
| 176 | + - [GCP API Pulumi Module](https://github.com/nitrictech/nitric/blob/main/cloud/gcp/deploy/api.go) |
| 177 | + - [Azure API Pulumi Module](https://github.com/nitrictech/nitric/blob/main/cloud/azure/deploy/api.go) |
0 commit comments