API Gateway#
Use this module to build an API Gateway for the project.
api.RESTApi#
Class for the API Gateway.
Terraform resources:
- ApiGatewayRestApi: The REST API.
- ApiGatewayResource: API resource for the project (/endpoint_name).
- ApiGatewayResource: /predictions resource.
Arguments
Name | Type | Description |
---|---|---|
endpoint_name | str | Name of the resource for the project api |
tags | dict | Tags for all resource, must include a 'project' and 'env' key |
Attributes
Name | Type | Description |
---|---|---|
integrations | list | List of integration on the api |
api_id | str | Unique id provided by AWS |
data_resource_id | str | Id of the user defined resource |
pred_resource_id | str | Id of the predictions resource |
dim_resource_id | str | Id of the dimension resource |
api_key_value | str | API key secret value set during finalize() |
api.RESTApi.add_endpoint#
Methode to attach Lambda endpoint to the API.
Terraform resources:
- IamRole: Role for the Lambda.
- LambdaFunction: The Lambda function.
- CloudwatchLogGroup: Log group for Lambda Logging (retention 30 days).
- LambdaPermission: Allow invokation of the lambda from API Gateway.
- ApiGatewayMethod: Create a method (GET, PUT, etc.) on the endpoint.
- ApiGatewayIntegration: Attach the Lambda to the method.
Argument | Type | Description |
---|---|---|
http | str | Http methode (GET, PUT, DELETE, etc.) |
policies | list | List of policies arn to attatch to the function |
filename | str | Path to the zip file of the lambda |
environement | dict | Environement variables to pass to the function |
timeout | int | Lambda timeout. Default 5, must be lower than 30 |
resource | str | data, pred or dim for the resource to attatch the endpoint to. |
Returns: The function arn.
api.RESTApi.finalize#
Methode to finalize the API.
Terraform resources:
- ApiGatewayDeployment: Deploy the API (make it accessible to the public internet).
- ApiGatewayStage: An API Stage (version) with name 'v1'.
- ApiGatewayUsagePlan: A usage plan for the api. No restriction applied on it.
- ApiGatewayApiKey: An API Key to query the endpoints.
- ApiGatewayUsagePlanKey: Attach the key to the usage plan.
Example#
Create an api and attach one lambda to /stockprice/GET:
from src.api import RESTApi
tags = {
"project": "My Project",
"env": "dev"
}
myapi = RESTApi(
self,
"api",
endpoint_name="stockprice",
tags=tags,
)
myapi.add_endpoint(
http="GET",
policies=[database.crud_policy_arn],
filename="path/to/my/zipfile.zip",
environement={"DATABASE_NAME": "db", "TABLE_NAME": "stockprice"},
timeout=20,
)
api.finalize()