|
| 1 | +--- |
| 2 | +description: 'C4 Api' |
| 3 | +tags: |
| 4 | + - C4 |
| 5 | +published_at: 2024-07-25 |
| 6 | +--- |
| 7 | + |
| 8 | +# Nitric 'API' Architecture |
| 9 | + |
| 10 | +## 1. System Context (Level 1) |
| 11 | + |
| 12 | +- A **Developer** uses Nitric to create and manage APIs within their application. |
| 13 | + - App code interacts with the **API resource** through defined endpoints. |
| 14 | + - Developers define API specifications and implement backend logic to handle requests. |
| 15 | +- **Operations** use default or overridden Terraform modules to provision the necessary AWS API Gateway resources. |
| 16 | + - **AWS API Gateway v2** serves as the HTTP API management service. |
| 17 | + - **AWS Lambda** functions are deployed to handle API requests. |
| 18 | + - **AWS IAM** (implicitly assumed) provides roles and policies for secure interaction between API Gateway and Lambda functions. |
| 19 | + - **AWS ACM** manages SSL/TLS certificates for custom domain names. |
| 20 | + |
| 21 | +```mermaid |
| 22 | +flowchart TD |
| 23 | + Developer["Developer"] |
| 24 | + Operations["Operations"] |
| 25 | + App["nitric up"] |
| 26 | + APIGateway["AWS API Gateway v2<br>(HTTP API)"] |
| 27 | + Lambda["AWS Lambda Functions"] |
| 28 | + IAM["AWS IAM"] |
| 29 | + ACM["AWS ACM<br>(Certificates)"] |
| 30 | +
|
| 31 | + Developer -->|Define API| App |
| 32 | + Operations -->|Terraform| App |
| 33 | + App -->|Create API Gateway| APIGateway |
| 34 | + App -->|Deploy Lambda Functions| Lambda |
| 35 | + App -->|Configure Permissions| IAM |
| 36 | + APIGateway -->|Invoke| Lambda |
| 37 | + ACM -->|Provide Certificates| APIGateway |
| 38 | + IAM -->|Manage Access| APIGateway |
| 39 | + App -->ACM |
| 40 | +
|
| 41 | +classDef default line-height:1; |
| 42 | +classDef edgeLabel line-height:2; |
| 43 | +``` |
| 44 | + |
| 45 | +## 2. Container (Level 2) |
| 46 | + |
| 47 | +Each **API** is managed through AWS API Gateway v2 and interacts with backend Lambda functions to process HTTP requests. |
| 48 | + |
| 49 | +```mermaid |
| 50 | +flowchart TD |
| 51 | + Nitric["Nitric Application"] |
| 52 | + Nitric -->B(API 1) |
| 53 | + Nitric -->C(API 2) |
| 54 | + Nitric -->D(...) |
| 55 | + B(API 1) -->E(HTTP GET) |
| 56 | + B(API 1) -->F(HTTP POST) |
| 57 | + B(API 1) -->G(...) |
| 58 | +
|
| 59 | +classDef default line-height:1; |
| 60 | +classDef edgeLabel line-height:2; |
| 61 | +``` |
| 62 | + |
| 63 | +## 3. Component (Level 3) |
| 64 | + |
| 65 | +### API Module |
| 66 | + |
| 67 | +- **aws_apigatewayv2_api.api_gateway** |
| 68 | + - Creates an AWS API Gateway v2 HTTP API. |
| 69 | + - Configures the API name, protocol type, API specification (`body`), and tags for identification and management. |
| 70 | +- **aws_apigatewayv2_stage.stage** |
| 71 | + - Creates a stage for the API Gateway. |
| 72 | + - Sets the stage name to `$default` and enables automatic deployment of changes. |
| 73 | +- **aws_lambda_permission.apigw_lambda** |
| 74 | + - Grants API Gateway permission to invoke the specified Lambda functions. |
| 75 | + - Iterates over `var.target_lambda_functions` to set permissions for each target function. |
| 76 | +- **data.aws_acm_certificate.cert** |
| 77 | + - Looks up existing ACM certificates for the specified domains. |
| 78 | + - Iterates over `var.domains` to retrieve certificate details for each domain. |
| 79 | +- **aws_apigatewayv2_domain_name.domain** |
| 80 | + |
| 81 | + - Creates custom domain names for the API Gateway using the retrieved ACM certificates. |
| 82 | + - Configures domain name settings, including the certificate ARN, endpoint type, and security policy. |
| 83 | + |
| 84 | +## 4. Code (Level 4) |
| 85 | + |
| 86 | +**Developers** write application code that imports the 'api' resource from the SDK, configures the api, and implements HTTP routes, implement middleware, etc. |
| 87 | + |
| 88 | +```typescript |
| 89 | +import { api } from '@nitric/sdk' |
| 90 | + |
| 91 | +const publicApi = api('public') |
| 92 | + |
| 93 | +api('public').get('/customers', (ctx) => { |
| 94 | + // construct response for the GET: /customers request... |
| 95 | + const responseBody = {} |
| 96 | + ctx.res.json(responseBody) |
| 97 | +}) |
| 98 | + |
| 99 | +const authMiddleware = async (ctx, next) => { |
| 100 | + // Perform auth validation. |
| 101 | + return await next(ctx) |
| 102 | +} |
| 103 | + |
| 104 | +const privateApi = api('private', { middleware: authMiddleware }) |
| 105 | +``` |
| 106 | + |
| 107 | +**Operations** will use the provided Terraform module to create and manage the AWS API Gateway as defined. |
| 108 | + |
| 109 | +```hcl |
| 110 | +resource "aws_apigatewayv2_api" "api_gateway" { |
| 111 | + name = var.name |
| 112 | + protocol_type = "HTTP" |
| 113 | + body = var.spec |
| 114 | + tags = { |
| 115 | + "x-nitric-${var.stack_id}-name" = var.name, |
| 116 | + "x-nitric-${var.stack_id}-type" = "api", |
| 117 | + } |
| 118 | +} |
| 119 | +
|
| 120 | +resource "aws_apigatewayv2_stage" "stage" { |
| 121 | + api_id = aws_apigatewayv2_api.api_gateway.id |
| 122 | + name = "$default" |
| 123 | + auto_deploy = true |
| 124 | +} |
| 125 | +
|
| 126 | +# deploy lambda permissions for execution |
| 127 | +resource "aws_lambda_permission" "apigw_lambda" { |
| 128 | + for_each = var.target_lambda_functions |
| 129 | + action = "lambda:InvokeFunction" |
| 130 | + function_name = each.value |
| 131 | + principal = "apigateway.amazonaws.com" |
| 132 | + source_arn = "${aws_apigatewayv2_api.api_gateway.execution_arn}/*/*/*" |
| 133 | +} |
| 134 | +
|
| 135 | +# look up existing certificate for domains |
| 136 | +data "aws_acm_certificate" "cert" { |
| 137 | + for_each = var.domains |
| 138 | + domain = each.value |
| 139 | +} |
| 140 | +
|
| 141 | +# deploy custom domain names |
| 142 | +resource "aws_apigatewayv2_domain_name" "domain" { |
| 143 | + for_each = var.domains |
| 144 | + domain_name = each.value |
| 145 | + domain_name_configuration { |
| 146 | + certificate_arn = data.aws_acm_certificate.cert[each.key].arn |
| 147 | + endpoint_type = "REGIONAL" |
| 148 | + security_policy = "TLS_1_2" |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
0 commit comments