2

Description My application uses cognito user pools for identity management and cognito authorizer on API Gateway.

  • Once the user signs in (using aws amplify), a lambda function is triggered, which generates an API Key, adds it to the API Usage Plan against the cognito user and also adds it in a custom field in cognito.
  • Using the custom field (which is the API Key), I want to call an API endpoint (configured on the gateway with cognito authorizer).

Problem

  • Upon user signup/login, I get the custom field which is the API Key on client side and then immediately, I call an endpoint passing the key in headers (x-api-key).
  • But, the API Gateway takes time to recognise the API Key from the usage plan (and shows the error "Unable to find the key in Usage Plan" in cloudwatch logs).
  • Hence, on immediate use, it throws CORS error (which isn't actually the right error, because I've configured cross-origin header to *). The actual reason might be the one seen in cloudwatch logs.
  • After a delay of around 25-30 seconds, I am able to access the API Endpoint without any error.
  • I am unable to figure out what am I doing wrong and why is it taking time for API Gateway to populate the API key internally?
  • I've read about issues (Api Gateway Api Key immediate use upon creation giving forbidden) and (How long should I wait after applying an AWS IAM policy before it is valid?) earlier and they state that it's due to the globally distributed nature and hence the delay.
  • But, for others the delay is between 3-5 seconds, while in my case it's 25-30 seconds which is my main concern as it causes the user to wait until the API's are accessible on the page in my application.
  • What is the workaround or solution to this, and is my approach of this whole mechanism is correct or am I missing something?
Huddle01
  • 21
  • 1
  • 1
    Can you confirm if it is a cold start issue? What happens if you immediately do the same thing again? Does it still take 25-30 seconds? – Ninad Gaikwad Jul 24 '21 at 03:55
  • I guess it isn't a cold start issue. By same thing, do you mean the process of generating api key or calling the api using the obtained api key? For the former one, once it is completed, I can see it added in the usage plan immediately on the AWS console. For the latter one, currently I constantly ping the endpoint every 1 second until the API Gateway doesn't throws the forbidden error in response (hence reached to the 25-30 second number). Does this help? – Huddle01 Jul 26 '21 at 16:20

0 Answers0