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 };
+ }
}