Я пытаюсь использовать BOTO3 для создания метода Api Gateway, который вызывает функцию лямбда. Я до сих пор не смог найти, как предоставить необходимые разрешения.Предоставление разрешения AWS Api Gateway для вызова функции Lambda с использованием BOTO3
Любопытно, что установка имени метода лямбда вручную через консоль AWS автоматически устанавливает разрешения. Я не смог воспроизвести это в коде.
Это код, я использую для настройки шлюза:
# 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)
)
Все отлично работает с исключением надлежащего разрешения, определенными для шлюза для вызова лямбды.
Это существует в коде в моем вопросе. В шлюзе api все еще запрещен доступ. –
Мой плохой, не видел этого. Можете ли вы также предоставить источник для self.create_api_permission_uri (api_resource) –
Решил проблему. Это связано с ARN, который я создавал. Нужен шаблон в нем. –