首页 > 解决方案 > 使用 boto3 创建 aws lambda 集成 api 网关资源

问题描述

正如标题所示,我试图使用一种方法创建一个资源,该方法使用boto3python 库触发 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 调用 url,问题是我不知道这意味着什么或如何获取它。显示的示例如下。

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

我怎样才能使调用 URL 调用我想要的 lambda 函数,我什至如何获得该调用 url?

标签: amazon-web-servicesaws-lambdaaws-api-gatewayboto3

解决方案


呜呼。这是一岁,所以你可能找到了另一种方法。但我要把这个放在这里,以供下一个出现的人使用。AWS 目前不是我最喜欢的r/unpopularopinion

首先,通过使用这三个的组合来解决这个问题:

  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,您实际上无法制作资源。一旦有时间,我将构建一个示例无服务器存储库。


推荐阅读