chalice icon indicating copy to clipboard operation
chalice copied to clipboard

Add RestAPI.Stage ref to ApiGatewayCustomDomainMapping

Open chkothe opened this issue 4 years ago • 2 comments

This is intended to ensure that the mapping is created only after the stage exists (resolving a race condition in the CF template).

Issue #, if available:

The problem is explained in #1735 (the issue originally proposed another solution using DependsOn, but that's less clean since it refers to a logical ID that's only created by the serverless transform (albeit documented), and this fix achieves the same without doing that).

Description of changes:

This implements the fix proposed in this comment, which replaces the literal stage name by a reference to RestAPI.Stage. It also updates 2 unit tests.

It requires some documentation hunting to figure out why this should work, but basically the serverless transform substitutes RestAPI.Stage by the logical id of the stage (e.g., RestAPImystageStage) as described here, where it's documented as a referenceable property (also confirmed by inspecting sam validate --debug). Referencing the stage in the custom domain mapping results in delaying the creation of the mapping until after the stage resource is created (fixing the race condition). Then the Ref RestAPImystageStage resolves to the stage name (e.g., mystage) as documented here.

We've tested the PR in a Chalice API stack config that otherwise will pretty much always fail and roll back, and it resolves the issue, and generates a healthy API mapping (if the resolution chain was wrong it would fail with an "invalid stage identifier" error).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

chkothe avatar May 15 '21 18:05 chkothe

I'm running into this, too! Wondering if we can get this merged somehow? Thanks!

ogrodnek avatar Jan 06 '22 16:01 ogrodnek

I get the following error when I run your forked version:

jsii.errors.JavaScriptError:
  Error: Element used in Ref expression with logical ID: 'RestAPI.Stage' not found
...
jsii.errors.JSIIError: Element used in Ref expression with logical ID: 'RestAPI.Stage' not found

datashaman avatar Feb 23 '22 12:02 datashaman