aws-amplify / aws-amplify/amplify-cli

Add option in amplify push command to fetch lambda environment variables from any given existing environment

Open
#12,299 0 comments 0 reactions 0 assignees View on GitHub
feature-request functions multienv p3 pending-review
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

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

_No response_

### Is this related to another service?

_No response_

### Describe the feature you'd like to request

**First I will describe the problem I am facing which leads to the feature idea.**

In my amplify project I have lambda functions with environment variables set for each one. When I try to create a new environment in a CI/CD environment - like Amplify Previews or Github Actions - and, after initializing the new environment, I try to push it, I get the following error:

```
Cannot push Amplify environment "my-env-name" due to missing Lambda function environment variable values. Rerun 'amplify push' without '--yes' to fix.
Function myFunc is missing values for environment variables: MY_VAR
```

Here goes a simplified version of the steps I am taking in the pipeline:

1. Install latest @amplify/cli
2. Run amplify-init.sh - bash file with headless configuration following [this guide](https://docs.amplify.aws/cli/usage/headless)
3. Install project dependencies and setup project files
4. Push amplify env - run `amplify push --yes`

And then the previously mentioned error is thrown.

### Describe the solution you'd like

Add an option in the push command to choose a base environment to fetch the lambda function environment varibles from.

Example:
I want my new amplify environment to be called `mynewenv`, but I have another environment called `dev` where these environement variables are already set. So I can call:

`amplify push --fetch-lambda-env-vars-from dev --yes`

And it will automatically fetch the lambda environment variables from the `dev` environment.

### Describe alternatives you've considered

I wrote a pre-push hook (code below) that rewrites `team-provider-info.json` and copies the variables from the environment I want. It was working before amplify-cli version 10.0.0 but now it seems the pre-push hook is not run anymore in the first time the environment is built.

```
const glob = require('glob');
const fs = require('fs');

const branch = process.env.USER_BRANCH || process.env.ENVIRONMENT || 'dev';

// find all ts files in amplify/backend/function
const tsFiles = glob.sync('./amplify/backend/function/**/tsc-src/**/*.ts');

if (branch !== 'dev' || branch !== 'master') {
let teamProviderInfo = JSON.parse(
fs.readFileSync('./amplify/team-provider-info.json', 'utf8')
);

if (!teamProviderInfo[branch]) {
teamProviderInfo = {
...teamProviderInfo,
[branch]: {
categories: {
function: {},
auth: {},
},
},
};
}

const prCategories = teamProviderInfo[branch].categories;

const devAuth = teamProviderInfo.dev.categories.auth;

if (!prCategories.auth) {
prCategories.auth = {};
}

const prAuth = prCategories.auth;

// iterate through all prAuth key and check if the key is in devAuth
for (const key in devAuth) {
if (!prAuth[key]) {
// create key
prAuth[key] = {};
}
for (const variable in devAuth[key]) {
if (!prAuth[key][variable]) {
// write the variable to the prAuth
prAuth[key][variable] = devAuth[key][variable];
}
}
}

const devFunctions = teamProviderInfo.dev.categories.function;
if (!teamProviderInfo[branch]) {
console.log('No team provider info for branch: ' + branch);
process.exit(0);
}

const prFunctions = prCategories.function;

const ignoredKeyNames = ['s3Key', 'deploymentBucketName'];

// iterate through all prFunctions key and check if the key is in devFunctions
for (const key in devFunctions) {
if (!ignoredKeyNames.includes(key)) {
if (!prFunctions[key]) {
// create key
prFunctions[key] = {};
}
for (const variable in devFunctions[key]) {
if (
!prFunctions[key][variable] &&
!ignoredKeyNames.includes(variable)
) {
// write the variable to the prFunctions
prFunctions[key][variable] = devFunctions[key][variable];
}
}
}
}

fs.writeFileSync(
'./amplify/team-provider-info.json',
JSON.stringify(teamProviderInfo, null, 2)
);

console.log(JSON.stringify(teamProviderInfo[branch], null, 2));
}
```

### Additional context

_No response_

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

- [x] 👋 I may be able to implement this feature request

### Would this feature include a breaking change?

- [ ] ⚠️ This feature might incur a breaking change

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.