In this tutorial, you are going to learn how to use Amplify's env command. We will walk through an example where two people collaborate on an Amplify project.
Note: This article is a tutorial for intermediate developers who are familiar with the basics of Amplify. Do you want to learn how to accelerate the creation of your projects using Amplify π? I recommend beginners to check out Nader Dabit's free course on egghead, or Amplify's 'Getting Started'.
If you use Amplify in your team, you will have to know how to use the env command properly. Otherwise, your collaboration will be a mess. Done right, Amplify allows easy and effective collaboration on projects.
There are two ways you can share an Amplify project.
Each team member works in their own "sandbox" aka. their local version of the environment.
The team shares the same development environment.
The first approach is recommended. Let's walk through each procedure, so you understand both and can decide which you prefer. You need to be familiar with Git to follow this tutorial.
1. Sandboxes
We start with the sandbox approach. Create a folder for this tutorial called multiple-env/.
Initialize the React project.
Create React App automatically initializes a Git repository for us. The folder layout should later look like this.
Imagine env-two/ being on your co-worker's computer. Here we'll use the two apps on your computer to simulate multiple environments.
1. a) You
Initialize Amplify in your first environment and add authentication. Note: In some of these code examples, there will be several CLI commands that you have to run. Remember to double-check that you catch all and don't miss any.
Nothing fancy going on here. Just make sure to name the first environment prod, master or something along those lines.
Push your changes to the cloud.
If you run amplify status, you will see that your changes have been deployed.
Add the changes to git.
Set up a remote repository on GitHub or your version control provider of choice and upload your files.
1. b) Your Co-Worker
You might want to use a second editor and terminal window to simulate the collaboration.
Clone the remote repository.
If you run amplify status now, it will throw an error.
Initialize the master branch using the prod environment.
In a real-world scenario, your collaborators might have different CLI profiles. Make sure everyone has the permissions to access the relevant resources.
Now, your co-worker has the environment, too.
1. c) You
Change back to env-one/. We want to add another feature.
Amplify environments work hand in hand with Git. We can create our sandbox by creating a new git branch, and running amplify init within it.
Typically, you would give the branch a more meaningful name than "sandbox". Alternatively to amplify init we could've also used amplify env add.
The changes for this environment aren't deployed yet.
Note how it says our current environment is sandboxone. To list all environments run amplify env list.
In this list, our current environment is marked with a star (*).
Let's also add an API to the sandbox and deploy it.
The sandbox now has an API as well as authentication. If we made our changes and want to merge them into the production environment, we can do that by using Git.
If you are done using your sandbox environment, you can delete it by running amplify env remove.
1. d) Your Co-Worker
Your co-worker can get the changes by running amplify env pull. cd into env-two/ and do that.
When those commands are done your co-worker is synced up with the master branch.
Good job! ππ» Now you know how to collaborate using sandboxes. Next, we'll explore collaboration with ...
2. Sharing backends
Sharing backends is as easy as using the sandbox. I like to mix both approaches. Meaning each team member develops using their custom sandbox, but before changes get merged into production, they get merged into the development environment.
2. a) You
Similar to the sandbox approach, create a dev branch with Git and an environment with Amplify in env-one/.
Your co-worker, in env-two/, can get the remote branch by creating his local version and then fetching it.
Your co-worker can list all environments to see both the prod and the dev branch.
Moving code from dev to prod works the same way as we showed earlier when we moved from sandbox to prod.
If you liked this tutorial, you might want to read my other articles about Amplify because they cover advanced use cases and make you an Amplify expert.
Summary
Remember: Use Amplify's env command parallel to Git.
We understood the two ways - sandbox and shared environments - to use env by looking at an example for each.
To clean up run amplify delete.
Learn senior fullstack secrets
Subscribe to my newsletter for weekly updates on new videos, articles, and courses. You'll also get exclusive bonus content and discounts.