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

Populate custom resources CDK stacks with region and account

Open mattiarossi opened this issue 1 year ago • 4 comments

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:

  • [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.

mattiarossi avatar May 03 '23 09:05 mattiarossi