使用boto3创建aws-lambda集成api网关资源



正如标题所示,我试图用一个方法创建一个资源,该方法可以用boto3python库触发lambda函数。

我正在做以下事情。

首先,我创建资源

new_endpoint_response = aws_apigateway.create_resource(
restApiId = 'xxxxxxxx',
parentId = 'xxxxxxxx',
pathPart = event['Configure']
)

然后,后方法

put_method_response = aws_apigateway.put_method(
restApiId = 'xxxxxxxxxxx',
resourceId = new_endpoint_response['id'],
httpMethod = 'POST',
authorizationType = 'NONE'
)

最后,用给这个方法加一个lambda函数

aws_apigateway.put_integration(
restApiId = 'xxxxxxxxxx',
resourceId = new_endpoint_response['id'],
httpMethod = 'POST', 
integrationHttpMethod = 'POST',
type = 'AWS',
uri = 'LAMBDA ARN'
)

这就是我遇到的一些问题。当我尝试做最后一步时,我总是得到

An error occurred (BadRequestException) when calling the PutIntegration operation: AWS ARN for integration must contain path or action

我不知道为什么。从我搜索到的内容来看,所需的uri实际上是api调用url,问题是我不知道这意味着什么,也不知道如何获得它。下面的示例如下。

arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunctionAPI.Arn}/invocations

如何使调用URL调用我想要的lambda函数,以及如何获取该调用URL?

哇。这已经一年了,所以你可能找到了另一种方法。但我会把这个放在这里,留给下一个来的人。AWS目前不是我的最爱r/unpopularopinion

首先,通过使用以下三种方法的组合来计算:

  • Boto3文档
  • Lambda配置
  • 此SO帖子
  1. lambda函数必须首先存在,您可以通过aws CLI、GUI或使用boto3函数创建一个函数来实现这一点,这就是我进入第二步的原因:
lambda_client = boto3.client('lambda')
new_lambda = lambda_client.create_function(
FunctionName='HelloWorld',
Runtime='nodejs12.x',
Role='arn:aws:iam::<user_id>:role/HelloWorld',
Handler='handler.handler',
Code={ 'ZipFile': <zip_file_object_from_s3> },
Description='Hello World Function',
Publish=True,
)
# You can then get at least part of the invocation uri here:
print(new_lambda['FunctionArn'])
  1. 使用boto3创建Rest Api
# Create rest api
rest_api = api_client.create_rest_api(
name='GreatApi'
)
print(rest_api)
rest_api_id = rest_api["id"]
# Get the rest api's root id
root_resource_id = api_client.get_resources(
restApiId=rest_api_id
)['items'][0]['id']

# Create an api resource
api_resource = api_client.create_resource(
restApiId=rest_api_id,
parentId=root_resource_id,
pathPart='greeting'
)
api_resource_id = api_resource['id']
# Add a post method to the rest api resource
api_method = api_client.put_method(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
authorizationType='NONE',
requestParameters={
'method.request.querystring.greeter': False
}
)
print(api_method)
put_method_res = api_client.put_method_response(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
statusCode='200'
)
print(put_method_res)
# The uri comes from a base lambda string with the function ARN attached to it
arn_uri="arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<user_id>:function:HelloWorld/invocations"

put_integration = api_client.put_integration(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
type='AWS',
integrationHttpMethod='POST',
uri=arn_uri
credentials='arn:aws:iam::<user_id>:role/HelloWorldRole',
requestTemplates={
"application/json":"{"greeter":"$input.params('greeter')"}"
},
)
print(put_integration)
put_integration_response = api_client.put_integration_response(
restApiId=rest_api_id,
resourceId=api_resource_id,
httpMethod='GET',
statusCode='200',
selectionPattern=''
)
print(put_integration_response)
# this bit sets a stage 'dev' that is built off the created apigateway
# it will look something like this:
# https://<generated_api_id>.execute-api.<region>.amazonaws.com/dev
deployment = api_client.create_deployment(
restApiId=rest_api_id,
stageName='dev',
)
# all that done we can then send a request to invoke our lambda function
# https://123456.execute-api.us-east-1.amazonaws.com/dev?greeter=John
print(deployment)

更多的参与,但我认为如果没有apiGateway,你就无法真正制作资源。一旦我有时间,我将构建一个无服务器的repo示例。

最新更新