Read More...

Constantly Keep Track of Your Amplify Backend Environment

July 05, 2020 · 3 min read

I currently work with Amplify in a team setting and have multiple backend environments such as dev, staging, and prod. Sometimes I forget which env I am in so I run amplify status with Amplify CLI to check but found it annoying because of the following reasons.

  1. It can take a while to load. You can't use the command-line until it finishes loading.
  2. Depending on how many Resources (Functions, Apis...etc) added, you may have to scroll up your Terminal window to see it.
  3. You just finished amplify push to prod and started enhancing an existing Lambda Function and you want to test new changes in dev. But you forget to amplify env checkout dev and instead amplify push to prod by accident and now your users are angry because you didn't bother to check the current env.

The 3rd point probably wouldn't happen but why not constantly keep track of your background env in the command-line prompt to be more efficient and lower the risk of messing up? I already keep track of my git branches in the command-line prompt, so I might as well add amplify into the mix.

The following is a tutorial from a Mac User's point of view.

When you run amplify status, Amplify CLI retrieves the current env by checking the envName key in /amplify/.config/local-env-info.json underneath the hood from the root project folder. The value can be retrieved using jq, a command-line JSON processor. I'm sure you could do something similar with sed, but I think jq commands read nicely.

  • Install jq via brew install jq.
  • Add the following function to your .bash_profile.
prompt_amplify_env() {
  local amplifyEnv=`jq -r '.envName' ./amplify/.config/local-env-info.json 2> /dev/null`

  if [ -n "$amplifyEnv" ];
  then
    echo -e "${1}${amplifyEnv}";
  fi
}

local amplifyEnv=... retrieves the raw value (without quotes) of key envName from the provided json file path and saves it into a local variable named amplifyEnv. You can see that the file path provided is relative, so if you are not in the root project folder, it will not be able to find the env and error out. 2> /dev/null makes sure that if it errors out, instead of printing the error message in the command-line, it disappears into the void. The remaining lines mean if the env is retrieved, it will be printed in the command-line.

But we actually want the env to be printed in the command-line prompt, not the command-line.

  • Add the following below the function code.
PS1+="\$(prompt_amplify_env \"📣 \[$(tput bold)$(tput setaf 215)\]\")";

Everything after PS1+= is a string to be added into the command-line prompt. tput is styling. prompt_amplify_env is executed inside here and the result is captured into the string.

That's it!

Now the command-line prompt will display which env you are in. When you checkout to a different env, the prompt will update as well. As mentioned earlier, this script only works when you are in the root of the project folder where the amplify folder is located.

AmplifyPrompt

Here is a customized version which shows the node and npm version on the 1st line. The 2nd line shows the current user, project directory, git branch, and amplify env.

CustomPrompt