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

Figure out the environment inside any override.ts file #9063

Closed
2 tasks
cespin opened this issue Nov 24, 2021 · 45 comments
Closed
2 tasks

Figure out the environment inside any override.ts file #9063

cespin opened this issue Nov 24, 2021 · 45 comments
Assignees
Labels
extensibility Issues related to expand or customize current configuration feature-request Request a new feature p3

Comments

@cespin
Copy link

cespin commented Nov 24, 2021

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

auth, storage, api

Is this related to another service?

No response

Describe the feature you'd like to request

There is no context available in the override.ts files.

I was recently trying to reference a function name and in order to do that properly I needed to know to environment name. I wasn't able to neither figure it out nor to reference the stack parameters.

It would be useful if these files are given a context that contains the stack and project parameters.

Describe the solution you'd like

I would like to have a context parameter available during inside the override.ts file

Describe alternatives you've considered

I considered introspecting values already set inside the one object that's being passed to the override.ts, for example, in the case of Auth a parameter of type AmplifyAuthCognitoStackTemplate is available and there are attributes set in it which names contain the environment name like the cognito trigger function names. I wasn't able to do this because the values (of type String) that are already set in that object are some sort of internal reference.

Additional context

No response

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

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change
@chris-mds
Copy link

Got the same issue here.
I want to set a trigger function to my s3 bucket with the following override function (maybe someone can use this, when migrating his s3 setup)

import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyS3ResourceTemplate) {
  resources.s3Bucket.notificationConfiguration = {
    lambdaConfigurations : [
      {
        event: 's3:ObjectCreated:*', 
        function:'Here I need access for my env specific lambda function', 
        filter: {
          s3Key: {
            rules: [
              {
                name: 'prefix',
                value: 'exchange'
              },
              {
                name: 'suffix',
                value: 'xml'
              }
            ]
          }
        }
      },
      {
        event: 's3:ObjectRemoved:*', 
        function:'functionDriversAppFileInterfaceHandlerArn', 
        filter: {
          s3Key: {
            rules: [
              {
                name: 'prefix',
                value: 'exchange'
              },
              {
                name: 'suffix',
                value: 'xml'
              }
            ]
          }
        }
      }
    ]
  }
}

Of course I can hardcode it, but I guess this doesn't make sense in long term and isn't the idea behind this new override pattern,

@kaustavghosh06
Copy link
Contributor

@cespin @chris-mds Thanks for explaining your use cases. What all information would be helpful for you’ll to be present in this context apart from env name?

@chris-mds
Copy link

@kaustavghosh06 for me it would be very helpful to access my configured external resources just like we know it from a lambda function:

/* Amplify Params - DO NOT EDIT API_APPGRAPHQLAPI_GRAPHQLAPIENDPOINTOUTPUT API_APPGRAPHQLAPI_GRAPHQLAPIIDOUTPUT ENV REGION STORAGE_APPSTORAGE_BUCKETNAME Amplify Params - DO NOT EDIT */

So e.g.

amplify update storage

Do you need to access other resources from this project? (Y/n)

Chose the resources you need to access
- Auth ( )
- Function (x)
- Storage ( )
- ...
Chose which function you need to access
- MySuperCoolLambdaFunction (x)

and this will give you all the necessary informations you need to implement in your override function. Without this kind of functionality I don't have a clue, how I can access my functions for the trigger

@cespin
Copy link
Author

cespin commented Nov 25, 2021

In short: access to the same info that the we have available when editing the stack's CloudFormation file manually. Mainly the (nested) stack's parameters. The environment is just one of these parameters. Like this we can keep on adding parameters and secrets through the Amplify CLI and reuse them here.

@gakinson
Copy link

If you need just the environment name and app id, you can access it via

import {
   getProjectInfo,
} from '@aws-amplify/cli-extensibility-helper';

export function override(resources: ...) {
  const projectInfo = getProjectInfo();

}

@chris-mds
Copy link

Thanks @gakinson,
Is this somewhere documented?

