Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable ApiKeyRequired for an Api Method?

I want to use the standard API Keys feature of API Gateway. If I use standard cloudformation this is possible by setting the property ApiKeyRequired to true for a method. How can I do this with SAM?

I tried using swagger but that does not seem to work:

    swagger: "2.0"
    info:
      title: !Ref AWS::StackName

    paths:
      "/machines/{resourceid}":
        get:
          parameters: 
            - name: resourceid 
              in: path 
              type: string 
              required: true 
          x-amazon-apigateway-integration:
            httpMethod: POST
            type: aws_proxy
            uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambda.Arn}/invocations
            responses: {}
          security:
            - authorizer: []

    securityDefinitions:
      authorizer:
        type: apiKey
        name: Authorization
        in: header

Any suggestions?

like image 348
user3492652 Avatar asked May 15 '18 07:05

user3492652


People also ask

How do I assign a user pool to API?

Choose (or create) a method on your API. Choose Method Request. Under Settings, choose the pencil icon next to Authorization. Choose one of the available Amazon Cognito user pool authorizers from the drop-down list.

How do I activate my API key?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.


1 Answers

The following swagger definition works:

  DefinitionBody:
    swagger: "2.0"
    info:
      title: !Ref AWS::StackName
    x-amazon-apigateway-api-key-source : "HEADER"
    paths:
      "/machines/{resourceId}":
        get:
          parameters: 
            - name: resourceId 
              in: path 
              type: string 
              required: true 
          x-amazon-apigateway-integration:
            httpMethod: POST
            type: aws_proxy
            uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MessagingServiceTestHandler.Arn}/invocations
            responses: {}
          security:
            - api_key: []                      
    securityDefinitions:
      api_key:
        type: "apiKey"
        name: "x-api-key"
        in: "header"

The name of the api key header must be x-api-key rather than the standard Authorization header.

like image 96
user3492652 Avatar answered Sep 24 '22 10:09

user3492652