Bỏ qua để đến nội dung

FastAPI

FastAPI là một framework để xây dựng API trong Python.

Generator FastAPI tạo ra một FastAPI mới với cấu hình hạ tầng AWS CDK hoặc Terraform. Backend được tạo sử dụng AWS Lambda cho triển khai serverless, được expose thông qua AWS API Gateway API. Nó thiết lập AWS Lambda Powertools cho khả năng quan sát, bao gồm logging, AWS X-Ray tracing và Cloudwatch Metrics.

Bạn có thể tạo một FastAPI mới theo hai cách:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - py#fast-api
  5. Fill in the required parameters
    • Click Generate
    Parameter Type Default Description
    name Required string - Name of the API project to generate
    computeType string ServerlessApiGatewayRestApi The type of compute to use to deploy this API. Choose between ServerlessApiGatewayRestApi (default) or ServerlessApiGatewayHttpApi.
    integrationPattern string isolated How API Gateway integrations are generated for the API. Choose between isolated (default) and shared.
    auth string IAM The method used to authenticate with your API. Choose between IAM (default), Cognito or None.
    directory string packages The directory to store the application in.
    iacProvider string Inherit The preferred IaC provider. By default this is inherited from your initial selection.
    moduleName string - Python module name

    Generator sẽ tạo cấu trúc dự án sau trong thư mục <directory>/<api-name>:

    • project.json Cấu hình dự án và build targets
    • pyproject.toml Cấu hình dự án Python và dependencies
    • run.sh Lambda Web Adapter bootstrap script để khởi động ứng dụng FastAPI thông qua uvicorn
    • Thư mục<module_name>
      • __init__.py Khởi tạo module
      • init.py Thiết lập ứng dụng FastAPI và cấu hình powertools middleware
      • main.py Triển khai API
    • Thư mụcscripts
      • generate_open_api.py Script để tạo OpenAPI schema từ ứng dụng FastAPI

    Vì generator này cung cấp infrastructure as code dựa trên iacProvider bạn đã chọn, nó sẽ tạo một dự án trong packages/common bao gồm các CDK constructs hoặc Terraform modules liên quan.

    Dự án infrastructure as code chung được cấu trúc như sau:

    • Thư mụcpackages/common/constructs
      • Thư mụcsrc
        • Thư mụcapp/ Constructs cho infrastructure cụ thể của một dự án/generator
        • Thư mụccore/ Constructs chung được tái sử dụng bởi các constructs trong app
        • index.ts Entry point xuất các constructs từ app
      • project.json Các build targets và cấu hình của dự án

    Để triển khai API của bạn, các tệp sau được tạo ra:

    • Thư mụcpackages/common/constructs/src
      • Thư mụcapp
        • Thư mụcapis
          • <project-name>.ts CDK construct để triển khai API của bạn
      • Thư mụccore
        • Thư mụcapi
          • http-api.ts CDK construct để triển khai HTTP API (nếu bạn chọn triển khai HTTP API)
          • rest-api.ts CDK construct để triển khai REST API (nếu bạn chọn triển khai REST API)
          • utils.ts Các tiện ích cho API constructs

    Triển khai API chính nằm trong main.py. Đây là nơi bạn định nghĩa các route API và triển khai của chúng. Ví dụ:

    from pydantic import BaseModel
    from .init import app, tracer
    class Item(BaseModel):
    name: str
    @app.get("/items/{item_id}")
    @tracer.capture_method
    def get_item(item_id: int) -> Item:
    return Item(name=...)
    @app.post("/items")
    @tracer.capture_method
    def create_item(item: Item):
    return ...

    Generator tự động thiết lập một số tính năng:

    1. Tích hợp AWS Lambda Powertools cho khả năng quan sát
    2. Error handling middleware
    3. Request/response correlation
    4. Thu thập metrics
    5. Triển khai AWS Lambda thông qua Lambda Web Adapter với uvicorn
    6. Type-safe streaming (chỉ REST API)

    Generator cấu hình structured logging sử dụng AWS Lambda Powertools. Bạn có thể truy cập logger trong các route handler:

    from .init import app, logger
    @app.get("/items/{item_id}")
    def read_item(item_id: int):
    logger.info("Fetching item", extra={"item_id": item_id})
    return {"item_id": item_id}

    Logger tự động bao gồm:

    • Correlation IDs cho request tracing
    • Request path và method
    • Thông tin Lambda context
    • Chỉ báo cold start

    AWS X-Ray tracing được cấu hình tự động. Bạn có thể thêm custom subsegments vào traces của mình:

    from .init import app, tracer
    @app.get("/items/{item_id}")
    @tracer.capture_method
    def read_item(item_id: int):
    # Tạo một subsegment mới
    with tracer.provider.in_subsegment("fetch-item-details"):
    # Logic của bạn ở đây
    return {"item_id": item_id}

    CloudWatch metrics được thu thập tự động cho mỗi request. Bạn có thể thêm custom metrics:

    from .init import app, metrics
    from aws_lambda_powertools.metrics import MetricUnit
    @app.get("/items/{item_id}")
    def read_item(item_id: int):
    metrics.add_metric(name="ItemViewed", unit=MetricUnit.Count, value=1)
    return {"item_id": item_id}

    Metrics mặc định bao gồm:

    • Số lượng request
    • Số lượng thành công/thất bại
    • Metrics cold start
    • Metrics theo từng route

    Generator bao gồm xử lý lỗi toàn diện:

    from fastapi import HTTPException
    @app.get("/items/{item_id}")
    def read_item(item_id: int):
    if item_id < 0:
    raise HTTPException(status_code=400, detail="Item ID must be positive")
    return {"item_id": item_id}

    Các exception không được xử lý sẽ được middleware bắt và:

    1. Ghi log exception đầy đủ với stack trace
    2. Ghi lại failure metric
    3. Trả về response 500 an toàn cho client
    4. Bảo toàn correlation ID

    FastAPI được tạo hỗ trợ streaming responses ngay từ đầu khi sử dụng REST API. Hạ tầng được cấu hình để sử dụng AWS Lambda Web Adapter để chạy FastAPI của bạn thông qua uvicorn bên trong Lambda, với ResponseTransferMode.STREAM trong API Gateway cho tất cả các REST API operations, cho phép streaming hoạt động cùng với các non-streaming operations.

    File init.py được tạo exports một class JsonStreamingResponse cung cấp type-safe streaming với việc tạo OpenAPI schema phù hợp. Điều này đảm bảo rằng connection generator có thể tạo ra các streaming client methods được type đúng cách.

    from pydantic import BaseModel
    from .init import app, JsonStreamingResponse
    class Chunk(BaseModel):
    message: str
    async def generate_chunks():
    for i in range(100):
    yield Chunk(message=f"This is chunk {i}")
    @app.post(
    "/stream",
    response_class=JsonStreamingResponse,
    responses={200: JsonStreamingResponse.openapi_response(Chunk, "Stream of chunks")},
    )
    async def my_stream() -> JsonStreamingResponse:
    return JsonStreamingResponse(generate_chunks())

    Class JsonStreamingResponse:

    1. Serialize các Pydantic models sang định dạng JSON Lines (application/jsonl)
    2. Cung cấp helper openapi_response tạo ra OpenAPI schema đúng với itemSchema, cho phép connection generator tạo ra các type-safe streaming client methods

    Để sử dụng một stream của responses, bạn có thể tận dụng connection generator sẽ cung cấp một phương thức type-safe để lặp qua các streamed chunks của bạn.

    Generator FastAPI tạo CDK hoặc Terraform infrastructure as code dựa trên iacProvider bạn đã chọn. Bạn có thể sử dụng điều này để triển khai FastAPI của mình.

    CDK construct để triển khai API của bạn trong thư mục common/constructs. Bạn có thể sử dụng nó trong một ứng dụng CDK:

    import { MyApi } from ':my-scope/common-constructs';
    export class ExampleStack extends Stack {
    constructor(scope: Construct, id: string) {
    // Thêm api vào stack của bạn
    const api = new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this).build(),
    });
    }
    }

    Điều này thiết lập:

    1. Một AWS Lambda function cho mỗi operation trong ứng dụng FastAPI
    2. API Gateway HTTP/REST API làm function trigger
    3. IAM roles và permissions
    4. CloudWatch log group
    5. Cấu hình X-Ray tracing
    6. CloudWatch metrics namespace

    Các construct CDK của REST/HTTP API được cấu hình để cung cấp giao diện type-safe cho việc định nghĩa các tích hợp cho từng operation của bạn.

    Các construct CDK cung cấp hỗ trợ tích hợp type-safe đầy đủ như mô tả bên dưới.

    Bạn có thể sử dụng defaultIntegrations tĩnh để sử dụng pattern mặc định, định nghĩa một AWS Lambda function riêng biệt cho mỗi operation:

    new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this).build(),
    });

    Bạn có thể truy cập các AWS Lambda function bên dưới thông qua thuộc tính integrations của construct API, theo cách type-safe. Ví dụ, nếu API của bạn định nghĩa một operation tên là sayHello và bạn cần thêm một số quyền cho function này, bạn có thể làm như sau:

    const api = new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this).build(),
    });
    // sayHello được type theo các operation được định nghĩa trong API của bạn
    api.integrations.sayHello.handler.addToRolePolicy(new PolicyStatement({
    effect: Effect.ALLOW,
    actions: [...],
    resources: [...],
    }));

    Nếu API của bạn sử dụng pattern shared, router Lambda dùng chung được expose dưới dạng api.integrations.$router:

    const api = new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this).build(),
    });
    api.integrations.$router.handler.addEnvironment('LOG_LEVEL', 'DEBUG');

    Nếu bạn muốn tùy chỉnh các tùy chọn được sử dụng khi tạo Lambda function cho mỗi tích hợp mặc định, bạn có thể sử dụng phương thức withDefaultOptions. Ví dụ, nếu bạn muốn tất cả các Lambda function của mình nằm trong một Vpc:

    const vpc = new Vpc(this, 'Vpc', ...);
    new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this)
    .withDefaultOptions({
    vpc,
    })
    .build(),
    });

    Bạn cũng có thể ghi đè các tích hợp cho các operation cụ thể bằng phương thức withOverrides. Mỗi ghi đè phải chỉ định một thuộc tính integration được type theo construct tích hợp CDK phù hợp cho HTTP hoặc REST API. Phương thức withOverrides cũng là type-safe. Ví dụ, nếu bạn muốn ghi đè một API getDocumentation để trỏ đến tài liệu được lưu trữ bởi một trang web bên ngoài, bạn có thể thực hiện như sau:

    new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this)
    .withOverrides({
    getDocumentation: {
    integration: new HttpIntegration('https://example.com/documentation'),
    },
    })
    .build(),
    });

    Bạn cũng sẽ nhận thấy rằng tích hợp được ghi đè không còn có thuộc tính handler khi truy cập nó thông qua api.integrations.getDocumentation.

    Bạn có thể thêm các thuộc tính bổ sung vào một tích hợp cũng sẽ được type tương ứng, cho phép các loại tích hợp khác được trừu tượng hóa nhưng vẫn giữ type-safe, ví dụ nếu bạn đã tạo một tích hợp S3 cho REST API và sau này muốn tham chiếu bucket cho một operation cụ thể, bạn có thể làm như sau:

    const storageBucket = new Bucket(this, 'Bucket', { ... });
    const apiGatewayRole = new Role(this, 'ApiGatewayS3Role', {
    assumedBy: new ServicePrincipal('apigateway.amazonaws.com'),
    });
    storageBucket.grantRead(apiGatewayRole);
    const api = new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this)
    .withOverrides({
    getFile: {
    bucket: storageBucket,
    integration: new AwsIntegration({
    service: 's3',
    integrationHttpMethod: 'GET',
    path: `${storageBucket.bucketName}/{fileName}`,
    options: {
    credentialsRole: apiGatewayRole,
    requestParameters: {
    'integration.request.path.fileName': 'method.request.querystring.fileName',
    },
    integrationResponses: [{ statusCode: '200' }],
    },
    }),
    options: {
    requestParameters: {
    'method.request.querystring.fileName': true,
    },
    methodResponses: [{
    statusCode: '200',
    }],
    }
    },
    })
    .build(),
    });
    // Sau này, có thể trong một file khác, bạn có thể truy cập thuộc tính bucket mà chúng ta đã định nghĩa
    // theo cách type-safe
    api.integrations.getFile.bucket.grantRead(...);

    Bạn cũng có thể cung cấp options trong tích hợp của mình để ghi đè các tùy chọn method cụ thể như authorizer, ví dụ nếu bạn muốn sử dụng xác thực Cognito cho operation getDocumentation của mình:

    new MyApi(this, 'MyApi', {
    integrations: MyApi.defaultIntegrations(this)
    .withOverrides({
    getDocumentation: {
    integration: new HttpIntegration('https://example.com/documentation'),
    options: {
    authorizer: new CognitoUserPoolsAuthorizer(...) // cho REST, hoặc HttpUserPoolAuthorizer cho HTTP API
    }
    },
    })
    .build(),
    });

    Nếu bạn muốn, bạn có thể chọn không sử dụng các tích hợp mặc định và thay vào đó cung cấp trực tiếp một tích hợp cho mỗi operation. Điều này hữu ích nếu, ví dụ, mỗi operation cần sử dụng một loại tích hợp khác nhau hoặc bạn muốn nhận lỗi type khi thêm các operation mới:

    new MyApi(this, 'MyApi', {
    integrations: {
    sayHello: {
    integration: new LambdaIntegration(...),
    },
    getDocumentation: {
    integration: new HttpIntegration(...),
    },
    },
    });

    Các construct CDK API được tạo hỗ trợ hai integration pattern:

    • isolated tạo một Lambda function cho mỗi operation. Đây là mặc định cho các API được tạo.
    • shared tạo một router Lambda mặc định duy nhất và tái sử dụng nó cho mọi operation trừ khi bạn ghi đè các tích hợp cụ thể.

    isolated cung cấp cho bạn quyền và cấu hình chi tiết hơn cho mỗi operation. shared giảm sự phân tán của Lambda và tích hợp API Gateway trong khi vẫn cho phép ghi đè có chọn lọc.

    Ví dụ, đặt pattern thành 'shared' sẽ tạo một function duy nhất thay vì một function cho mỗi tích hợp:

    packages/common/constructs/src/app/apis/my-api.ts
    export class MyApi<...> extends ... {
    public static defaultIntegrations = (scope: Construct) => {
    ...
    return IntegrationBuilder.rest({
    pattern: 'shared',
    ...
    });
    };
    }

    Vì các operation trong FastAPI được định nghĩa bằng Python và hạ tầng CDK bằng TypeScript, chúng tôi sử dụng code-generation để cung cấp metadata cho CDK construct nhằm cung cấp giao diện type-safe cho integrations.

    Một target generate:<ApiName>-metadata được thêm vào project.json của common constructs để hỗ trợ việc tạo code này, tạo ra một file như packages/common/constructs/src/generated/my-api/metadata.gen.ts. Vì điều này được tạo tại thời điểm build, nó bị bỏ qua trong version control.

    Nếu bạn chọn sử dụng xác thực IAM, bạn có thể sử dụng phương thức grantInvokeAccess để cấp quyền truy cập vào API của mình:

    api.grantInvokeAccess(myIdentityPool.authenticatedRole);

    Generator cấu hình một local development server mà bạn có thể chạy với:

    Terminal window
    pnpm nx run my-api:serve

    Điều này khởi động một local FastAPI development server với:

    • Auto-reload khi có thay đổi code
    • Tài liệu API tương tác tại /docs hoặc /redoc
    • OpenAPI schema tại /openapi.json

    Để gọi API của bạn từ một React website, bạn có thể sử dụng connection generator.