Change GitHub repository name and reflect the change in AWS amplify

No doubt, changing the name of a GitHub repository can feel like a simple task—just a few clicks, and you're done. But if that repository is connected to an AWS Amplify app, things get a bit more complicated. Why? Because AWS Amplify links your app to the repository using its URL, and when the repository name changes, so does the URL. I have faced a similar challenge today. At first it seems like a 10-15 minutes task but took more than an hour to find the right approach.

Scenario:

I have a GitHub repository which is connected to AWS amplify app. Now I want to change the GitHub repository name and reflect the same on AWS amplify. Since My amplify app is connected with a specific branch of that repository so we have to reconnect the amplify app to the repository. Why is that ? Because changing the repository name also changes the Github url and AWS amplify app is connected to that repository by the Github url. If we don’t able to change the repository url in AWS amplify app or reconnect with the changed Github repository then the auto build functionality will not work as before.

Problem faced:

  • Once created an amplify app and connected a GitHub repository and branch , you can not change the GitHub repository url or disconnect from the previous repository.

  • You can only change the branch from AWS console or disconnect from the previous branch.

  • Once you change the branch or disconnect the branch, the domain connected with the previous branch will also be vanished.

  • When trying to reconnect with repository it was trying to connect to old repository url which is not there in Github

Goal :

  • Change the Github repository name

  • Change the GitHub repository url for the AWS amplify app

  • Reconnect the AWS amplify app with the new repository URL

  • Check if the auto build is working as expected or not when new code is pushed to that particular branch.

Things to understand :

Changing the repository URL is not supported for an AWS amplify app. One workaround is to create a new app and connect the new app to the changed Github repository. But this will be a daunting task as we have to copy all the environment variables and build settings to that newly created amplify app which we don’t want. Since AWS console don’t allow us to do this, aws-cli provides this functionality. In my experience, AWS-CLI is the go to solution when we want some specific job done. The same is true in this case as well.

Solution:

  • Change the GitHub repository name to whatever name you want. Go to the repository settings and change the desired name as shown below in the screenshot.

  • Go to the desired repository folder in your machine and change the remote origin URL of that git repository folder. For changing the git repository url run the following git command

      git remote set-url origin new-git-repository-url
    
  • Now run this following command to change the repository url for your amplify app. You need to have AWS-CLI installed in your system and have proper permission to run this command. These are the prerequisites for running this command

      aws amplify update-app --app-id AMPLIFY_APP_ID --repository REPOSITORY_URL --access-token ACCESS_TOKEN
    
    • The AMPLIFY_APP_ID is the last bit of the app ARN (e.g. dXXXXXXXXXXXXX)

    • For GitHub repositories, the access token is a Personal Access Token. Can be found under settings/developer-settings/Personal access token. If there is none generate one. For this purpose I created a classic personal token.

  • Now go to the AWS amplify app to check whether the repository URL is actually changed or not. Go the amplify app then navigate to the branch settings. I have blurred the app name, repository url and branch name for security reason. At this stage you will see the repository URL is changed.

  • Now we need to reconnect the amplify app to the new repository URL. Click the “Reconnect Repository“ button as shown in the above screenshot. Go through the process of reconnecting your GitHub repository as shown in the below screenshots.

  • Now push some new code to the desired branch, one commented line is enough. You will see the amplify hook is working and the deployment is started. We are done

When we first started the process it seems like a lot of work before finding the actual solution. We have spent hours before finding the actual solid solution which is often times the simplest solution. This is also the case this time. Changing the repository URL for an AWS Amplify app may seem like a daunting challenge due to the lack of direct support in the AWS Management Console. While creating a new app and manually transferring all configurations is one way to handle this, it can be time-consuming and error-prone. Thankfully, AWS CLI offers a powerful and efficient alternative, providing the functionality needed to update the repository URL without the need to recreate the app.

The AWS CLI proves its worth in scenarios like this, where precise control and automation are required. Leveraging CLI commands not only saves time but also ensures that environment variables, build settings, and other configurations remain intact, making it the go-to solution for developers who seek efficiency and reliability. By utilizing this approach, you can streamline your workflows and focus more on building and deploying applications, rather than spending unnecessary effort on administrative tasks.