1. Managed AI APIs with Usage Plans in API Gateway


    To create a managed AI API with usage plans in AWS API Gateway, you would typically define an API Gateway REST API, deploy it to a stage, and then attach a usage plan to it. A usage plan specifies who can access one or more deployed API stages and methods, and also how much they can access them with quotas and rate limits.

    Here's how you can use Pulumi to provision these resources in AWS. This program consists of:

    1. API Definition: You need to define your API, which in this case will be done using Swagger or OpenAPI specifications.
    2. API Deployment: After defining the API resources, you deploy it to a stage where the API can be invoked.
    3. Usage Plan: You create a usage plan to manage throttling and quotas for the API consumers.
    4. API Keys: If you want to require API consumers to use an API key, you can create those in API Gateway and associate them with your usage plan.

    Let's examine each part in the Python code and how you can use Pulumi to deploy a Managed AI API with Usage plans:

    import pulumi import pulumi_aws as aws # Define an API Gateway REST API. rest_api = aws.apigateway.RestApi("my-api", body="""{ "swagger": "2.0", "info": { "title": "example", "version": "1.0" }, "paths": { "/example": { "get": { "x-amazon-apigateway-integration": { "uri": "arn:aws:apigateway:region:lambda:path/2015-03-31/functions/lambda_function_arn/invocations", "passthroughBehavior": "when_no_match", "httpMethod": "POST", "type": "aws_proxy" } } } } }""" ) # Define a deployment for the API, to make it callable over the internet. deployment = aws.apigateway.Deployment("api-deployment", rest_api=rest_api.id, # Pulumi auto-names the deployment with an auto-generated suffix for updates to be picked up. stage_name='prod', opts=pulumi.ResourceOptions(depends_on=[rest_api]), ) # Create a usage plan to manage access to the deployed API. usage_plan = aws.apigateway.UsagePlan("api-usage-plan", name='daily-usage-plan', throttle_settings={ 'rateLimit': 10, 'burstLimit': 2 }, quota_settings={ 'limit': 1000, 'period': 'DAY', 'offset': 2, }, api_stages=[{ 'apiId': rest_api.id, 'stage': deployment.stage_name, }] ) # If you want to require an API key for your API, # create an API key and associate with the usage plan. api_key = aws.apigateway.ApiKey("api-key", description="API Key for external clients", ) # Associate the API key with the usage plan. usage_plan_key = aws.apigateway.UsagePlanKey("usage-plan-key", key_id=api_key.id, key_type="API_KEY", usage_plan_id=usage_plan.id, ) # Export the endpoint of the API. pulumi.export('api_endpoint', deployment.invoke_url)
    • RestApi: Here we're defining the REST API with the "/example" endpoint. The body parameter describes the API structure in Swagger/OpenAPI format. The endpoint is set up to proxy requests to a lambda function (specified by its ARN).
    • Deployment: We create an API Gateway Deployment which makes our API callable. The stage_name parameter assigns the deployment to a particular stage environment (in this case, 'prod').
    • UsagePlan: This is how we implement rate limiting for our API. The throttle_settings control the request rate limits and the quota_settings specify the number of requests that can be made within a defined period.
    • ApiKey and UsagePlanKey: If you want to control and monitor who accesses your API, you can create API Keys and associate them with your Usage Plan.

    Please replace the uri in the 'x-amazon-apigateway-integration' section with the actual URI of the integration point for your managed AI API (e.g., a Lambda Function ARN).

    After you write this program in a __main__.py file, you can deploy it with Pulumi by running pulumi up command. Pulumi will provision the AWS resources specified, in the correct order, and output the endpoint URL after the deployment is complete.