Serverless-application-model: "API Stage not found" in AWS::ApiGateway::UsagePlan resource when changing API Stage name

Created on 14 Apr 2020  路  4Comments  路  Source: aws/serverless-application-model

Description:
I have the below API and Usage Plan resources created in my SAM template:

Parameters:
  ApiStageName:
    Type: String

ManagementApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: "my-api"
      StageName: !Ref ApiStageName

UsagePlanForManagementApi:
    Type: AWS::ApiGateway::UsagePlan
    DependsOn: [ "ManagementApi" ]
    Properties:
        ApiStages:
            - ApiId: !Ref ManagementApi
              Stage: !Ref ApiStageName
        Description: "Management API Usage Plan"
        Quota:
            Limit: 100
            Period: DAY
        Throttle:
            BurstLimit: 50
            RateLimit: 100
        UsagePlanName: "my-api-usage-plan"

The above resource get deployed just fine and without any issues.

However, when I change the value of the ApiStageName parameter from v1 to v2, and try to run a sam deploy, the update with fail with the below error:

UPDATE_IN_PROGRESS                        AWS::ApiGateway::RestApi        ManagementApi        -
UPDATE_ROLLBACK_IN_PROGRESS               AWS::CloudFormation::Stack      dev-management-api-sam          The following resource(s) failed to update: [UsagePlanForManagementApi].
UPDATE_FAILED                             AWS::ApiGateway::UsagePlan      UsagePlanForManagementApi 
                                                                                                           API Stage not found: API_ID:v2 
                                                                                                           (Service: AmazonApiGateway; Status Code: 404; 
                                                                                                           Error Code: NotFoundException; Request ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxx)

It looks like there's some race condition going on here. I have used DependsOn on the Usage Plan resource to depend on the API resource but it does not seem make any difference.

Any idea how this can be fixed? Is this a bug in SAM?

Steps to reproduce the issue:

  1. Create the above resources and initially set the value of the ApiStageName parameter to v1
  2. Deploy
  3. Update the value of the ApiStageName parameter from v1 to v2
  4. Deploy

Observed result:
The deployment fails with the above error when updating the value of the ApiStageName parameter to v2

Expected result:
The deployment should not fail with the above error

I came across issue #32 which has been closed but I didn't see a solution. Also, that issue is from 2 years ago so I'm hoping a fix has been released since then.

Thank you

Most helpful comment

@keetonian for some reason, the DependsOn solution didn't work for me: I had to remove the dot in the resource name.

  UsagePlanForManagementApi:
    Type: AWS::ApiGateway::UsagePlan
    DependsOn:
-      - ManagementApi.Stage
+      - ManagementApiStage

All 4 comments

For your current setup, it looks like you need to depend on the Stage resource that SAM generates rather than the API.

  UsagePlanForManagementApi:
    Type: AWS::ApiGateway::UsagePlan
    DependsOn:
      - ManagementApi.Stage

On a side note, have you considered using the UsagePlan feature in SAM? The syntax for this same example you posted would be the following:

Parameters:
  ApiStageName:
    Type: String

Resources:
  ManagementApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: "my-api"
      StageName: !Ref ApiStageName
      Auth:
        UsagePlan:
          CreateUsagePlan: PER_API
          UsagePlanName: "my-api-usage-plan"
          Quota:
            Limit: 100
            Period: DAY
          Throttle:
            BurstLimit: 50
            RateLimit: 100
          Description: "Management API Usage Plan"

SAM handles the depends on and other resource dependencies for you in this case.

@keetonian for some reason, the DependsOn solution didn't work for me: I had to remove the dot in the resource name.

  UsagePlanForManagementApi:
    Type: AWS::ApiGateway::UsagePlan
    DependsOn:
-      - ManagementApi.Stage
+      - ManagementApiStage

I had same issue and this works for me.

DependsOn: 'ApiGatewayDeployment${sls:instanceId}'

I had same issue and this works for me.

DependsOn: 'ApiGatewayDeployment${sls:instanceId}'

@duc2019 isn't that from the serverless framework? This is AWS SAM we're talking about.

Was this page helpful?
0 / 5 - 0 ratings