使用Boto3创建AWS Lambda集成API网关资源

时间:2019-11-14 15:08:04

标签: amazon-web-services aws-lambda aws-api-gateway boto3

正如标题所示,我正在尝试使用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'
    )

这是我遇到一些问题的地方。当我尝试执行最后一步时,我总是得到

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

我不知道为什么会这样。根据我的搜索,所需的uri实际上是 api调用网址,问题是我不知道这是什么意思或如何获取它。显示的示例如下。

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

如何做到这一点,以便调用URL调用我想要的lambda函数,甚至如何获得该调用URL?

1 个答案:

答案 0 :(得分:0)

呜呼。今年一岁,所以您可能找到了另一种方法。但是,我将把它放在这里,供下一个出现的人使用。目前r/unpopularopinion

上我不是AWS的最爱

首先,结合使用以下三种方法来找出答案:

  1. 首先必须存在一个lambda函数,您可以通过aws CLI,GUI或使用boto3函数创建一个lambda函数来完成第二步:
 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,您实际上无法获得资源。 一分钟后,我将构建一个无服务器回购示例。