When you start developing in AWS Amplify without collaborating with team members, it is pretty straight-forward to have your changes just to yourself and adding new features at will. But enter your team into the mix and it becomes tricky, especially when you are following the Git workflow to handle branches for working on separate features.
This post is going to address that how you can manage different Git branches while mapping them to corresponding Amplify environments and how to finally merge them to your develop and master branch. This post assumes you know the concepts of Git branches and also some basic knowledge of how Amplify works.
This post also assumes that the team members working on their own Amplify environments are working in a single AWS account so they can see and manage the resources created by their Amplify app. This post does not describe how to work with Amplify using multiple AWS accounts.
This post assumes that you have the following setup already:
- A javascript frontend project(Angular/React) in a git repo.
- Amplify CLI installed.
- An AWS Profile with permissions to deploy to your AWS account for each developer
Initial setup
Master branch/prod environment
As in the Git workflow, we have the master branch as the default and the develop branch is the one where ongoing development work is kept and all the feature branches are merged into it.
Assuming we just created a new Git repository and are in the default master branch, we will start from there. In the master branch, run the following commands to initialize a new Amplify app and your first Amplify environment(we will name this environment prod, name it so it implies that it is the production level environment):
amplify init
amplify add auth
amplify push
git add --all
git commit -m "Master initial commit"
git push origin master
After initializing the Amplify app, we add a new service called auth, which will create a Cognito user pool for us. We then push the changes to Amplify console and commit them to our Git repo as well. You should now be able to see your app in your Amplify console.
develop branch/dev environment
Now let’s create a new develop branch and map it to an environment called dev. The new environment will be created on top of the prod environment; all the resources in the prod environment are going to be present in the dev environment.
git checkout -b develop
amplify init
...
? Do you want to use an existing environment? No
? Choose the environment you would like to use: dev
...
amplify push
git add --all && git commit -m "Develop initial commit" && git push origin develop
Our 2 main branches for the Git workflow are now set along with their corresponding Amplify environments.
Feature Environment for team mate
Now suppose that one of your team mate wants to work on a new feature for Amplify and wouldn’t want to bother the dev or prod environments until they are done with their feature. We will create a new Git branch and environment called sandbox.
git checkout -b sandbox
amplify init
? Do you want to use an existing environment? No
? Choose the environment you would like to use: sandbox
...
amplify add api
amplify push
git add --all && git commit -m "sandbox commit" && git push origin sandbox
We initialize a new environment, add api
to the app and push our changes to Amplify and Git. You can see those changes in the Amplify console, and also check that the dev and prod environments don’t have the changes from the sandbox environment yet!
Merging changes to develop and master
Now that the team mate is done with the sandbox feature, we are going to merge those changes to develop and then to master:
git checkout develop
git merge sandbox
amplify env checkout dev
amplify push
git add --all && git commit -m "Develop merge commit" && git push origin develop
Check your Amplify app in the console. Your dev environment now has the API and functions added as a result of merging the sandbox environment to it.
removing sandbox environment
If you would like to remove the sandbox environment and git branch:
amplify env remove sandbox
amplify env list
git branch -d sandbox
Summary
This post showed you how to have separate environments for Amplify, how to add new environments for feature and development purposes, how a developer can have their own sandbox for their own development work and finally, how to merge changes to the Amplify app to the main develop and master branches.