Continuous Integration and Continuous Deployment also known as CI/CD is a set of software development practices that revolutionizes the way applications are built, tested, and delivered. With CI, developers frequently integrate their code changes into a shared repository, ensuring early detection and testing of integration issues. On the other hand, CD requires continuously deploying code to production environments, making the delivery process seamless and error-free. Adopting CI/CD practices, empowers development teams to deliver innovative software at an accelerated pace while maintaining a higher standard of reliability.
In today’s fast-paced development landscape, establishing a robust and streamlined CI/CD (Continuous Integration and Continuous Deployment) pipeline has become indispensable. In this blog post, we embark on an exciting journey of setting up an AWS CodeCommit repository, hosting it on AWS Amplify, and implementing best CI/CD practices to automate our software delivery.
Setting up AWS CodeCommit
In this section, we will work on setting up a repository and moving a local project onto the AWS Cloud using the AWS CLI, Git, your machine’s terminal, and AWS CodeCommit:
After logging in to your AWS Management Console, search for the AWS CodeCommit service.
![](https://digico.solutions/wp-content/uploads/2023/07/Picture1-1.png)
Navigate to the Repositories section in AWS CodeCommit, and click ‘Create repository’.
![](https://digico.solutions/wp-content/uploads/2023/07/Picture2-1.png)
Give your repository a memorable name and relevant description, then click ‘Create’.
![](https://digico.solutions/wp-content/uploads/2023/07/Picture3-1.png)
You will be redirected to a page containing instructions on how to connect your local machine to your remote AWS CodeCommit repository.
![](https://digico.solutions/wp-content/uploads/2023/07/Picture4-1.png)
As shown below, your repository is initially empty and you may add files manually using the AWS CodeCommit UI.
![](https://digico.solutions/wp-content/uploads/2023/07/Picture5-1.png)
On your local machine, open your terminal to move your project to your newly created AWS CodeCommit repository. The first thing to do is to make sure that you have AWS CLI installed on your machine by executing the following command in your terminal:
aws --version
You should get an output printed to your terminal similar to the one below, indicating that a version of aws-cli is installed on your machine.
![](https://digico.solutions/wp-content/uploads/2023/07/Picture6-1.png)
The next step is to configure Access Keys for your IAM User, which you used to create your AWS CodeCommit repository. Navigate to the IAM service and select the IAM user from the Users list. Make sure your IAM User is given the AWSCodeCommitPowerUser permission policy.
In your IAM User Security Credentials:
![](https://digico.solutions/wp-content/uploads/2023/07/Picture7-2.png)
Go to the Access Keys section and click ‘Create access key’:
![](https://digico.solutions/wp-content/uploads/2023/07/Picture8-1.png)
Choose the use case to be the CLI:
![](https://digico.solutions/wp-content/uploads/2023/07/Picture9-1.png)
Add a description to the access keys to state what you will use your access key for, and click the create button
![](https://digico.solutions/wp-content/uploads/2023/07/Picture10-1.png)
You will be navigated to a page which shows your access key and secret access key like the screenshot below:
![](https://digico.solutions/wp-content/uploads/2023/07/Picture11-1.png)
Back in your terminal, execute the command below to login using your newly created access key credentials:
aws configure
Back in your terminal, execute the command below to login using your newly created access key credentials:
![](https://digico.solutions/wp-content/uploads/2023/07/Picture12-1.png)
Finally, make sure you have git installed on your machine, by executing the following command in your terminal:
$ git --version
You should get an output indicating the git version that you have installed.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture13-1.png)
The next step is to navigate to the directory of your project using the cd
command in your terminal.
cd falcon-ui //replace falcon-ui with the path to your project
Once you have navigated to your project (in this case falcon-ui) in your terminal, execute the following git command in order to use a branch called main as your default branch:
git config --local init.defaultBranch main
To create a repository locally from your project on your machine, execute the following command:
git init
Add all your project’s files to be tracked by git using the command below in your terminal:
git add .
Then, to commit your changes, use the commit command in your terminal with a custom commit message in between quotations that describes what changes you are adding to your project:
git commit -m "Initial commit adding all project files"
This will show you all the files you have created or changed since your last commit:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture14-1.png)
At this point you have created your repository locally including your entire project and project configurations. You now need to push your project to AWS CodeCommit. To do this, you need to make AWS CodeCommit credentials for the same IAM User that created the AWS CodeCommit repository.
Go to the Security Credentials page from the profile menu on the top right of the screen in your AWS Management Console:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture15-1.png)
Click the ‘AWS Code Commit credentials’ tab and scroll to HTTPS Git credentials for AWS CodeCommit:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture16-1.png)
Click ‘Generate credentials’:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture17-1.png)
Download your credentials if you think you might have to use them later. Otherwise, copy them and save them in a note for immediate use.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture18.png)
After closing the window above, you will see that your credentials are Active.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture19.png)
To push your code to the AWS CodeCommit repository, first copy your AWS CodeCommit Repository URL by clicking Clone URL > Clone HTTPS:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture20.png)
Then execute the following git push
command in your machine’s terminal from within your project directory with the URL you just copied to push your project to AWS CodeCommit:
git push --all
This will open a Git Credential Manager window as shown below. Enter the username and password which you created for your IAM User and copied in HTTPS Git credentials for AWS CodeCommit:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture21.png)
When clicking continue, the authorization should succeed and you should get a message in your terminal, which indicates that your files are being written to your AWS CodeCommit repository.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture22.png)
You will then be able to view and edit your files directly within your AWS CodeCommit repository:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture23.png)
Set up hosting on Amplify
n this section, we will connect our AWS CodeCommit repository to a hosting environment on Amplify to host our project website on the web.
Navigate to your Amplify App, and click the ‘Hosting environments’ tab. Choose AWS CodeCommit, then click ‘Connect branch’:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture24.png)
You will be prompted to choose the repository and branch you would like to connect to. Choose your AWS CodeCommit repository and select the branch you would like to deploy.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture25.png)
On the Build settings page, your App name will automatically be selected. For the Environment setting, choose ‘create new environment’ and name your environment. This will tick ‘Enable full-stack continuous deployments (CI/CD)’ by default which will automatically deploy your updated project whenever you commit a new change to your connected AWS CodeCommit repository.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture26.png)
Below this, you will be prompted to assign a role in the build settings. Click ‘Create new role’:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture27.png)
This will redirect you to the IAM role editor where you can select and confirm the role’s use case as Amplify Backend Deployment:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture28.png)
This will create a role for you with the default permission policies associated with this use case:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture29.png)
Give your role a name and click Create:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture30.png)
Back on the Build Settings page, click ‘Refresh existing roles’
![](https://digico.solutions/wp-content/uploads/2023/08/Picture31.png)
Then select the role that you just created
![](https://digico.solutions/wp-content/uploads/2023/08/Picture32.png)
In the Advanced settings of the Build Settings page, add an environment variable called API KEY with the value of your falcon-sagemaker API key.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture33.png)
Finally, click ‘Save and deploy:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture34.png)
Your web app will go through the provisioning, build, then finally deployment stage if there are no errors in your project. Once the web app is deployed you can use the URL from the Domain setting shown below to access your web app from your browser.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture35.png)
Update settings in API Gateway
In this section, changes will be made to the API created as an endpoint called from our repository code. Previously, our API allowed access from our undeployed website running on localhost:3000. We will update this to the new domain of our web app.
To do this, go to your API’s method, and click ‘Enable CORS’ in the Actions menu.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture36.png)
In the Access-Control-Allow-Headers section, add Access-Control-Allow-Origin
to the list of allowed headers, and in the Access-Control-Allow-Origin field add your web app’s URL in between single quotation marks in order to allow access to your API from your website. Then click Enable CORS.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture37.png)
Deploy your API from the Actions menu to apply the changes:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture38.png)
Note: If you deploy your API to a new stage as shown below, you will need to update the URL used in your web app code to invoke the API:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture39.png)
The Invoke URL for each stage can be found in the Stages section of your API.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture40.png)
Update API call in code:
n this section, we will update the code to reflect our deployment changes.
As shown below, we will update the code in AWS CodeCommit.
Navigate to the src folder App.js file.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture41.png)
Edit the code to use the API key defined in the environment variables, which we set in Amplify Build Settings during deployment. Next, we will update the fetch url value to be the Invoke URL of the newly deployed API. Finally, in the headers object, edit the value of the Access-Control-Allow-Origin header to be the domain of your deployed website to allow access to the invoked API from your web application.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture42.png)
To finalize the changes, commit the changes with a descriptive commit message.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture43.png)
Once committed, the changes will be built and deployed again automatically by AWS Amplify.
Our Example
We can see that we were able to build our website using AWS Amplify in under 4 minutes. This highlights the speed at which we can take our code from built to ready to use.
![](https://digico.solutions/wp-content/uploads/2023/08/Picture44.png)
In the Build tab of our AWS Amplify deployment, we were able to view the build logs which referred to our AWS CodeCommit repository:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture45.png)
In the Deploy Tab, we can also keep track of the corresponding logs in case of any errors:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture46.png)
Once deployed, the end result shows our website up and running, where it can be accessed from a browser using the URL provided by AWS Amplify:
![](https://digico.solutions/wp-content/uploads/2023/08/Picture47.png)
Conclusion
The journey from setting up a AWS CodeCommit repository to hosting it on Amplify and implementing CI/CD practices has transformed our development process from static to dynamic, automating deployment of changes.Thus, by leveraging such AWS services, we can build more agile and scalable application delivery pipelines. As the era of CI/CD continues to revolutionize software development, we stand confidently with these practical tools at our fingertips.