Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use AmplifyHelpers in a standalone CDK app #13584

Closed
2 tasks done
OperationalFallacy opened this issue Feb 8, 2024 · 3 comments
Closed
2 tasks done

Can't use AmplifyHelpers in a standalone CDK app #13584

OperationalFallacy opened this issue Feb 8, 2024 · 3 comments
Labels
extensibility Issues related to expand or customize current configuration pending-triage Issue is pending triage

Comments

@OperationalFallacy
Copy link

OperationalFallacy commented Feb 8, 2024

How did you install the Amplify CLI?

yarn

If applicable, what version of Node.js are you using?

20

Amplify CLI Version

12.10.1

What operating system are you using?

Mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

Describe the bug

When I try to use amplify helper in a standalone CDK app (which is NOT a custom resource inside amplify app, it's completely separate folder with cdk project) like this

ln -s ../myApp-api/amplify amplify

import { AmplifyDependentResourcesAttributes } from "../../amplify/backend/types/amplify-dependent-resources-ref";
import * as AmplifyHelpers from "@aws-amplify/cli-extensibility-helper";
import { getProjectInfo } from "@aws-amplify/cli-extensibility-helper";

export class myAppAmplify extends Stack {
  constructor(
    scope: Construct,
    id: string,
    props: BaseStackProps,
  ) {
    super(scope, id, props);

    const envName = props?.environment.environmentName || "dev";
    const domain: string =
      envName === "prod" ? "myapp.com" : `${envName}.myapp.com `;

    new CfnParameter(this, "env", {
      type: "String",
      description: "Current Amplify CLI env name",
      default: props?.environment.environmentName || "dev",
    });

    const amplifyProjectInfo = getProjectInfo();
    const amplifyEnv = amplifyProjectInfo.envName;
...
    const dependencies: AmplifyDependentResourcesAttributes =
      AmplifyHelpers.addResourceDependency(
        this,
        props.amplifyResourceProps.category,
        props.amplifyResourceProps.resourceName,
        [
          {
            category: "auth",
            resourceName: "xxx221xxx",
          },
        ]
      );

    new Route53Construct(this, "Route53Construct", {
      ...props,
      domain: domain,
      googleVerificationToken:
        "google-xxx",
      userPool: dependencies.auth.xxx221xxx.UserPoolId,
    });

I got the error

❌ myAppPipelineCDK-main/dev/myAppAmplify (dev-myAppAmplify) failed: Error: The following CloudFormation Parameters are missing a value: authmyAppxxx221xxxIdentityPoolId, authmyAppxxx221xxxIdentityPoolName, authmyAppxxx221xxxHostedUIDomain, authmyAppxxx221xxxOAuthMetadata, authmyAppxxx221xxxUserPoolId, authmyAppxxx221xxxUserPoolArn, authmyAppxxx221xxxUserPoolName, authmyAppxxx221xxxAppClientIDWeb, authmyAppxxx221xxxAppClientID, authmyAppxxx221xxxGoogleWebClient

Expected behavior

Isn't it supposed just work? Or there is something tangled between amplify project and custom cdk application inside amplify?

So why would I not use custom CDK resource in Amplify?

Well, I tried that and run into another problem of importing properties from existing resources managed outside of Amplify. For example, a hosted zone.

Amplify cli doesn't understand CDK well, especially permissions around CDK Pipelines, thus I hoped that a standalone cdk app would work better.

I also couldn't find design document how these helpers supposed to work, it looks like they add bunch of dependencies somewhat in form of CF parameters? What if I need only user pool id, a simple string?

Reproduction steps

Create CDK app and try to access any backend properties from amplify.

Project Identifier

No response

Log output

# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.
@OperationalFallacy OperationalFallacy added the pending-triage Issue is pending triage label Feb 8, 2024
@OperationalFallacy OperationalFallacy changed the title Can't use AmplifyHelpers in a standalone stack Can't use AmplifyHelpers in a standalone CDK app Feb 8, 2024
@ykethan
Copy link
Member

ykethan commented Feb 9, 2024

Hey @OperationalFallacy, thank you for reaching out. using the cli-extensibility-helper as in a CDK app is currently not an expected behavior. But curious on the use case for using this in a cdk app?
additionally, if this AWS CDK application is currently in its early stages i would suggest exploring our newest addition Amplify Gen 2, which utilizes AWS CDK.

@ykethan ykethan added pending-response Issue is pending response from the issue author extensibility Issues related to expand or customize current configuration labels Feb 9, 2024
@OperationalFallacy
Copy link
Author

OperationalFallacy commented Feb 9, 2024

I've looked at the source code, indeed the cdk helper is too specific for working with amplify resources.

Perhaps next project I'll try gen 2, the @aws-amplify/backend does look like it could solve many of this problems. On the other side, I still want native cdk pipelines, not sure if new backend can work with it.

The use case was to setup custom auth domain for Cognito.

I found a solution eventually, had to add SSM string param to a custom resource in Amplify

    const amplifyProjectInfo = AmplifyHelpers.getProjectInfo();

    const mypoolId = cdk.Fn.ref(dependentResources.auth.myauthxxx.UserPoolId)

    // I think this is a wrong design which is being solved in gen 2.
    // To use a simple string for the Amplify resource, I needed to do all this acrobatics with dependencies and SSM parameters
    
    const dependentResources: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(
      this,
      amplifyResourceProps.category,
      amplifyResourceProps.resourceName,
      [{ category: 'auth', resourceName: 'myauthxxx' }],
    );
    
...
    new StringParameter(this, `poolId-${amplifyProjectInfo.envName}`, {
      parameterName: `/amplify/UserPoolId-${amplifyProjectInfo.envName}`,
      stringValue: mypoolId,
      description: 'Pool id from auth stacks'
    });

Then in the standalone CDK app

// Custom domain for Cognito
    const cert = new Certificate(this, "authCert", {
      domainName: `auth.${props.domain}`,
      validation: CertificateValidation.fromDns(appZone),
    });

    const poolId = StringParameter.valueForStringParameter(
      this,
      `/amplify/UserPoolId-${props.environment.environmentName}`
    );
    const up = UserPool.fromUserPoolId(this, "pool", poolId);

    const userPoolDomain = new UserPoolDomain(this, "UserPoolDomain", {
      userPool: up,
      customDomain: {
        domainName: `auth.${props.domain}`,
        certificate: cert,
      },
    });

    new ARecord(this, "UserPoolCloudFrontAliasRecord", {
      zone: appZone,
      recordName: authDomainName,
      target: RecordTarget.fromAlias(
        new aws_route53_targets.UserPoolDomainTarget(userPoolDomain)
      ),
    });

Hope this solution helps people looking to integrate amplify with external CDK apps.

@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Feb 9, 2024
Copy link

github-actions bot commented Feb 9, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensibility Issues related to expand or customize current configuration pending-triage Issue is pending triage
Projects
None yet
Development

No branches or pull requests

2 participants