【发布时间】:2023-03-22 01:37:01
【问题描述】:
我需要创建一个 API 端点,它将触发一个 Lambda 函数并从 S3 存储桶返回一个图像。
示例网址:https://abc.execute-api.eu-west-1.amazonaws.com/dev/xyz/00/01/23911414.jpeg
我使用 Web 控制台手动创建了一个 APIGateway 实例,它运行良好。 我使用 CloudFormation 创建了相同的(我猜)但它不起作用。
Lambda 被触发,但它没有获得event 中的path 参数。
但我希望 Lambda 函数将 /xyz/00/01/23911414.jpeg 作为 event['path']。
这是我的 CloudFormation 模板的一部分:
RestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Description: Example API Gateway
EndpointConfiguration:
Types:
- REGIONAL
Name: imaginary-api
ProxyResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref RestApi
ParentId: !GetAtt
- RestApi
- RootResourceId
PathPart: '{proxy+}'
ProxyResourceANY:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref RestApi
ResourceId: !Ref ProxyResource
HttpMethod: ANY
AuthorizationType: NONE
MethodResponses:
- StatusCode: 200
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ImaginaryLambda.Arn}/invocations
Credentials: !GetAtt ApiGatewayIamRole.Arn
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
"image/jpeg": ""
"image/jpg": ""
IntegrationResponses:
- StatusCode: 200
RestAPIDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- ProxyResource
Properties:
RestApiId: !Ref RestApi
StageName: dev
ImaginaryInvoke:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt ImaginaryLambda.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:abc/dev
这是我第一次使用 APIGateway,我可能在这里做错了什么。任何帮助将不胜感激。
更新:
将RequestParameters 添加到方法中也不起作用。
RequestParameters:
method.request.path.proxy: true
【问题讨论】:
标签: amazon-web-services aws-lambda proxy amazon-cloudformation aws-api-gateway