aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.82k stars 820 forks source link

Populate custom resources CDK stacks with region and account #9360

Closed mpashkovskiy closed 1 year ago

mpashkovskiy commented 2 years ago

Is this feature request related to a new or existing Amplify category?

New category

Is this related to another service?

No response

Describe the feature you'd like to request

In many cases region and account is needed to create a custom resource like VPC or perform lookup. Could region and account be propageted from the used AWS CLI profile during the custom resource CloudFormation template generation phase? Also "placeholders" like Aws.region/Aws.accountId could be used.

Describe the solution you'd like

Code in generateCloudFormationFromCDK could be changed to something like:

const env = {
    region: 'Aws.region',
    account: 'Aws.accountId'
};
const customStack: cdk.Stack = new cdkStack(undefined, undefined, {env}, amplifyResourceProps);

Describe alternatives you've considered

Tried to use env varaiables CDK_DEFAULT_REGION/CDK_DEFAULT_ACCOUNT and AWS_DEFAULT_REGION/AWS_DEFAULT_ACCOUNT when calling amplify push and that didn't work for obvious reasons.

Additional context

Using custom resource like that:

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
  }
}

Results in following exception when executing amplity push:

⠙ 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)

Is this something that you'd be interested in working on?

nathanagez commented 2 years ago

Any updates on this feature ?

github-actions[bot] commented 2 years ago

👋 Hi, this issue was referenced in the v7.6.12 release!

Check out the release notes here https://github.com/aws-amplify/amplify-cli/releases/tag/v7.6.12.

lee-pl commented 2 years ago

+1

andreav commented 2 years ago

+1

I also tried exporting CDK_DEPLOY_ACCOUNT and CDK_DEPLOY_REGION to workaround the problem but the error still persists.

Do you know how can I accomplish a Vpc.fromLookup inside my CDK stack while adding custom resources?

josefaidt commented 1 year ago

Closing issue as the fix has been released with CLI 7.x. If you are still experiencing this issue please file a separate bug report and we can investigate further

github-actions[bot] commented 1 year ago

⚠️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.