Amazon web services 使用boto3创建aws lambda集成api网关资源
正如标题所示,我试图用一个方法创建一个资源,该方法通过Amazon web services 使用boto3创建aws lambda集成api网关资源,amazon-web-services,aws-lambda,aws-api-gateway,boto3,Amazon Web Services,Aws Lambda,Aws Api Gateway,Boto3,正如标题所示,我试图用一个方法创建一个资源,该方法通过boto3python库触发lambda函数 我在做下面的事情 首先,我创建资源 new_endpoint_response = aws_apigateway.create_resource( restApiId = 'xxxxxxxx', parentId = 'xxxxxxxx', pathPart = event['Configure'] ) 然后是post方法 put_meth
boto3
python库触发lambda函数
我在做下面的事情
首先,我创建资源
new_endpoint_response = aws_apigateway.create_resource(
restApiId = 'xxxxxxxx',
parentId = 'xxxxxxxx',
pathPart = event['Configure']
)
然后是post方法
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'
)
这就是我遇到的一些问题。当我试着做最后一步时,我总是
调用PutIntegration操作时发生错误(BadRequestException):用于集成的AWS ARN必须包含路径或操作
我不知道这是为什么。根据我的搜索,所需的uri实际上是api调用url,问题是我不知道这意味着什么,也不知道如何获得它。所示示例如下
arn:aws:apigateway:${aws::Region}:lambda:path/2015-03-31/functions/${LambdaFunctionAPI.arn}/invocations
如何使调用URL调用我想要的lambda函数,以及如何获得该调用URL?Woohoo。这已经一岁了,所以你可能找到了另一种方法。但是我要把这个放在这里给下一个来的人。AWS目前不是我最喜欢的 首先,结合使用以下三种方法来解决问题:
boto3
函数创建一个lambda函数,这是我进入第二步的原因:lambda\u client=boto3.client(“lambda”)
新建\u lambda=lambda\u client.create\u函数(
FunctionName='HelloWorld',
Runtime='nodejs12.x',
Role='arn:aws:iam:::Role/HelloWorld',
Handler='Handler.Handler',
代码={'ZipFile':},
Description='Hello World Function',
Publish=True,
)
#然后,您可以在此处获得至少部分调用uri:
打印(新λ['FunctionArn'])
#创建RESTAPI
rest\u api=api\u client.create\u rest\u api(
name='GreatApi'
)
打印(rest_api)
rest_api_id=rest_api[“id”]
#获取RESTAPI的根id
root\u resource\u id=api\u client.get\u resources(
restApiId=rest\u api\u id
)['items'][0]['id']
#创建一个api资源
api\U资源=api\U客户端。创建\U资源(
restApiId=rest\u api\u id,
parentId=根资源id,
pathPart='问候语'
)
api_资源\u id=api_资源['id']
#向RESTAPI资源添加post方法
api_方法=api_客户端.put_方法(
restApiId=rest\u api\u id,
resourceId=api\u资源\u id,
httpMethod='GET',
authorizationType='NONE',
请求参数={
'method.request.querystring.greeter':False
}
)
打印(api_方法)
put\u method\u res=api\u client.put\u method\u response(
restApiId=rest\u api\u id,
resourceId=api\u资源\u id,
httpMethod='GET',
状态代码='200'
)
打印(放置方法)
#uri来自一个基本lambda字符串,该字符串附带函数ARN
arn_uri=“arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1::function:HelloWorld/invocations”
put\u integration=api\u client.put\u integration(
restApiId=rest\u api\u id,
resourceId=api\u资源\u id,
httpMethod='GET',
type='AWS',
integrationHttpMethod='POST',
uri=arn\u uri
凭据='arn:aws:iam:::角色/HelloWorldRole',
请求模板={
“application/json”:“{\“greeter\”:\“$input.params('greeter')\”}”
},
)
打印(put_集成)
put\u integration\u response=api\u client.put\u integration\u response(
restApiId=rest\u api\u id,
resourceId=api\u资源\u id,
httpMethod='GET',
statusCode='200',
selectionPattern=''
)
打印(输入\集成\响应)
#此位设置在创建的apigateway基础上构建的阶段“dev”
#它看起来像这样:
# https://.execute-api..amazonaws.com/dev
部署=api_客户端。创建_部署(
restApiId=rest\u api\u id,
stageName='dev',
)
#完成所有这些之后,我们可以发送一个请求来调用lambda函数
# https://123456.execute-api.us-east-1.amazonaws.com/dev?greeter=John
打印(部署)
更复杂一点,但我认为没有apiGateway,您实际上无法生成资源。
一旦我有时间,我将构建一个无服务器回购示例。感谢您提供的详细代码!让我大吃一惊的是在“put_集成”中,我将AWS文档解释为使用httpMethod='POST',但正确的方法(对于我的GET Api端点)是使用httpMethod='GET',integrationHttpMethod='POST'
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'])
# 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)