Skip to content

Instantly share code, notes, and snippets.

@rujmah
Last active January 25, 2019 09:16
Show Gist options
  • Save rujmah/dfcd0ee8054d94d093ecea0c1bd334a1 to your computer and use it in GitHub Desktop.
Save rujmah/dfcd0ee8054d94d093ecea0c1bd334a1 to your computer and use it in GitHub Desktop.
AWS API Gateway that calls an single endpoint Lambda with GET

AWS API Gateway that calls an single endpoint Lambda with GET

Pertinent info

Ensure IntegrationHttpMethod is set to POST even if you want to use a GET (or whatever)

e.g.

  rootMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizationType: NONE
      HttpMethod: GET
      Integration:
        IntegrationHttpMethod: POST
        Type: AWS_PROXY
        Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
      ResourceId: !GetAtt apiGateway.RootResourceId
      RestApiId: !Ref apiGateway

See this thread: https://forums.aws.amazon.com/thread.jspa?messageID=745275

Background

When attempting to set up an API Gateway with a single endpoint that used a Lambda we continually got the following error message when attempting to use GET methods:

<AccessDeniedException>
  <Message>Unable to determine service/operation name to be authorized</Message>
</AccessDeniedException>

This was apparently due to the fact that regardless of the actual HttpMethod we set, IntegrationHttpMethod needs to be set to POST as per this piece of AWS documentation:

https://docs.amazonaws.cn/en_us/apigateway/latest/developerguide/set-up-lambda-custom-integrations.html

For Lambda integrations, you must use the HTTP method of POST for the integration request, according to the specification of the Lambda service action for function invocations.

AWSTemplateFormatVersion: "2010-09-09"
Description: Test API G setup
Parameters:
lambdaArn:
Type: "String"
Default: arn:aws:lambda:eu-west-1:xxxxxxxxxx:function:xxxxxxxxxx
Resources:
apiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: rjjmTestApiGateway
Description: "Test API Gateway"
rootMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: GET
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
ResourceId: !GetAtt apiGateway.RootResourceId
RestApiId: !Ref apiGateway
deployment:
Type: AWS::ApiGateway::Deployment
DependsOn: rootMethod
Properties:
RestApiId: !Ref apiGateway
StageName: v1
lambdaApiGatewayInvoke:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref lambdaArn
Principal: apigateway.amazonaws.com
SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment