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?