授予AWS Api网关权限以使用BOTO3调用Lambda函数 [英] Giving AWS Api Gateway Permission To Invoke Lambda Function using BOTO3

查看:157
本文介绍了授予AWS Api网关权限以使用BOTO3调用Lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BOTO3创建一个调用lambda函数的Api Gateway方法.到目前为止,我一直找不到如何授予必要的权限.

I am attempting to use BOTO3 to create an Api Gateway method that invokes a lambda function. I have so far been unable to find how to grant the necessary permissions.

奇怪的是,通过AWS控制台手动设置lambda方法名称会自动设置权限.我无法在代码中复制它.

Curiously, setting the lambda method name manually through the AWS console sets up permissions automatically. I have been unable to replicate this in code.

这是我用来设置网关的代码:

This is the code I am using to set up the gateway:

# Create a rest api
self.rest_api = self.apigateway.create_rest_api(
    name='AWS_CMS_Operations'
)

# Get the rest api's root id
root_id = self.apigateway.get_resources(
    restApiId=self.rest_api['id']
)['items'][0]['id']

# Create an api resource
api_resource = self.apigateway.create_resource(
    restApiId=self.rest_api['id'],
    parentId=root_id,
    pathPart='AWS_CMS_Manager'
)

# Add a post method to the rest api resource
api_method = self.apigateway.put_method(
    restApiId=self.rest_api['id'],
    resourceId=api_resource['id'],
    httpMethod='POST',
    authorizationType='NONE'
)

# Add an integration method to the api resource
self.apigateway.put_integration(
    restApiId=self.rest_api['id'],
    resourceId=api_resource['id'],
    httpMethod='POST',
    type='AWS',
    integrationHttpMethod='POST',
    uri=self.create_api_invocation_uri()
)

# Set the put method response for the api resource
self.apigateway.put_method_response(
    restApiId=self.rest_api['id'],
    resourceId=api_resource['id'],
    httpMethod='POST',
    statusCode='200',
    responseModels={
        'application/json': 'Empty'
    }
)

# Set the put integration response for the api resource
self.apigateway.put_integration_response(
    restApiId=self.rest_api['id'],
    resourceId=api_resource['id'],
    httpMethod='POST',
    statusCode='200',
    responseTemplates={
        'application/json': ''
    }
)

# Create a deployment of the rest api
self.apigateway.create_deployment(
    restApiId=self.rest_api['id'],
    stageName='prod'
)

# Give the api deployment permission to trigger the lambda function
self.lmda.add_permission(
    FunctionName=self.lmda_function['FunctionName'],
    StatementId='apigateway-production-aws-cms',
    Action='lambda:InvokeFunction',
    Principal='apigateway.amazonaws.com',
    SourceArn=self.create_api_permission_uri(api_resource)
)

一切正常,除了为网关调用lambda设置了适当的权限之外.

Everything works fine with the exception of the proper permission being set for the gateway to invoke lambda.

推荐答案

From section 3.6 in this tutorial is a sample CLI command:

$ aws lambda add-permissionn \
--function-name <function-name> \
--statement-id apigateway-test-2 \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "<method-arn">

应该足够直接,才能翻译成Boto3.

Should be straight forward enough to translate to Boto3.

这篇关于授予AWS Api网关权限以使用BOTO3调用Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