amplify env
Amplify CLI supports creating multiple backend environments. Each Amplify backend environment is a container for the categories added to your project. You can find more information about multi-environment feature here.
The code for multi-environment resides in the packages/amplify-cli/src/commands/env directory. It has the following files for each command -
The code in these files mostly uses functions defined in the context object. Following is the overview of what each file is responsible for and how it works.
add.ts#
- Used for
amplify env add, which is essentiallyamplify init. So, this file just calls theinitfunction.
checkout.ts#
- Used for
amplify env checkout <envname>. - It first checks the input to make sure a valid environment name has been passed. The
contextobject is used to get the list of all available environment (context.amplify.getEnvDetails()). - Then, it uses the
stateManagerfromamplify-cli-coreto set the current environment to the environment name provided. - Once that is done, it sets up the
contextobject to reflect the new environment. - Finally, it initializes each
providerModulewith the settings for new environment.
get.ts#
- Used for
amplify env get <envname>, through which you can list all details for<envname>environment. Details include the AWS Region, IAM roles, S3 bucket and stack information. - It gets a list of environments using
getEnvDetails()and performs a search for the input<envname>. If found, it uses theprintEnvInfofunction found inpackages/amplify-cli/src/commands/helpers/envUtils.ts.
import.ts#
- Used for
amplify env import <envname>, through which you can import an existing environment from the cloud. - It parses the input command, and checks if the config provided by the environment is valid.
- Then, it uses the
getLocalAWSInfofunction defined instateManagerto actually fetch the specified environment. - Finally, it adds the fetched environment to the current context.
list.ts#
- Used for
amplify env list, through which you list all existing environments currently available. - The
getEnvDetailsfunction from currentcontextobject is used to get the list of all available environment (context.amplify.getEnvDetails()). - After that, it formats and prints the data to the console.
pull.ts#
- Used for
amplify env pull <envname>, through which you can pull the current environment from the cloud. If the--restoreflag is specified, it will overwrite any local changes. - The inner workings of this are similar to the
importcommand. It uses thecontextobject'sconstructExeInfofunction to fetch the latest changes to the current environment from the cloud, and then re-initializes the current environment based on the fetched data.
remove.ts#
- Used for
amplify env remove <envname>, through which you can remove both the local and the cloud environments, including all provisioned services and resources. - First, it checks whether the envname provided is a valid one. Next, it asks for confirmation from the user through the
getConfirmation()function. - Finally, it uses the
removeFromCloud()function fromcontextto delete resources through CloudFormation and usesstateManager.setLocalAWSInfoto remove the environment locally.