I updated my code with the new variable and it works for different env like expected. Maybe someone can use this as a reference. Function overrides trigger for an s3 bucket with prefix 'exchange' and suffix 'xml' in this example. Trigger lambda function gets called for every create and put event when the filter matches. And of course you have to replace the 'xxxx' in the arn with your account id as well as your region and function name.

import { AmplifyS3ResourceTemplate, getProjectInfo } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyS3ResourceTemplate) {
  const projectInfo = getProjectInfo()
  resources.s3Bucket.notificationConfiguration = {
    lambdaConfigurations : [
      {
        event: 's3:ObjectCreated:*', 
        function:`arn:aws:lambda:YourRegion:xxxxxxx:function:YourSuperCoolTriggerFunctionName-${projectInfo.envName}`, 
        filter: {
          s3Key: {
            rules: [
              {
                name: 'prefix',
                value: 'exchange'
              },
              {
                name: 'suffix',
                value: 'xml'
              }
            ]
          }
        }
      },
      {
        event: 's3:ObjectRemoved:*', 
        function:`arn:aws:lambda:YourRegion:xxxxxxx:function:YourSuperCoolTriggerFunctionName-${projectInfo.envName}`, 
        filter: {
          s3Key: {
            rules: [
              {
                name: 'prefix',
                value: 'exchange'
              },
              {
                name: 'suffix',
                value: 'xml'
              }
            ]
          }
        }
      }
    ]
  }
}

@chris-mds
Copy link

chris-mds commented Nov 25, 2021

HI @kaustavghosh06 ,
I get a new error I didn't recognized before when using the getProjectInfo.

⠇ Building resource storage/MyStorage🛑 Error: Skipping override due to VMError: Access denied to require 'os'

So I guess this helper function tries to get the env through the os, which is denied by my system. How can I fix this?

@cespin
Copy link
Author

cespin commented Nov 25, 2021

I had already tried this @chris-mds, the only difference on my side is that I was overriding the Auth. I got the same error.

I also tried to access the nested stack's parameter using CDK primitives, all the info we need is in the params. I got a similar error that said that I didn't have permissions to access 'fs'.

@kaustavghosh06, why can't we import CDK constructs in the override.js files and use them? I was trying to set a Ref this way and I got the error I mentioned above. Similarly, if we need to use the SSM/secret manager resolve feature we cannot because imports and CDK primitives are restricted.

@chris-mds , it's documented here

@gakinson
Copy link

I am also experiencing the same error when I add the getProjectInfo function to my overrides.ts file in auth. I tried doing a bunch of debugging last night, but didn't get anywhere. Hopefully the Amplify team can help with this one soon.

@akshbhu akshbhu added feature-request Request a new feature extensibility Issues related to expand or customize current configuration labels Nov 25, 2021
@chris-mds
Copy link

Hi @akshbhu,

Do you have an ETA for this fix? Without it, we cant migrate amplify to the new versions >= 7.x.x properly

@christianguevara
Copy link

christianguevara commented Dec 3, 2021

Any ETA? Overrides are not fully useful if the env can't be obtained.

arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 7, 2021
…ndbox

Provides getProjectInfo() filtered process.env for vm2 sandbox

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 7, 2021
…global.amplify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 7, 2021
… of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 7, 2021
…y of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 7, 2021
…lify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 7, 2021
…lify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 8, 2021
…global.amplify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 8, 2021
… of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 8, 2021
…y of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 8, 2021
…lify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 8, 2021
…lify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
@arledesma
Copy link

I have not seen a pr for the proposed solution from @akshbhu

Currently fs/ env access is blocked in override files except for custom resources which we use at build time, so getProjectInfo wont work in overrides.ts files.

We are working as an enhancement to provide project/env info in the overrides function.

I have opened pr #9241 to provide project/env into overrides via global.amplify. 🤷

arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 14, 2021
…ndbox

provide getProjectInfo() and filtered process.env for vm2 sandbox

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 14, 2021
…global.amplify of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
arledesma added a commit to arledesma/amplify-cli that referenced this issue Dec 14, 2021
… of override.ts

