From 6cfae20fcfdce0e1fefade2d9dd52c298a593812 Mon Sep 17 00:00:00 2001 From: Nick Cipollina Date: Tue, 13 Oct 2026 15:02:12 -0400 Subject: [PATCH 1/2] feat(cognito): accept an explicit hosted zone for custom domains CognitoUserPoolConstruct always found the custom-domain hosted zone with HostedZone.FromLookup, a synth-time context lookup that requires AWS credentials (or a cached cdk.context.json value). Consumers that already know the zone could not synthesize offline, for example in credential-free pull request CI. Add an optional CognitoUserPoolDomainProps.HostedZone. When supplied it is used for certificate DNS validation and the alias record, and no lookup is performed; when null the existing lookup is unchanged. The interface member has a default implementation so existing implementers keep compiling. Co-Authored-By: Claude Opus 5.5 --- docs/constructs/cognito-user-pool.md | 14 ++++- .../CognitoUserPoolConstruct.cs | 2 +- .../Models/CognitoUserPoolDomainProps.cs | 12 ++++ .../CognitoUserPoolConstructTests.cs | 57 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/docs/constructs/cognito-user-pool.md b/docs/constructs/cognito-user-pool.md index c8f800c..57d8931 100644 --- a/docs/constructs/cognito-user-pool.md +++ b/docs/constructs/cognito-user-pool.md @@ -74,6 +74,8 @@ Exactly one of `CognitoDomainPrefix` or (`DomainName` + `AuthSubDomain`) must be | `AuthSubDomain` | `string?` | `null` | Subdomain for custom domain (e.g., `auth` → `auth.example.com`) | | `ManagedLoginVersion` | `CognitoManagedLoginVersion` | `ManagedLogin` | `ClassicHostedUi` or `ManagedLogin` (v2) | | `CreateRoute53Record` | `bool` | `true` | Whether to create an A-alias record in the hosted zone (custom domain only) | +| `Certificate` | `ICertificate?` | `null` | Existing ACM certificate (must be in `us-east-1`); when `null`, one is created with DNS validation (custom domain only) | +| `HostedZone` | `IHostedZone?` | `null` | Public hosted zone for `DomainName`; when supplied, no `HostedZone.FromLookup` context lookup is performed, so the stack synthesizes without AWS credentials (custom domain only) | ### App Client Properties (`CognitoUserPoolAppClientProps`) @@ -148,7 +150,17 @@ var pool = new CognitoUserPoolConstruct(this, "auth-pool", new CognitoUserPoolCo }); ``` -> The hosted zone for `DomainName` must already exist in the same account. The construct performs a `HostedZone.FromLookup` and creates an ACM certificate with DNS validation automatically. +> The hosted zone for `DomainName` must already exist in the same account. Unless `HostedZone` is supplied, the construct performs a `HostedZone.FromLookup` (a synth-time context lookup that needs AWS credentials or a cached `cdk.context.json` value). It creates an ACM certificate with DNS validation automatically unless `Certificate` is supplied. +> +> To synthesize without AWS credentials (for example, in CI), pass a zone you already know: +> +> ```csharp +> HostedZone = HostedZone.FromHostedZoneAttributes(this, "zone", new HostedZoneAttributes +> { +> HostedZoneId = "Z0123456789EXAMPLE", +> ZoneName = "example.com", +> }), +> ``` ### Resource Servers and API Scopes diff --git a/src/LayeredCraft.Cdk.Constructs/CognitoUserPoolConstruct.cs b/src/LayeredCraft.Cdk.Constructs/CognitoUserPoolConstruct.cs index 43493fc..f23d116 100644 --- a/src/LayeredCraft.Cdk.Constructs/CognitoUserPoolConstruct.cs +++ b/src/LayeredCraft.Cdk.Constructs/CognitoUserPoolConstruct.cs @@ -341,7 +341,7 @@ private void ConfigureUserPoolDomain(string id, ICognitoUserPoolConstructProps p } else { - var zone = HostedZone.FromLookup(this, $"{id}-hosted-zone", new HostedZoneProviderProps + var zone = props.Domain.HostedZone ?? HostedZone.FromLookup(this, $"{id}-hosted-zone", new HostedZoneProviderProps { DomainName = props.Domain.DomainName!, }); diff --git a/src/LayeredCraft.Cdk.Constructs/Models/CognitoUserPoolDomainProps.cs b/src/LayeredCraft.Cdk.Constructs/Models/CognitoUserPoolDomainProps.cs index afad518..ed5cd9f 100644 --- a/src/LayeredCraft.Cdk.Constructs/Models/CognitoUserPoolDomainProps.cs +++ b/src/LayeredCraft.Cdk.Constructs/Models/CognitoUserPoolDomainProps.cs @@ -1,4 +1,5 @@ using Amazon.CDK.AWS.CertificateManager; +using Amazon.CDK.AWS.Route53; namespace LayeredCraft.Cdk.Constructs.Models; @@ -23,6 +24,14 @@ public interface ICognitoUserPoolDomainProps /// in the stack's region (valid when the stack is already deployed to us-east-1). /// ICertificate? Certificate { get; } + + /// + /// The public hosted zone for , used to validate a created certificate and + /// to create the alias record. When provided, the construct performs no hosted-zone context lookup, + /// so the stack synthesizes without AWS credentials. If , the zone is found + /// with HostedZone.FromLookup. + /// + IHostedZone? HostedZone => null; } public sealed record CognitoUserPoolDomainProps : ICognitoUserPoolDomainProps @@ -40,6 +49,9 @@ public sealed record CognitoUserPoolDomainProps : ICognitoUserPoolDomainProps /// public ICertificate? Certificate { get; init; } + + /// + public IHostedZone? HostedZone { get; init; } } public enum CognitoManagedLoginVersion diff --git a/test/LayeredCraft.Cdk.Constructs.Tests/CognitoUserPoolConstructTests.cs b/test/LayeredCraft.Cdk.Constructs.Tests/CognitoUserPoolConstructTests.cs index d5936e9..7b0aefb 100644 --- a/test/LayeredCraft.Cdk.Constructs.Tests/CognitoUserPoolConstructTests.cs +++ b/test/LayeredCraft.Cdk.Constructs.Tests/CognitoUserPoolConstructTests.cs @@ -1,5 +1,6 @@ using Amazon.CDK; using Amazon.CDK.Assertions; +using Amazon.CDK.AWS.Route53; using AwesomeAssertions; using LayeredCraft.Cdk.Constructs; using LayeredCraft.Cdk.Constructs.Models; @@ -81,6 +82,47 @@ public void Construct_ShouldNotCreateDomain_WhenDomainIsNull() template.ResourceCountIs("AWS::Cognito::UserPoolDomain", 0); } + [Fact] + public void Construct_ShouldUseSuppliedHostedZone_WithoutContextLookup_WhenCustomDomainHasHostedZone() + { + var app = new App(); + var stack = CreateEnvironmentStack(app); + var zone = HostedZone.FromHostedZoneAttributes(stack, "zone", new HostedZoneAttributes + { + HostedZoneId = "Z0123456789EXAMPLE", + ZoneName = "example.com", + }); + + _ = new CognitoUserPoolConstruct(stack, "test-pool", CustomDomainProps(new CognitoUserPoolDomainProps + { + DomainName = "example.com", + AuthSubDomain = "auth", + HostedZone = zone, + })); + + app.Synth().Manifest.Missing.Should().BeNullOrEmpty(); + Template.FromStack(stack).HasResourceProperties("AWS::Route53::RecordSet", new Dictionary + { + ["Name"] = "auth.example.com.", + ["HostedZoneId"] = "Z0123456789EXAMPLE", + }); + } + + [Fact] + public void Construct_ShouldLookUpHostedZone_WhenCustomDomainHasNoHostedZone() + { + var app = new App(); + var stack = CreateEnvironmentStack(app); + + _ = new CognitoUserPoolConstruct(stack, "test-pool", CustomDomainProps(new CognitoUserPoolDomainProps + { + DomainName = "example.com", + AuthSubDomain = "auth", + })); + + app.Synth().Manifest.Missing.Should().ContainSingle().Which.Key.Should().StartWith("hosted-zone:"); + } + // ------------------------------------------------------------------------- // App client // ------------------------------------------------------------------------- @@ -270,4 +312,19 @@ public void Construct_ShouldExportAppClientId(CognitoUserPoolConstructProps prop // ------------------------------------------------------------------------- private static Stack CreateStack() => new(new App(), "test-stack"); + + // Context lookups require an explicit account and region. + private static Stack CreateEnvironmentStack(App app) => new(app, "test-stack", new StackProps + { + Env = new Amazon.CDK.Environment { Account = "123456789012", Region = "us-east-1" }, + }); + + private static CognitoUserPoolConstructProps CustomDomainProps(CognitoUserPoolDomainProps domain) + { + var props = CdkTestHelper.CreateCognitoUserPoolPropsBuilder() + .AddWebAppClient("client", ["https://example.com/callback"], ["https://example.com"]) + .Build(); + + return props with { Domain = domain }; + } } From a6521e669c3db536725f865323db3b3d83c2fc47 Mon Sep 17 00:00:00 2001 From: Nick Cipollina Date: Tue, 13 Oct 2026 19:40:29 -0400 Subject: [PATCH 2/2] ci: run the PR build now that the PR is ready for review Co-Authored-By: Claude Opus 5.5