AWS API网关路由是一种管理和分发API请求的服务,可以将请求路由到不同的后端服务或Lambda函数。下面是一个使用AWS API网关路由的代码示例:
import boto3
client = boto3.client('apigateway')
response = client.create_rest_api(
name='MyAPI',
description='My API Gateway'
)
rest_api_id = response['id']
response = client.create_resource(
restApiId=rest_api_id,
parentId='xxxxxx', # 父资源ID,如果是根资源则为'/',可以通过get_resources方法获取
pathPart='myresource'
)
resource_id = response['id']
response = client.put_method(
restApiId=rest_api_id,
resourceId=resource_id,
httpMethod='GET',
authorizationType='NONE'
)
uri = 'http://mybackend.com/myendpoint' # 后端服务的URI
response = client.put_integration(
restApiId=rest_api_id,
resourceId=resource_id,
httpMethod='GET',
type='HTTP',
integrationHttpMethod='GET',
uri=uri
)
response = client.create_deployment(
restApiId=rest_api_id,
stageName='prod'
)
以上代码示例演示了如何创建API网关、资源和方法,并将请求路由到后端服务。你可以根据自己的需求对代码进行修改和扩展。