amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

add support for retrieving CDK context values for the purpose of setting environment-specific values for domainPrefix and callbackUrls

Open fossamagna opened this issue 1 year ago • 3 comments

Environment information

N/A

Description

I would like to get the values of amplify-deploy-type, amplify-backend-namespace and amplify-backend-name in the CDK Context to create a name or URL for the environment and deployment type.

We can configure Congito's userPool domain by domainPrefix. I would like to create this value from the amplify-backend-namespace and amplify-backend-name, like ${amplifyBackendNamespace}-${amplifyBackendName}. However, to get the value of a context variable in my app, I must use the TryGetContext method in the context of a construct. Therefore, I cannot specify it by domainPrefix as shown below, but must call the addDomain method.

amplify/backend.ts

const backend = defineBackend({
  auth,
  data,
});
const amplifyBackendType = backend.auth.resources.userPool.node.tryGetContext(
  "amplify-backend-type"
);
const amplifyBackendNamespace =
  backend.auth.resources.userPool.node.tryGetContext(
    "amplify-backend-namespace"
  );
const amplifyBackendName = backend.auth.resources.userPool.node.tryGetContext(
  "amplify-backend-name"
);
if (amplifyBackendType === "sandbox") {
  const domain = `${amplifyBackendNamespace}-${amplifyBackendName}`;
  backend.auth.resources.userPool.addDomain("Domain", {
    cognitoDomain: { domainPrefix: domain },
  });
}

I would like to be able to set up a function that provides a domainPrefix in ExternalProviderOptions as follows The function is passed a BackendIdentifier containing amplify-backend-namespace and amplify-backend-name as arguments, so it can return different values for different environments.

export type ExternalProviderOptions = {
  ...
  /**
   * You must provide a function to provide a unique domain prefix for the Hosted UI that Cognito will use for external login providers.
   */
  domainPrefixProvider?: (backendIdentifier: BackendIdentifier) => string;
  ...
}

fossamagna avatar Mar 01 '24 06:03 fossamagna

Hey @fossamagna :wave: thanks for raising this! Totally agree with you that this would simplify creating env-specific identifiers, marking as a feature request

josefaidt avatar Mar 01 '24 21:03 josefaidt

💯 Would be a fantastic addition!

stewartmoreland avatar May 15 '24 22:05 stewartmoreland