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

Populate custom resources CDK stacks with region and account #12575

Open
2 tasks done
mattiarossi opened this issue May 3, 2023 · 4 comments
Open
2 tasks done

Populate custom resources CDK stacks with region and account #12575

mattiarossi opened this issue May 3, 2023 · 4 comments
Labels
bug Something isn't working custom-cdk Issues related to custom CDK resource functionality p2 regression Flair label to track regressions

Comments

@mattiarossi
Copy link

How did you install the Amplify CLI?

No response

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

No response

Amplify CLI Version

11.1.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 using a custom CDK deployment and trying to reference existing infrastructure, it is expected that the stack refrences a default account and region.
Usually it is possible to setup these values using the documented env variables CDK_DEFAULT_REGION/CDK_DEFAULT_ACCOUNT or AWS_DEFAULT_REGION/AWS_DEFAULT_ACCOUNT
but amplify does not honor these.

A possible solution was suggested in #9360 and a pr provided and an attempt to merge it was done in the v7.6.12 release but it has been reverted and this functionality is still missing/behaviour has reverted to functionality prior to v7.6.12

Expected behavior

being able to create a reference object to an existing resource like :

import * as cdk from '@aws-cdk/core';
import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper';
import { AmplifyDependentResourcesAttributes } from '../../types/amplify-dependent-resources-ref';
import * as ec2 from '@aws-cdk/aws-ec2';

export class cdkStack extends cdk.Stack {
  constructor(scope: cdk.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',
    });
    /* AWS CDK code goes here - learn more: https://docs.aws.amazon.com/cdk/latest/guide/home.html */
    
    const vpc = ec2.Vpc.fromLookup(this, "VPC", {
      isDefault: true,
    });

    // creation of resources inside the VPC
  }
}

and not getting the 'missing env' error:

⠙ Building custom resources🛑 There was an error building the custom resources
🛑 Error: Cannot retrieve value from context provider vpc-provider since account/region are not specified at the stack level. Configure "env" with an account and region when you define your stack.See https://docs.aws.amazon.com/cdk/latest/guide/environments.html for more details.
    at Function.getValue (/Users/pashma/workspace/sgc.backend/aws/sgc/amplify/backend/custom/serverlessPostgreSQL/node_modules/@aws-cdk/core/lib/context-provider.ts:63:13)
    at Function.fromLookup (/Users/pashma/workspace/sgc.backend/aws/sgc/amplify/backend/custom/serverlessPostgreSQL/node_modules/@aws-cdk/aws-ec2/lib/vpc.ts:635:66)
    at new cdkStack (/Users/pashma/workspace/sgc.backend/aws/sgc/amplify/backend/custom/serverlessPostgreSQL/build/cdk-stack.js:44:29)
    at generateCloudFormationFromCDK (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/@aws-amplify/amplify-category-custom/src/utils/generate-cfn-from-cdk.ts:17:34)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at buildResource (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/@aws-amplify/amplify-category-custom/src/utils/build-custom-resources.ts:107:3)
    at buildCustomResources (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/@aws-amplify/amplify-category-custom/src/utils/build-custom-resources.ts:26:7)
    at transformCategoryStack (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/@aws-amplify/amplify-category-custom/src/index.ts:30:3)
    at transformResourceWithOverrides (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-provider-awscloudformation/src/override-manager/transform-resource.ts:27:9)
    at Object.buildOverrides (/usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-provider-awscloudformation/src/utility-functions.js:72:7)
    at AmplifyToolkit.pushResources [as _pushResources] (/usr/local/lib/node_modules/@aws-amplify/cli/src/extensions/amplify-helpers/push-resources.ts:60:3)
    at Object.run (/usr/local/lib/node_modules/@aws-amplify/cli/src/commands/push.ts:64:12)
    at Object.executeAmplifyCommand (/usr/local/lib/node_modules/@aws-amplify/cli/src/index.ts:373:5)
    at executePluginModuleCommand (/usr/local/lib/node_modules/@aws-amplify/cli/src/execution-manager.ts:204:3)
    at executeCommand (/usr/local/lib/node_modules/@aws-amplify/cli/src/execution-manager.ts:30:5)
    at Object.run (/usr/local/lib/node_modules/@aws-amplify/cli/src/index.ts:205:5)

Reproduction steps

...

Project Identifier

No response

Log output

No response

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.
@mattiarossi mattiarossi added the pending-triage Issue is pending triage label May 3, 2023
@ykethan ykethan added custom-cdk Issues related to custom CDK resource functionality bug Something isn't working labels May 3, 2023
@josefaidt josefaidt removed the pending-triage Issue is pending triage label May 3, 2023
@ykethan
Copy link
Member

ykethan commented May 3, 2023

Marking this as bug as I was able to reproduce this issue.
As an alternative we can use fromAttributes method to retrieve a vpc using an ID.

@josefaidt josefaidt added p2 regression Flair label to track regressions labels May 9, 2023
@marcantoineveilleux
Copy link

Is there any workaround for this without using a specific VPC id?

@anandkumarpatel
Copy link

+1 Just ran into this as well.

@priyanshu-sekhar
Copy link

A lot of devs are facing this issue w.r.t several custom resources. Why is the PR not merged yet ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working custom-cdk Issues related to custom CDK resource functionality p2 regression Flair label to track regressions
Projects
None yet
Development

No branches or pull requests

6 participants