projectInfo is from getProjectInfo()
env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET*

fix aws-amplify#9063
@cespin
Copy link
Author

cespin commented Mar 8, 2022

@gakinson , it literally took me ~5 hours to make the pipeline execute the hooks. In my case, I tried different flavours of what @anurag128 is suggesting but it wasn't working out.

I tried different things (and commits and amplify push commands) so I'm not 100% sure what made the pipeline finally execute the hooks but when it finally did it once it never stopped working.

Assuming that you're following the recipe that I described above, try this:

  • Go to the folder /amplify/backend/auth/YOUR_USERPOOL_RESOURCE_NAME
  • In there, create the file config-auth-override.ts
  • Put this inside the file:
export const AuthContext = {
    "environment": "XXX",
    "resource_name":"YOUR_USERPOOL_RESOURCE_NAME",
    "account_id":"000000000000",
    "region":"us-east-1"
}
  • Commit and push to let the pipeline run

Let me know if it works, I'm curious :)

@himesh-sv
Copy link

himesh-sv commented Mar 8, 2022

Hi @gakinson ,
For some reason, Amplify Hook (pre-push.js) is getting overwritten with a default file at the time of running amplify push in the CI/CD pipeline. You can programmatically copy the file and insert it before running amplify push in your deployment script. First copy your current pre-push.js to your root (better to check in the file) and add the following to your deployment script before amplify push command.

cp pre-push.js amplify/hooks/

Hope this workaround works 😄

@gakinson
Copy link

gakinson commented Mar 8, 2022

Thanks for all the help!

I will test some stuff out today, but I did find that if I ran an amplify push locally for an environment, the contents in the s3 bucket for deployment get updated. I may just try copying the content directly into the S3 deployment bucket to see if that works.

@gakinson
Copy link

One thing I ended up doing was deploy just the hook change first so that it gets uploaded to the cloud on the end of the deployment. Then I did the change for the auth resource and pushed after.

@cespin
Copy link
Author

cespin commented Aug 12, 2022

For anyone still fighting with this:

  • I had to disable the --simple flag in my amplify.yml file. Otherwise my pre-push hook wouldn't run.
  • Within the pipeline execution I couldn't get a hold of the amplify-meta.json file so I had to grab the values from env vars. The end result is this:
const fs = require("fs");
const parameters = JSON.parse(fs.readFileSync(0, {encoding: 'utf8'}));
let accountId, region, environment;

try {
    const amplifyMeta = JSON.parse(fs.readFileSync("amplify/backend/amplify-meta.json"));
    accountId = amplifyMeta.providers.awscloudformation.AuthRoleArn.split(":")[4];
    region = amplifyMeta.providers.awscloudformation.Region;
    environment = parameters.data.amplify.environment.envName;
} catch (e) {
    console.warn("No amplify-meta.json found, falling back to environment variables");
    accountId = process.env.CUSTOMER_ACCOUNTID;
    region = process.env.AWS_REGION;
    environment = process.env.AWS_BACKEND_ENVIRONMENT_ARN.split("/").pop();
}
if (!accountId || !region || !environment) {
    throw new Error("Couldn't find accountId, region or environment");
}
const content = `export const BuildContext = {
    "environment": "${environment}",
    "account_id":"${accountId}",
    "region":"${region}"
}`;
console.info("config-override content:", content);
try {
    fs.writeFileSync(`amplify/backend/auth/MY-AUTH-RESOURCE/config-override.ts`, content);
    fs.writeFileSync(`amplify/backend/api/MY-API-RESOURCE/config-override.ts`, content);
    process.exit(0);
} catch (err) {
    console.error(err);
    process.exit(1);
}

The first case with the amplify-meta.json file works in my local environment and the second one with the env vars works for the pipeline.

With that I can do this in the override.ts files:

import {BuildContext} from "./config-override";
...
console.info(BuildContext["environment"]);
console.info(BuildContext["account_id"]);
console.info(BuildContext["region"]);

