6-part series - (4) Create, clone a CodeCommit Repo, move-push the Dockerfile and index.html to remote repository master branch
Tags: codepipeline, codecommit, docker, ecr
Published: 2023-02-16 03:22:31

In this **6-part series on configuring a CI/CD pipeline using Customized Docker image on an Apache Web Server, Application Load Balancer, ECS, ECR, CodeCommit, CodeBuild, CodeDeply services** -

In the **4th article**, We will create a CodeCommit Repository, and with proper permissions to IAM User, Using SSH public keys we will authenticate access to AWS CodeCommit, then Clone the repository and we will move the Dockerfile and index.html inside the repo and push them to remote repository master branch.


**1st article**
- As a reference, please read my **[1st article - 6-part series - (1) Deploy a Custom Docker Image with Centos on an Apache web server and Save it to AWS ECR Repository](https://dev.to/aws-builders/6-part-series-1-deploy-a-custom-docker-image-with-centos-on-an-apache-web-server-and-save-it-to-aws-ecr-repository-186)**

**2nd article**
- As a reference, please read my **[2nd article - 6-part series - (2) Create Task Definition in ECS and Application Load balancer for the Task to be run on Fargate Cluster](https://dev.to/aws-builders/6-part-series-2-create-task-definition-in-ecs-and-application-load-balancer-for-the-task-to-be-run-on-fargate-cluster-1ohm)**

**3rd article**
- As a reference, please read my **[3rd article - 6-part series - (3) Create Fargate Cluster, and a Service which can access customized Docker image ](https://dev.to/aws-builders/6-part-series-3-create-fargate-cluster-and-a-service-which-can-access-customized-docker-image-1dl7)**

_**Let’s get started!**_

Please visit my **[GitHub Repository for Docker/ECS/ECR articles](https://github.com/awsmine/Docker_projects)** on various topics being updated on constant basis.

# Objectives:

**1.** Create a CodeCommit repository called "my-codecommit-repo"

**2.** Assign permissions to IAM User to access CodeCommit

**3.** Use SSH public keys to authenticate access to AWS CodeCommit

**4.** Clone the repository

**5.** Move the Dockerfile and index.html inside the repo and push them to remote repository master branch



# Pre-requisites:
- AWS user account with admin access, not a root account.
- AWS CLI.

# Resources Used:

**[CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)**

**[AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html)**


# Steps for implementation to this project

## 1. Create a CodeCommit repository called "my-codecommit-repo"

- **`On the Codecommit dashboard, Repositories, Create repository, my-codecommit-repo, Description - my-codecommit-repo, Tags, Name, Value - my-codecommit-repo`**

- Create 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swo5ty0wrzxd2orh0qzp.png)



- On the connection steps page

- There are 3 types of connections, HTTPS, SSH and HTTPS (GRC)
- I am going to be working with SSH connection



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtdy3fwfnc3jr5r1v3r6.png)


## 2. Assign permissions to IAM User to access CodeCommit

- **`On IAM Dashboard, Users, goti2, Permissions tab, Add permissions, Attach policies directly, Search and select AWSCodeCommitPowerUser, Next, Review and Add permissions`**


## 3. Use SSH public keys to authenticate access to AWS CodeCommit


- SSH into the EC2 Instance thru Putty

- Go to the docker folder using the command

```
sudo su
cd /opt/docker
```

- Install git

```
yum install git -y
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewwiyq7opwtdg20ges83.png)


- Generate the public and private keys for Git and CodeCommit

```
ssh-keygen
```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86pn64l27x0z0l0xb6ku.png)


- Go to the SSH keys location

```
cd ~/.ssh/
```
- command "ll" to check.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3n9rhq53dmh28vml44t.png)


- In order to access the CodeCommit through SSH, we need to copy and paste the SSH public key in IAM User

- Use the following command to copy the public key content

```
cat id_rsa.pub

```

- **`On the IAM console, Users, click on the user - goti2, Go to the Security credentials tab and under SSH Keys for AWS CodeCommit, Click on Upload SSH public key`**

**Make sure to paste it properly to avoid errors in the future**

- **`Once pasted, click on Upload SSH public key, you can see the SSH Key ID`**

- Copy the SSH Key ID for future reference.

```
APKAT7SS6ZVXC6LRIFXL
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dixjphqo5fles8cuajq8.png)


- **`Back to the CodeCommit, SSH connection page, Linux, Step 3,
Copy the 3 lines of code in step 3`**


```
Step 3: Edit Local SSH Configuration

Edit your SSH configuration file named "config" in your local ~/.ssh directory. Add the following lines to the file, where the value for User is the SSH Key ID you copied in Step 2.

Host git-codecommit.*.amazonaws.com
User Your-IAM-SSH-Key-ID-Here
IdentityFile ~/.ssh/Your-Private-Key-File-Name-Here

Once you have saved the file, make sure it has the right permissions by running the following command in the ~/.ssh directory.

chmod 600 config
```

- In the .ssh directory, Create a file - name it **config**

```
Host git-codecommit.*.amazonaws.com
User APKAT7SS6ZVXC6LRIFXL
IdentityFile ~/.ssh/id_rsa
```

- To change the permissions for the config file

```
chmod 600 config
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hoj4ol1os4z4f7etl202.png)


 - Go to the docker folder 

```
cd /opt/docker

```


## 4. Clone the repository


- Clone command from the SSH page in CodeCommit

```
Clone your repository to your local computer and start working on code. Run the following command

git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-codecommit-repo
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g654q4cjdp5y1hxp195.png)



## 5. Move the Dockerfile and index.html inside the repo and push them to remote repository master branch


- Go to the docker folder 

```
sudo su
cd /opt/docker
```

- Move the dockerfile and index.html file to my-codecommit-repo


```
mv dockerfile index.html my-codecommit-repo/

cd my-codecommit-repo

ls -lt
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqfs1ritafw98llnv279.png)

- to add all the files to the git 

```
git add .

```

- Check the status

```
git status
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3tio48gnb85tf2vy0zn.png)


- Commit the files

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7lyqd48obpmntlt71jk.png)


- Pushing the files into the remote repository’s master branch

```
git push

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/323eajq4d57kuuxwttra.png)

- to the CodeCommit repository and check the files.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmk1fiwpq32xum92ybzx.png)


# What we have done so far

- We have successfully created a CodeCommit Repository, authenticated using SSH public keys, cloned the repository, and moved the Dockerfile and index.html inside the repo and pushed them to remote repository master branch.