gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:28:00 27 4
gpt4 key购买 nike

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

我在做以下事情。

首先,我创建资源

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?

最佳答案

呜呼。这是一岁,所以你可能找到了另一种方式。但我要把它放在这里,供下一个出现的人使用。 AWS 目前不是我的最爱 r/unpopularopinion首先,通过使用这三个的组合来弄清楚:

  • Boto3 docs
  • Lambda Configurations
  • This SO post
  • 首先必须存在一个 lambda 函数,您可以通过 aws CLI 、GUI 创建一个 lambda 函数或使用 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'])
  • 使用 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,您实际上无法制作资源。
    一旦我得到一分钟,我将构建一个示例无服务器存储库。

    关于amazon-web-services - 使用 boto3 创建 aws lambda 集成 api 网关资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58859917/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com