-
Notifications
You must be signed in to change notification settings - Fork 825
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
Comments
Got the same issue here.
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, |
@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? |
@kaustavghosh06 for me it would be very helpful to access my configured external resources just like we know it from a lambda function:
So e.g.
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 |
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. |
If you need just the environment name and app id, you can access it via
|
Thanks @gakinson, 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.
|
HI @kaustavghosh06 ,
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? |
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 |
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. |
Hi @akshbhu, Do you have an ETA for this fix? Without it, we cant migrate amplify to the new versions >= 7.x.x properly |
Any ETA? Overrides are not fully useful if the env can't be obtained. |
…ndbox Provides getProjectInfo() filtered process.env for vm2 sandbox fix aws-amplify#9063
…global.amplify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
… of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…y of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…lify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…lify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…global.amplify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
… of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…y of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…lify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
…lify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
I have not seen a pr for the proposed solution from @akshbhu
I have opened pr #9241 to provide project/env into overrides via |
…ndbox provide getProjectInfo() and filtered process.env for vm2 sandbox fix aws-amplify#9063
…global.amplify of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
… of override.ts projectInfo is from getProjectInfo() env is process.env excluding AWS_ACCESS_KEY_ID and *SECRET* fix aws-amplify#9063
@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 Assuming that you're following the recipe that I described above, try this:
export const AuthContext = {
"environment": "XXX",
"resource_name":"YOUR_USERPOOL_RESOURCE_NAME",
"account_id":"000000000000",
"region":"us-east-1"
}
Let me know if it works, I'm curious :) |
Hi @gakinson ,
Hope this workaround works 😄 |
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. |
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. |
For anyone still fighting with 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"]); |
Any update to this? if we can't get the ARN of amplify generated resource the override feature is useless...🥲 |
Even importing file from current directory won't work . Please considering include the directory path of the |
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 import {
AmplifyAuthCognitoStackTemplate,
AmplifyProjectInfo,
} from '@aws-amplify/cli-extensibility-helper'
export function override(
resources: AmplifyAuthCognitoStackTemplate,
amplifyProjectInfo: AmplifyProjectInfo
) {
console.log(amplifyProjectInfo.projectName, amplifyProjectInfo.envName)
} |
Closing as this issue is now resolved 🙂 |
|
I was able to do this: resources.deploymentResource.stageName |
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 |
Hi, this is not working for me when deploying the branch to Amplify Hosting, the back-end build fails with an error message : |
Hey, I have the same problem with @macamhi. |
I paid a little more attention to this comment and I saw that I was trying to use
|
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?
The text was updated successfully, but these errors were encountered: