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

Hosting CI/CD fails due to custom resource depending on storage

Open armenr opened this issue 1 year ago • 9 comments

How did you install the Amplify CLI?

pnpm

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

https://github.com/aws-amplify/amplify-hosting/issues/2478

^^ This bug is still a thing, in January 2024. Locally, I am able to build/push a custom CDK resource that contains this:

    const dependencies: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
      amplifyResourceProps.category,
      amplifyResourceProps.resourceName,
      [
        {
          category: "storage",
          resourceName: "myprojectV2DevelopStorage"
        },
      ]
    );

    const bucketName = cdk.Fn.ref(dependencies.storage.myprojectV2DevelopStorage.BucketName)
    const bucket = s3.Bucket.fromBucketName(this, bucketName, bucketName);

but in the Amplify CI/CD pipeline, this fails miserably with the error:

Command failed with exit code 2: /codebuild/output/src2150635331/src/myproject-v2/amplify/backend/custom/hcV2CDN/node_modules/.bin/tsc
cdk-stack.ts(57,48): error TS2339: Property 'storage' does not exist on type 'AmplifyDependentResourcesAttributes'.

And just like in the comments for the GitHub issue...this appears to work:

const bktNotBad = cdk.Fn.ref('storagemyprojectV2DevelopStorageBucketName')

Expected behavior

Following the Amplify docs and instructions generated by the CLI and in the docs, I'd expect to be able to use this pattern, without my builds breaking, inexplicably.

    const dependencies: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
      amplifyResourceProps.category,
      amplifyResourceProps.resourceName,
      [
        {
          category: "storage",
          resourceName: "myprojectV2DevelopStorage"
        },
      ]
    );

    const bucketName = cdk.Fn.ref(dependencies.storage.myprojectV2DevelopStorage.BucketName)
    const bucket = s3.Bucket.fromBucketName(this, bucketName, bucketName);

Reproduction steps

  1. add storage to your project
  2. add a custom CDK resource to your project
  3. follow the instructions and documentation by doing this in your CDK resource:
    const dependencies: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
      amplifyResourceProps.category,
      amplifyResourceProps.resourceName,
      [
        {
          category: "storage",
          resourceName: "myprojectV2DevelopStorage"
        },
      ]
    );

    const bucketName = cdk.Fn.ref(dependencies.storage.myprojectV2DevelopStorage.BucketName)
    const bucket = s3.Bucket.fromBucketName(this, bucketName, bucketName);
  1. amplify push --> everything works
  2. set up CI/CD on your project
  3. git push
  4. Observe your CI/CD pipeline break/fail
  5. Change the code to this:
    const bktNotBad = cdk.Fn.ref('storagemyprojectV2DevelopStorageBucketName')
    // const bucketName = cdk.Fn.ref(dependencies.storage.myprojectV2DevelopStorage.BucketName)
    const bucket = s3.Bucket.fromBucketName(this, bktNotBad, bktNotBad);
  1. Watch it work.

Project Identifier

e800fd03f8e9a7692235717d7b1bf9d4

Log output

# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [X] I have removed any sensitive information from my code snippets and submission.

armenr avatar Jan 13 '24 21:01 armenr

Hey @armenr, thank you for reaching out. The issue does look similar to https://github.com/aws-amplify/amplify-cli/issues/11847, referring to the comment https://github.com/aws-amplify/amplify-cli/issues/11847#issuecomment-1489013184 it appears a fix was released on CLI v11.0.3. If the issues does not occur locally and occurs on CICD, does the console app use a live package override that ties Amplify CLI to an older version or install on the yml file?

ykethan avatar Jan 16 '24 18:01 ykethan

@ykethan - I am installing the latest CLI package using both the override, as well as a manual install command in my amplify.yml

armenr avatar Jan 16 '24 20:01 armenr

@armenr, thank you for the information. I was able to reproduce the issue, marking as bug. possible workarounds: https://github.com/aws-amplify/amplify-cli/issues/11847#issuecomment-1428861075 and https://github.com/aws-amplify/amplify-cli/issues/11847#issuecomment-1464094946

ykethan avatar Jan 17 '24 16:01 ykethan

Thanks for the quick turn-around on investigating this, and for confirmation that we're not crazy 😂 .

I'll try the workaround and report back, but for now, this approach const bucketName = cdk.Fn.ref('storagemyProjectV2DevelopStorageBucketName') seems to be working as well.

I'll experiment if I have free time and report back.

armenr avatar Jan 17 '24 19:01 armenr

I can confirm that this workaround https://github.com/aws-amplify/amplify-cli/issues/11847#issuecomment-1464094946 , reordering config-backend.json, does work.

johnf avatar Feb 02 '24 02:02 johnf

That workaround is unreliable - the json may get reordered or regenerated without warning.

armenr avatar Feb 02 '24 08:02 armenr

Any updates on this ?

hasadata avatar Mar 02 '24 21:03 hasadata

Just dropped in to see what condition our condition is in... :)

armenr avatar Mar 19 '24 06:03 armenr

Our team is facing this issue. Any update?

Zhuohui-Li avatar Mar 30 '24 00:03 Zhuohui-Li