@yaquawa
Copy link

yaquawa commented Jan 23, 2023

Any update to this? if we can't get the ARN of amplify generated resource the override feature is useless...🥲

@yaquawa
Copy link

yaquawa commented Jan 26, 2023

Even importing file from current directory won't work .
For example import X from './XXX.ts' won't work.

Please considering include the directory path of the override.ts, by setting the require.root option in the upstream lib vm2.

@josefaidt
Copy link
Contributor

Hey folks in the thread 👋 this is now possible with resource overrides for all categories except for API (for now). You can upgrade to the latest CLI (11.0.3 at the time of writing) and upgrade the Extensibility helper - with CDK v2.

Once upgraded, you can add the second argument to the override function amplifyProjectInfo to get the project name and environment name! 🎉

import {
  AmplifyAuthCognitoStackTemplate,
  AmplifyProjectInfo,
} from '@aws-amplify/cli-extensibility-helper'

export function override(
  resources: AmplifyAuthCognitoStackTemplate,
  amplifyProjectInfo: AmplifyProjectInfo
) {
  console.log(amplifyProjectInfo.projectName, amplifyProjectInfo.envName)
}

@josefaidt
Copy link
Contributor

Closing as this issue is now resolved 🙂

@github-actions
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.

@alezzios
Copy link

I was able to do this:

resources.deploymentResource.stageName

@wvidana
Copy link

wvidana commented Jun 26, 2023

In case anyone is looking into this and don't want to upgrade just yet, take a look at this comment on the use of addPropertyOverride to write plain cfn code into override.ts (using Fn:Sub or Ref with access to the cfn properties like env and so on)

#12582 (comment)

@macamhi
Copy link

macamhi commented Jun 26, 2023

Hey folks in the thread 👋 this is now possible with resource overrides for all categories except for API (for now). You can upgrade to the latest CLI (11.0.3 at the time of writing) and upgrade the Extensibility helper - with CDK v2.

Once upgraded, you can add the second argument to the override function amplifyProjectInfo to get the project name and environment name! 🎉

import {
  AmplifyAuthCognitoStackTemplate,
  AmplifyProjectInfo,
} from '@aws-amplify/cli-extensibility-helper'

export function override(
  resources: AmplifyAuthCognitoStackTemplate,
  amplifyProjectInfo: AmplifyProjectInfo
) {
  console.log(amplifyProjectInfo.projectName, amplifyProjectInfo.envName)
}

Hi, this is not working for me when deploying the branch to Amplify Hosting, the back-end build fails with an error message :
Cannot read property 'envName' of undefined
Indeed, amplifyProjectInfo is undefined when the override function is called at that time.
What's really strange is that it works fine when building and pushing locally!
I tried both with amplify CLI 11.0.5 and latest (12.1.1).
Any idea of what's wrong?

@redjonzaci
Copy link
Contributor

Hey, I have the same problem with @macamhi.

@redjonzaci
Copy link
Contributor

I paid a little more attention to this comment and I saw that I was trying to use amplifyProjectInfo in an API's override.ts.
Is there any workaround for the API category? Because I can't merge my changes otherwise.

Hey folks in the thread 👋 this is now possible with resource overrides for all categories except for API (for now). You can upgrade to the latest CLI (11.0.3 at the time of writing) and upgrade the Extensibility helper - with CDK v2.

Once upgraded, you can add the second argument to the override function amplifyProjectInfo to get the project name and environment name! 🎉

import {
  AmplifyAuthCognitoStackTemplate,
  AmplifyProjectInfo,
} from '@aws-amplify/cli-extensibility-helper'

export function override(
  resources: AmplifyAuthCognitoStackTemplate,
  amplifyProjectInfo: AmplifyProjectInfo
) {
  console.log(amplifyProjectInfo.projectName, amplifyProjectInfo.envName)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensibility Issues related to expand or customize current configuration feature-request Request a new feature p3
Projects
None yet
Development

No branches or pull requests