项目作者: suud

项目描述 :
AWS CDK - API Gateway from OpenAPI 3 Specs
高级语言: Python
项目地址: git://github.com/suud/cdk-openapigateway.git
创建时间: 2020-12-27T15:15:15Z
项目社区:https://github.com/suud/cdk-openapigateway

开源协议:MIT License

下载


openapigateway

Create an Amazon API Gateway from an OpenAPI 3 Document.

PyPI
PyPI - Python Version
Code style: black
PyPI - License

AWS CDK Construct that creates an Amazon API Gateway HttpApi based on a
parameterized OpenAPI 3 Document.

Installation

  1. pip install openapigateway

Usage

Example 1: API backed by Lambda Function

openapi.yaml:

  1. [...]
  2. paths:
  3. /pets:
  4. get:
  5. summary: List all pets
  6. responses:
  7. [...]
  8. x-amazon-apigateway-integration:
  9. uri: "${API_LAMBDA_ARN}"
  10. type: "AWS_PROXY"
  11. httpMethod: "POST"
  12. connectionType: "INTERNET"
  13. payloadFormatVersion: "2.0"
  14. x-amazon-apigateway-request-validator:
  15. validateRequestBody: true
  16. validateRequestParameters: true
  17. [...]

open_api_stack.py:

  1. from aws_cdk import core, aws_iam as iam, aws_lambda as _lambda
  2. from openapigateway import OpenApiGateway
  3. class OpenApiStack(core.Stack):
  4. def __init__(
  5. self, scope: core.Construct, construct_id: str, **kwargs
  6. ) -> None:
  7. super().__init__(scope, construct_id, **kwargs)
  8. # function that handles api request(s)
  9. api_lambda = _lambda.Function([...])
  10. # create api from openapi document and replace params
  11. openapi = OpenApiGateway(
  12. self,
  13. "OpenAPI Gateway",
  14. openapi_path="openapi.yaml",
  15. param_value_dict={"API_LAMBDA_ARN": api_lambda.function_arn},
  16. fail_on_warnings=True,
  17. )
  18. # grant HttpApi permission to invoke api lambda function
  19. api_lambda.add_permission(
  20. f"Invoke By {openapi.http_api.node.id} Permission",
  21. principal=iam.ServicePrincipal("apigateway.amazonaws.com"),
  22. action="lambda:InvokeFunction",
  23. source_arn=openapi.http_api_arn,
  24. )

Documentation

Development setup

optional: use virtualenv

  1. # create virtualenv on MacOS and Linux
  2. python3 -m venv .venv
  3. # activate virtualenv
  4. source .venv/bin/activate

install dependencies

To install this package, along with the tools you need to develop and publish
it, run the following:

  1. pip install -e ".[dev]"

Contributing

  1. Fork this repository
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

License

MIT