serverless-localstack icon indicating copy to clipboard operation
serverless-localstack copied to clipboard

apiKeys/usagePlan not supported

Open sesteva opened this issue 5 years ago • 1 comments
trafficstars

I have a function that required an apiKey. The gateway's endpoint exists but it fails

"The security token included in the request is invalid."

Looking at Moto I noticed I have a path to retrieve apiKeys and usagePlan. It returns an empty array on both counts. It seems none are created when I run sls deploy --stage local. In AWS (sls deploy --stage dev) it works fine.

I exported the cloudformaiton stack output when running local.

So here are the defects/questions:

**1. Are apiKets and usagePlan supported?

  1. Why is a function created in AWS when I run it locally**

Here is the stack-local.json

{
  "ServerlessDeploymentBucketName": "hello-local-serverlessdeploymentbucket-1zocaat71p",
  "EnterpriseLogAccessIamRole": "arn:aws:iam::000000000000:role/cf-hello-local-7d29cfc90000ecf5ca94c1ef30a842",
  "ChanceLambdaFunctionQualifiedArn": "ChanceLambdaVersionQw0GRKWEP40hVpdfVpuID54OcxS6K90mUSWN78",
  "RegistrarLambdaFunctionQualifiedArn": "RegistrarLambdaVersiongB1BIRyX6CwDq2fJUTCkt6VKa5e7BLwuLM1SLE",
  **"ServiceEndpoint": "https://aahxs5c0n.execute-api.us-east-1.amazonaws.com/local"**
}

Here is the serverless.yml

tenant: pepe
app: hello
service: hello

plugins:
  - serverless-pseudo-parameters
  - serverless-stack-output
  - serverless-localstack

provider:
  name: aws
  runtime: nodejs12.x
  iamRoleStatements:
    - Effect: Allow
      Action: dynamodb:scan
      Resource: arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/transactions
    - Effect: Allow
      Action: lambda:InvokeFunction
      Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello-dev-registrar
  apiKeys:
    - free:
        - myFreeKey
        - ${opt:stage}-myFreeKey
    - paid:
        - myPaidKey
        - ${opt:stage}-myPaidKey
  usagePlan:
    - free:
        quota:
          limit: 5000
          offset: 2
          period: MONTH
        throttle:
          burstLimit: 200
          rateLimit: 100
    - paid:
        quota:
          limit: 50000
          offset: 1
          period: MONTH
        throttle:
          burstLimit: 2000
          rateLimit: 1000

functions:
  chance:
    handler: functions/chance/index.handler
    events:
      - http:
          path: /chance/
          method: post
          private: true
          cors: true
  registrar:
    handler: functions/registrar.handler
    environment:
      transactions_table: transactions

resources:
  Resources:
    transactionsTables:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: transactions
        AttributeDefinitions:
          - AttributeName: game_id
            AttributeType: S
        KeySchema:
          - AttributeName: game_id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

custom:
  output:
    file: .build/stack-${opt:stage}.json
  localstack:
    debug: true
    stages:
      - local

┆Issue is synchronized with this Jira Task by Unito

sesteva avatar Jun 28 '20 16:06 sesteva

One more thing to add, is that localStack did not capture that api key is required; even when I set private as true.

❯ awslocal apigateway get-resources --rest-api-id wxjhxs5c0n

          "POST": {
                    "httpMethod": "POST",
                    "authorizationType": "NONE",
                    "apiKeyRequired": false,
                    "methodIntegration": {
                        "type": "AWS_PROXY",
                        "httpMethod": "POST",
                        "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:hello-local-chance/invocations",
                        "passthroughBehavior": "WHEN_NO_MATCH",
                        "cacheNamespace": "39312675",
                        "cacheKeyParameters": [],
                        "integrationResponses": {
                            "200": {
                                "statusCode": 200,
                                "responseTemplates": {
                                    "application/json": null
                                }
                            }
                        }
                    }
                }
            }

sesteva avatar Jun 28 '20 22:06 sesteva

Hi @sesteva,

apologies for the late response. Could you please let us know if you still encounter this issue with the latest localstack version?

steffyP avatar Oct 13 '23 17:10 steffyP

This issue seems to be stale. As we didn't get any response in two months, and the original report is from 2020, we are closing this issue now. Please feel free to reopen if it still persists, and provide updates logs and details.

steffyP avatar Dec 15 '23 10:12 steffyP