---
# System prepended metadata

title: 'CFH CI/CD with CodeCommit, CodeBuild, CodePipeline introduction + Lab'

---

# CFH CI/CD with CodeCommit, CodeBuild, CodePipeline introduction + Lab

## Lab 1. Basic Scenario - Version Control & Continuous Integration(CI)

### Getting started to turn on your AWS EC2 instance

* Create an AWS EC2 with cfh-demo-ec2
    * ![](https://i.imgur.com/TkGGkbq.png)

    * ![](https://i.imgur.com/zlvdRgh.jpg)

* Set the permission for your AWS EC2 with IAM
    * Got to IAM, and create an AWS Role cfh-demo and append to EC2 
        * Create a Role
            * ![](https://i.imgur.com/N4tnWok.png)

            * ![](https://i.imgur.com/sThcDY0.png)

        * Add “AmazonSSMManagedInstanceCore”(for the web console login use)and “AmazonEC2ContainerRegistryReadOnly“(for the AWS ECR pull container image use)
            * ![](https://i.imgur.com/EPzrgWR.png)

            * ![](https://i.imgur.com/EH7Fqc1.png)

            * Name is cfh-demo
                * ![](https://i.imgur.com/YNnV0gt.png)

                * ![](https://i.imgur.com/51COb8o.png)

        * Result
            * ![](https://i.imgur.com/6RS56uh.png)

    * Go to your AWS EC2 and modify the IAM Role with cfh-demo
        * ![](https://i.imgur.com/A3r8yua.png)

        * ![](https://i.imgur.com/Cl472ki.png)


### Getting started with AWS CodeCommit

* Go to AWS CodeCommit and create a repository
    * Create a repository cfh_demo_repo with AWS Console UI of AWS CodeCommit
        * ![](https://i.imgur.com/7Xs4ck2.png)

        * ![](https://i.imgur.com/z1Dyo0K.png)

* Go to your AWS Cloud9
    * Initialize your repository in your command line
        ```
        git clone {AWS CodeCommit URL}`
        ```
    ex:
        ```
        git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/cfh_demo_repo`
        ```
* Push your repository to AWS CodeCommit
    * Install python3
        ```
        yum install python3 -y
        python3 --version
        ```
    * Install the django with version 2
        ```
        pip3 install django==2
        python3 -m django --version
        ```
    * Initiate the basic project
        ```
        python3 -m django startproject mysite
        ```
    * Copy file to cfh_demo_repo folder to initial commit and push
    ```
    cp -R mysite/* /home/ec2-user/environment/cfh_demo_repo
    ```
    * Go to the folder
        ```
        cd /home/ec2-user/environment/cfh_demo_repo
        ```
    * Basic configure
        ```
        pip3 install django==2
        python3 manage.py migrate
        ```
    * Allow all hosts with 
        ```
        sed -i 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[\"*\"\]/g' mysite/settings.py > settings.py
        ```
    * Commit your code and push to the server
        ```
        git add .
        git commit -m "Initiate" && git push
        ```



### Getting Started with AWS CodeBuild
* In your AWS Cloud9, write a simple Unit Test for Continuous Integration (CI)
    * Go to the repository folder and create some unit test case
        ```
        cd /home/ec2-user/environment/cfh_demo_repo

        mkdir tests
        cp mysite/__init__.py tests/

        touch test_demo.py
        ```
    * Start to write/add a test (tests/test_demo.py) and remember to Save this modification
        * ![](https://i.imgur.com/wLyehtu.png)

    * You can copy the code as below ⬇️:
        ```
        from unittest import TestCase


                class YourTestClass(TestCase):
                    def test_something_that_will_pass(self):
                        self.assertFalse(False)

                    # def test_something_that_will_fail(self):
                    #     self.assertTrue(False)
        ```
    * Remember to push to your repository
        ```
        cd /home/ec2-user/environment/cfh_demo_repo
        git add .
        git commit -m "Add the unit test"
        git push
        ```
* Set up your buildspec.yml - it can let AWS CodeCommit run the test for you!
    * Create a file buildspec.yml
        * ![](https://i.imgur.com/SoiR8oJ.png)

    * Copy codes as below
        ```
        version: 0.2

        phases:
          pre_build:
            commands:
              - pip3 install django==2
          build:
            commands:
            # Start to run your unit test
              - python3 manage.py test

        artifacts:
          files:
            - "**/*"
        ```
    * Save this file as buildspec.yml and choose the right path cfh_demo_repo
        * ![](https://i.imgur.com/HQFkuHP.png)

    * And you will see this screen
        * ![](https://i.imgur.com/eIhg2BE.png)

    * Remember to push to your repository
        ```
        cd /home/ec2-user/environment/cfh_demo_repo
        git add .
        git commit -m "Add buildspec.yml for the AWS CodeBuild"
        git push
        ```
* Go to AWS CodeBuild to build up your AWS CodeBuild
    * Enter your Project name cfh-demo-master
        * ![](https://i.imgur.com/pBJl6LO.png)

    * Set the Source & Branch
        * ![](https://i.imgur.com/HwkItBH.png)

        * ![](https://i.imgur.com/IVLgJdv.png)

    * Set your Environment
        * ![](https://i.imgur.com/gMe2AxS.png)

        * ![](https://i.imgur.com/jlD84QM.png)

    * Set your Logs for monitoring
        * ![](https://i.imgur.com/ikbfxCH.png)

* Trigger your CI(Continuous Integration)
    * Click the Start build to test
        * ![](https://i.imgur.com/tdvP8ji.png)

    * You can check the Build logs & Phase details
        * ![](https://i.imgur.com/A2Jaqab.png)

        * ![](https://i.imgur.com/OlbQBCB.png)




### Getting Started with AWS CodePipeline
* Go to AWS CodePipeline and create your master(main) pipeline cfh-demo-master
    * ![](https://i.imgur.com/RabjS25.png)

    * ![](https://i.imgur.com/0LyRUKu.png)

    * ![](https://i.imgur.com/kyLotzB.png)

    * ![](https://i.imgur.com/8c61X9A.png)

    * ![](https://i.imgur.com/iR1y8Lh.png)

* Click Next
* Waiting for about 1 minute
    * ![](https://i.imgur.com/FWipHgL.png)

*  Start to test your automation Continuous Integration(CI)
    *  Go to AWS Cloud9 and update your code to trigger the AWS CodePipeline
        * Update test_demo.py
            ```
                from unittest import TestCase


                class YourTestClass(TestCase):
                    def test_something_that_will_pass(self):
                        # Add a new line for testing the AWS CodePipelin & Continuous Integration(CI)
                        print("This is just a test")
                        self.assertFalse(False)

                    # def test_something_that_will_fail(self):
                    #     self.assertTrue(False)
            ```
        * And git push it
            * ![](https://i.imgur.com/J89WuTK.png)

                ```
                cd /home/ec2-user/environment/cfh_demo_repo
                git add .
                git commit -m "Testing the AWS CodePipelin & Continuous Integration(CI)"
                git push
                ```
        * Check your AWS CodePipeline
            * ![](https://i.imgur.com/XKEmQIc.png)

            *  Congratulations! 🎉 Your basic Continuous Integration(CI) completed!

---

## Lab 2. Advanced Scenario - Continuous Delivery & Continuous Deployment(CD)

### Set up your EC2 environment for the deployment
* Connect your EC2 instance
    * ![](https://i.imgur.com/rBDFhKz.png)

    * ![](https://i.imgur.com/Ojhb6BC.png)

    * ![](https://i.imgur.com/EESoC3z.png)

* Set up your docker
    * Install docker
        ```
        sudo yum install docker -y
        ```
    *  Start docker
        ```
        sudo systemctl start docker
        ```
    *  Allow Session Manager can execute docker
        ```
        sudo setfacl --modify user:ssm-user:rw /var/run/docker.sock
        ```



### Set up AWS CodeBuild of CI(Continuous Integration) for the AWS Elastic Container Registry(ECR)
* Create a repository in the AWS AWS Elastic Container Registry(ECR)
    * Go to AWS ECR(Elastic Container Registry) and create a repository cfh-demo-master
        * ![](https://i.imgur.com/sJYhGod.png)

        * ![](https://i.imgur.com/BWUXxhB.png)

        * ![](https://i.imgur.com/zabHtfy.png)

    * Click it to get more information for pushing docker images
        * ![](https://i.imgur.com/lX2xZj9.png)

        * ![](https://i.imgur.com/b2NHFcz.png)

        * ![](https://i.imgur.com/UqWX9Dj.png)

* Go to AWS Cloud9 and create a Dockerfile to build the Docker image
    * Create a Dockerfile
        * ![](https://i.imgur.com/bv9QXVB.png)

    * You can copy & paste following the below
        ```
        FROM python:3.7.16

        USER root
        WORKDIR /usr/src

        ENV TZ Asia/Taipei

        RUN pip3 install django==2

        COPY . /usr/src

        ENTRYPOINT ["python3","manage.py","runserver","0.0.0.0:8080"]
        ```
        * ![](https://i.imgur.com/pyNyEM9.png)

    * Save it as Dockerfile
        * ![](https://i.imgur.com/8CN13b9.png)

        * ![](https://i.imgur.com/wsj9JMU.png)

* Go to IAM Role to add AmazonEC2ContainerRegistryFullAccess and AmazonElasticContainerRegistryPublicFullAccess to your AWS CodeBuild role (codebuild-cfh-demo-master-service-role) 
    * ![](https://i.imgur.com/9U5yy1q.png)

* You can search with the keyword “containerregistry”
    * ![](https://i.imgur.com/dILHeRT.png)

* Modify your buildspec.yml with the AWS ECR sample commands
    * ![](https://i.imgur.com/1oaZBOT.png)

* Go to AWS Cloud9 to adjust the buildspec.yml
    * ![](https://i.imgur.com/7n5BIaS.png)

* And git push to AWS CodeCommit
    ```
    git add . && git commit -m "Add Dockerfile and add AWS ECR commands to buildspec.yml" && git push
    ```



### Set up your CD(Continuous Delivery) AWS CodeBuild
* Add the specific deployspec.yml with AWS Cloud9
    * ![](https://i.imgur.com/FFVw9d5.png)

* After click New File, please MODIFY {parameters} below codes and paste on the new file
    ```
    version: 0.2

    phases:
      build:
        commands:
        # Start to run your unit test
          - aws ssm send-command
            --document-name "AWS-RunShellScript"
            --targets '[{"Key":"InstanceIds","Values":["{YOUR EC2 INSTANCEID}"]}]'
            --parameters '{"commands":[
            "sudo su",
            "systemctl start docker",
            "docker rm -f cfh-demo",
            "{YOUR ECR LOGIN COMMAND}",
            "docker pull {YOUR ECR URL with TAG}",
            "docker run -d --name cfh-demo -p 80:8080 {YOUR ECR URL with TAG}"]}'

    artifacts:
      files:
        - "**/*"
    ```
* Sample code(please don’t directly copy, remember to modify the parameter)
    ```
    version: 0.2

    phases:
      build:
        commands:
        # Start to run your unit test
          - aws ssm send-command
            --document-name "AWS-RunShellScript"
            --targets '[{"Key":"InstanceIds","Values":["i-0d48c3c55c19ec5f0"]}]'
            --parameters '{"commands":[
            "sudo su",
            "systemctl start docker",
            "docker rm -f cfh-demo",
            "aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 915311728802.dkr.ecr.us-west-2.amazonaws.com",
            "docker pull 915311728802.dkr.ecr.us-west-2.amazonaws.com/cfh-demo-master:latest",
            "docker run -d --name cfh-demo -p 80:8080 915311728802.dkr.ecr.us-west-2.amazonaws.com/cfh-demo-master:latest"]}'

    artifacts:
      files:
        - "**/*"
    ```
    * ![](https://i.imgur.com/BjS5Waf.png)

* And the save it with the name “deployspec.yml”, please do remember to chose the right path with your project “cfh_demo_repo”
    * ![](https://i.imgur.com/RIk2ohg.png)

    * ![](https://i.imgur.com/OdO8Psm.png)

    * ![](https://i.imgur.com/sJa09WQ.png)

* And then git add, git commit, and git push to the master branch
    ```
    git add . && git commit -m "Add the deployspec.yml" && git push
    ```
*  Go to AWS CodeBuild and start to create a AWS CodeBuild with cfh-demo-master-deploy for the deployment process
    * ![](https://i.imgur.com/cBC2jII.png)

    * ![](https://i.imgur.com/vRE54p7.png)

    * ![](https://i.imgur.com/HefHbdH.png)

    * ![](https://i.imgur.com/EeLKtE7.png)

* Enter deployspec.yml
    * ![](https://i.imgur.com/t0snCZO.png)

    * ![](https://i.imgur.com/WcCi3qg.png)

*  Go to IAM Role to add a permission “AmazonSSMAutomationRole” for this role “codebuild-cfh-demo-master-deploy-service-role”
    * ![](https://i.imgur.com/JxTfAAC.png)

    * ![](https://i.imgur.com/9l05kjH.png)

    * ![](https://i.imgur.com/CQQ3GxE.png)

*  Go to your master AWS CodePipeline to add AWS CodeBuild for the deployment process
    * Click Edit
        * ![](https://i.imgur.com/aXaodiH.png)

    * Click Add stage
        * ![](https://i.imgur.com/CCFtxSb.png)

    * Enter the stage name Deploy
        * ![](https://i.imgur.com/gvzZHQn.png)

    * Click Add action group
        * ![](https://i.imgur.com/b6CATQW.png)

    * Enter the information as below and click Done
        * ![](https://i.imgur.com/uwoRjq3.png)

    *  Click Done
        * ![](https://i.imgur.com/qWSPg5K.png)

    * Click Save
        * ![](https://i.imgur.com/BCI8rKE.png)




### Test your CI / CD pipeline
* Click Release change to test it
    * ![](https://i.imgur.com/b5THF4T.png)


 

### Insert a manual approve process
* Click Add stage under the Build Stage
    * ![](https://i.imgur.com/RAurHYU.png)

*   Enter the name Approval and click Add stage
    * ![](https://i.imgur.com/fSFQGSs.png)

* Click Add action group
    * ![](https://i.imgur.com/uo2xj4X.png)

* Enter the information as below and click Done
    * ![](https://i.imgur.com/mQk1JxR.png)

*  Remember to click Save
    * ![](https://i.imgur.com/EbG1J6M.png)




### Test your complete process
* Click Release change
    * ![](https://i.imgur.com/H9ChPQ2.png)

* When the pipeline goes to this process Approval, it will need to click the Review to get the approval.
    * ![](https://i.imgur.com/sYK5vzU.png)

*  You need to click the Approval to let the process keeps going
    * ![](https://i.imgur.com/uanESQc.png)

    * ![](https://i.imgur.com/IsHvWRR.png)

*  Congratulations! 🎉 Your have completed an excellent CI(Continuous Integration) & CD (Continuous Delivery) pipeline

---

## Lab 3. Extra Scenario - PR(Pull Request) with develop & master(main) pipeline

### Create a develop branch
* Go to AWS Cloud9 and create a branch develop
    ```
    cd /home/ec2-user/environment/cfh_demo_repo
    git checkout develop
    ```
*  Push develop branch to AWS CodeCommit
    ```
    git push --set-upstream origin develop
    ```



### Set up AWS CodeBuild & AWS CodePipeline for your develop branch
You can follow the master branch’s building process
* Create the AWS CodeBuild for develop with cfh-demo-develop
    * ![](https://i.imgur.com/kjPIs84.png)

    * ![](https://i.imgur.com/MTHyjaH.png)

    * ![](https://i.imgur.com/Y9xW7xj.png)

    * ![](https://i.imgur.com/SYayQ1u.png)

    * ![](https://i.imgur.com/BbS7SzP.png)

* Create the AWS CodePipeline for develop with cfh-demo-develop
    * ![](https://i.imgur.com/KhTL7CC.png)

    * ![](https://i.imgur.com/CkGNRQ8.png)

    * ![](https://i.imgur.com/fMg9I14.png)




### Set up the approval setting of master branch PR(Pull Request)
* ![](https://i.imgur.com/oh6Blak.png)

* ![](https://i.imgur.com/NrokTT1.png)

* ![](https://i.imgur.com/GEQRi6E.png)

* Go to AWS Cloud9 and the commit and push a new update for testing the PR
    * ![](https://i.imgur.com/H36zCOz.png)

    ```
    git add .
    git commit -m "Add a unit test for PR test"
    git push
    ```
    * ![](https://i.imgur.com/6SIucl7.png)

*  Check your AWS CodePipeline
    * ![](https://i.imgur.com/nqdGp57.png)

*  Create your PR from develop branch to master branch
    * Go to your Repository and click to enter it
        * ![](https://i.imgur.com/hhSfXmB.png)

    * Click the Create pull request
        * ![](https://i.imgur.com/PsJtZvw.png)

    * Click Compare and select the develop branch as the Source and select the master branch as the Destination
        * ![](https://i.imgur.com/lnFFMg7.png)

    *   Enter the Title of PR
        * ![](https://i.imgur.com/bvZ9FEY.png)

    *  Then you can see the differences as below and click Create pull request button
        * ![](https://i.imgur.com/5jx4H96.png)

    *  And you will set the result as below
        * ![](https://i.imgur.com/ZE3byPa.png)

*  How to merge the PR?
    * You need another user(colleague) to press the approval button - This is the correct and normal process
    * You can use Overwrite approval rules - This is the special case and unusual process
        * ![](https://i.imgur.com/Rh3nZys.png)

        * ![](https://i.imgur.com/kZZ0axw.png)

    *  After clicking the Merge button you will see the UI as below
        * ![](https://i.imgur.com/y4Zk0d8.png)

    * For this case, do NOT check the option, it will delete your develop branch
        * ![](https://i.imgur.com/kGAtLUu.png)

    * After checking and choose the Merge strategy, please click Merge pull request, and go to AWS CodePipeline to check the master’s pipeline
        * ![](https://i.imgur.com/rFg6vvp.png)


Congratulations! 🎉 You have learned how to create a PR!  


