在Amazon API Gateway中,"指定的URI无效的HTTP端点"错误通常发生在尝试创建或配置API Gateway资源时。以下是一些可能的解决方法:
检查URI格式:确保在URI中使用正确的URL格式。例如,确保URI以"http://"或"https://"开头,并且包含正确的主机名和路径。
检查URI路径:确保URI路径正确。例如,如果您在URI中指定了路径,则确保路径正确,并且API Gateway可以在指定路径上找到相应的资源。
检查URI参数:如果您在URI中包含参数,则确保参数正确,并且符合API Gateway预期的格式。您可以使用模板语法在API Gateway中定义参数,例如{proxy+}表示匹配任何路径。
以下是一个使用AWS SDK for Python(Boto3)的示例代码,用于创建一个使用Lambda函数作为后端的API Gateway资源:
import boto3
# 创建API Gateway客户端
client = boto3.client('apigateway')
# 创建一个API
api_response = client.create_rest_api(
name='MyAPI',
description='My API Gateway'
)
api_id = api_response['id']
# 创建资源
resource_response = client.create_resource(
restApiId=api_id,
parentId='root',
pathPart='myresource'
)
resource_id = resource_response['id']
# 创建方法
method_response = client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod='GET',
authorizationType='NONE',
)
method_response = client.put_method_response(
restApiId=api_id,
resourceId=resource_id,
httpMethod='GET',
statusCode='200',
)
# 配置Lambda集成
integration_response = client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='GET',
integrationHttpMethod='POST',
type='AWS',
uri='arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1::function:/invocations'
)
# 部署API
deployment_response = client.create_deployment(
restApiId=api_id,
stageName='prod'
)
print("API Gateway created successfully")
在上面的示例代码中,您需要将和替换为您的Lambda函数的ARN和名称。然后,运行代码将创建一个名为"MyAPI"的API Gateway资源,并将其与指定的Lambda函数集成。