aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[@aws-cdk/aws-apigateway] Getting 429 from APIGateway when trying to deploy the stack

Open podoprigo opened this issue 6 years ago • 10 comments

:question: General Issue

The Question

I'm forced to use older version of CDK constructs (1.32.2), in combination with OpenAPI rest API definition. As a result, I can't use latest SpecRestApi.

I'm tryin to achieve similar results, by combining sam.CfnApi with apigateway.Cfn constructs. When I'm trying to deploy the stack, I keep getting errors like this:

Invalid request provided: Too Many Requests (Service: ApiGateway, Status Code: 429, Request ID: XXXXXX, Extended Request ID: null)

Though experimentation, I figured out that this is the code that causes the error:

    const apigDomain = new apig.CfnDomainName(this, 'DomainName', {
      regionalCertificateArn: props.certificate.certificateArn,
      domainName: props.domain,
      endpointConfiguration: {
        types: ['REGIONAL'],
      },
    });
    api.addDependsOn(apigDomain);

Without it, CfnApi is created just fine. Surprinsingly I cannot find any mentions of CloudFormation getting throttled by APIGateway. Any hints?

Environment

  • CDK CLI Version: 1.51.0
  • Module Version: 1.32.2
  • Node.js Version: 12
  • OS: all
  • Language (Version): TypeScript (3.8.3)

podoprigo avatar Aug 07 '20 18:08 podoprigo

I don't know if this is a new bug on CloudFormation's and API Gateway's side, as a CF stack that was deploying successfully for us, stopped working with the same error:

Invalid request provided: Too Many Requests (Service: ApiGateway, Status Code: 429, Request ID: 17f0fe0f-7974-490d-b728-adf86597357f, Extended Request ID: null)

The resource in the template triggering that error is a custom domain:

CustomDomainName:
    Type: 'AWS::ApiGateway::DomainName'
    Properties:
      CertificateArn: !Ref CertificateArn
      DomainName: !If [ IsProd, !Join [ '', [ 'api.', !ImportValue HostedZoneName ] ], !Join [ '', [ !Ref Stage, 'api.', !ImportValue HostedZoneName ] ]  ]

Let me know if the full template is useful for this but I think it points to the same issue from @podoprigo

federicorinaldi avatar Aug 09 '20 19:08 federicorinaldi

FWIW I got the same bug and google searching brought me here. I'm not even using CDK so it seems like a CloudFormation bug. Here's the snippet of my SAM template:

  "Globals": {
    "Api": {
      "OpenApiVersion": "3.0.1",
      "EndpointConfiguration": "REGIONAL",
      "Domain": {
        "CertificateArn": { "Ref": "DomainCertificateArn" },
        "DomainName": { "Ref": "DomainName" },
        "Route53": {
          "HostedZoneId": { "Ref": "DomainZoneId" }
        },
        "EndpointConfiguration": "REGIONAL",
        "BasePath": [ "/app" ]
      }
    },
...

I guess the upside is that the error was intermittent. A redeploy resulted in success. /shrug

Also, only reason I'm using globals is because CloudFormation kept giving an error about a missing StageName when I declared a Serverless::Api directly even though I had a StageName in the Api. /shrug

Seems like more than a few CloudFormation bugs are sneaking through as of late.

mikerochip avatar Aug 10 '20 06:08 mikerochip

Unfortunately, this seems to be an issue with API Gateway. Please reach out to AWS Support regarding this.

nija-at avatar Aug 10 '20 13:08 nija-at

Related to this issue in CloudFormation: https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/588.

So you need to ensure a 30 seconds window between each API call.

eduardomourar avatar Aug 10 '20 14:08 eduardomourar

In my case, we deployed the same CF Stack multiple times (over 20 times already) as we use it as the base template for new accounts and we never had this issue so we are pointing to a bug or an issue as pointed out by @nija-at

federicorinaldi avatar Aug 10 '20 14:08 federicorinaldi

This issue is not related to CDK or CloudFormation. After checking this issue, the same error message "Too Many Requests" will also display on AWS console if you want to change the settings to two Custom Domain and the timing is too close. Even the CloudFormation status of Custom Domain resource is "UPDATE_COMPLETE", you could check the status of Custom Domain in AWS console's API Gateway page and the status is still "UPDATING". On the other hand, adding DependsOn in CloudFormation is not able to resolve this situation. As a result, if you put two Custom Domain resource in the same CloudFormation and you want to change both settings of Custom Domain, you will need to change them one by one. After deploying first Custom Domain settings you see the Status of first Custom Domain is change to "AVAILABLE", and then you could start to change the settings of second Custom Domain.

rachsu avatar Sep 09 '20 06:09 rachsu

As a result, if you put two Custom Domain resource in the same CloudFormation and you want to change both settings of Custom Domain, you will need to change them one by one

It works when the stack already exists. What about expansion in a new region - do we need to put some hacky flags in place to only create one domain first? This is not logical, and CF must be able to handle retries or add an ability to actually 'sleep' in between resources.

podoprigo avatar Sep 09 '20 16:09 podoprigo

Unfortunately, this seems to be an issue with API Gateway. Please reach out to AWS Support regarding this.

No it's not a problem with APIG. Their API limits are clearly stated here: https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html. The problem is that neither CDK nor CF currently have a solution for that. Which results in flaky stack deployments (for example, a stack with 2 APIG domains may succeed on deployment, but will fail on deletion, just because of bad timing).

podoprigo avatar Sep 09 '20 16:09 podoprigo

you can find a proposed solution here what will add a delay in between resources: https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/589

eduardomourar avatar Sep 09 '20 17:09 eduardomourar

I understand there are limits to what aws-cdk developers can do to resolve issues with other AWS service owners but I've seen a few different issues on this project where the same developer has closed and communicated poorly with the community. You have to realize that there are paying AWS customers behind these issues and as an AWS employed developer you can act as a bridge between cdk and cloudformation which is a completely opaque AWS service. It's extremely frustrating to see multiple issues about the same problem being closed prematurely.

In fact, I am currently working on a cdk project that was created and developed by AWS Proserve which is producing this issue. This is embarrassing to say the least.

codernaive avatar Dec 23 '21 20:12 codernaive