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

Custom resource can not reference files during deployment #13545

Closed
2 tasks done
Dennis-Dekker opened this issue Jan 19, 2024 · 2 comments
Closed
2 tasks done

Custom resource can not reference files during deployment #13545

Dennis-Dekker opened this issue Jan 19, 2024 · 2 comments
Labels
pending-triage Issue is pending triage

Comments

@Dennis-Dekker
Copy link

How did you install the Amplify CLI?

npm

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

16.16.0

Amplify CLI Version

12.10.1

What operating system are you using?

Windows

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

Not relevant changes for this issue

Describe the bug

After following this AWS guide on how to connect graphql through AWS Appsync with AWS DynamoDB, I ran into an issue with deploying the custom resource to AWS Amplify.

The goal is to create a new query in the schema.grapqhl file of the amplify backend/api folder, then to add a JS resolver that will fetch some data from DDB. While the general workflow did work, loading the JS resolver from a file does not.

Creating the custom resource worked fine, however, when I tried to add a JS resolver to my custom query the build errored on finding a file during the deployment:

🛑 The following resources failed to deploy:
Resource Name: APIJsResolver13C5C4E9 (AWS::AppSync::Resolver)
Event Type: create
Reason: Resource handler returned message: "Error occurr
ed during operation 'Error reading CodeS3Location from S
3. Error message: The specified bucket does not exist (S
ervice: S3, Status Code: 404, Request ID: 772EK6TY7DN4VG
DH, Extended Request ID: QRGdqUIXJfkx/vjbPcOwsx01gaCmeLj
K5caYgQ4ErOlfTus/nKTnOx0bTr8gL2iusFBCvZzvkXY=)'." (Reque
stToken: 1cef193e-2404-4ad2-0f55-4eb6d33dd796, HandlerErrorCode: GeneralServiceException)

The js file is located in the custom resource folder, and I have verified that it was added to the build folder created during the deployment (in the local build folder). Folder structure:

amplify:
  backend:
    custom:
      jsResolvers:
        build
          resolvers:
            testJsResolver.js:
          cdk-stack.js
          jsResolvers-cloudformation-template.json
        resolvers:
          testJsResolver.js:
        .npmrc (not changed)
        cdk-stack.ts (see code below under "Additional information")
        package.json (not changed)
        tsconfig.json (only added "allowJs": true )

The build only fails if I add the code with the Code.fromAsset function rom aws-cdk-lib/aws-appsync, it works if I use the Code.fromInLine function.

Expected behavior

CDK should resolve the file reference.

Reproduction steps

  1. Create project
  2. Add item model to schema
  3. Add test query to schema
  4. Use amplify add custom to create cdk resource
  5. Copy paste cdk-stack code (see "Additional information" for code).
  6. Add "allowJs": true to tsconfig.json
  7. amplify push

Project Identifier

e146314c59d34bb77745c891be2e93d1

Log output

# Put your logs below this line


Additional information

// @ts-nocheck

import * as cdk from "aws-cdk-lib";
import * as AmplifyHelpers from "@aws-amplify/cli-extensibility-helper";
import { AmplifyDependentResourcesAttributes } from "../../types/amplify-dependent-resources-ref";
import { Construct } from "constructs";
import { dependencies } from "webpack";
import { Code, FunctionRuntime } from "aws-cdk-lib/aws-appsync";
import * as path from "path";

export class cdkStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps, amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps) {
    super(scope, id, props);
    /* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */
    new cdk.CfnParameter(this, "env", {
      type: "String",
      description: "Current Amplify CLI env name",
    });

    const amplifyProjectInfo = AmplifyHelpers.getProjectInfo();

    const dependencies: AmplifyDependentResourcesAttributes =
      AmplifyHelpers.addResourceDependency(
        this,
        amplifyResourceProps.category,
        amplifyResourceProps.resourceName,
        [
          {
            category: "api",
            resourceName: "REDACTED",
          },
        ],
      );
    const apiId = cdk.Fn.ref(
      dependencies.api.beam3.GraphQLAPIIdOutput,
    );

    const api = cdk.aws_appsync.GraphqlApi.fromGraphqlApiAttributes(
      this,
      "API",
      {
        graphqlApiId: apiId,
      },
    );

    const dynamoDataSource = api.addDynamoDbDataSource(
      "dynamo-item-datasource",
      cdk.aws_dynamodb.Table.fromTableName(this, "tableTest", `Item-${apiId}-${amplifyProjectInfo.envName}`),
    );

    const resolverCode = `import { util } from "@aws-appsync/utils";

    export function request(ctx) {
      return {
        operation: "GetItem",
        key: util.dynamodb.toMapValues({ id: ctx.args.id }),
      };
    }

    export function response(ctx) {
      return {
        body: ctx.result,
        text: "Hello from JS resolver"
      };
    }`;

    const resolverPath = path.join(__dirname, "resolvers", "testJsResolver.js"); // This is the path to the resolver file

    const resolverCodeAsset = Code.fromAsset(resolverPath, resolverPath); // This is the code asset, which does not give error, only when used in the resolver below

    // const resolver = dynamoDataSource.createResolver("JsResolver", {
    //   typeName: "Query",
    //   fieldName: "getItem",
    //   runtime: new cdk.aws_appsync.FunctionRuntime(
    //     cdk.aws_appsync.FunctionRuntimeFamily.JS,
    //     cdk.aws_appsync.FunctionRuntime.JS_1_0_0.version,
    //   ),
    //   code: Code.fromInline(resolverCode) // This works!!!
    // });

    const resolver = dynamoDataSource.createResolver("JsResolver", {
      typeName: "Query",
      fieldName: "getItem",
      runtime: new cdk.aws_appsync.FunctionRuntime(
        cdk.aws_appsync.FunctionRuntimeFamily.JS,
        cdk.aws_appsync.FunctionRuntime.JS_1_0_0.version,
      ),
      code: Code.fromAsset(resolverPath, resolverPath), // This does not work!!!
    });
  }
}

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.
@Dennis-Dekker Dennis-Dekker added the pending-triage Issue is pending triage label Jan 19, 2024
@ykethan
Copy link
Member

ykethan commented Jan 19, 2024

Hey @Dennis-Dekker, thank you for reaching out. Assets are currently not supported in Amplify custom category, please refer to a similar issue #9055 where the behavior is currently being tracked.
As a workaround the code will need to be added as inline.
Closing the issue as duplicate.

@ykethan ykethan closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
Copy link

⚠️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
pending-triage Issue is pending triage
Projects
None yet
Development

No branches or pull requests

2 participants