Setup CodeBuild Project

Setup CodeBuild with CodeCommit Integration

To setup CodeBuild, first we need to create/modify buildspec.yml file to describe build steps. After that we can configure CodeBuild Project linking with CodeCommit Repository.

Prepare buildspec.yml file

  1. Download buildspec.yml file.

    cd ~/environment/tripmgmt/
       
    wget https://build-first-devops-ecs.workshop.aws/reference-files/buildspec.yml
    
  2. Edit buildspec.yml file.

    Replace «YOUR_REPOSITORY_URI» with Container Repository URL - For example, accountid.dkr.ecr.region.amazonaws.com/repository name

  3. Commit buildspec.yml file

    git add buildspec.yml
    git commit -m "Updated buildspec file with ECR path"
    git push 
    

Setup CodeBuild Project

  1. Open the CodeBuild console.

  2. On the Build projects page, choose Create build project.

    Create Build

  3. In Project configuration: Enter a name for this build project tripmgmt-demo-build. Build project names must be unique across each AWS account. You can also include an optional description of the build project to help other users understand what this project is used for.
    If required, Select Build badge to make your project’s build status visible and embeddable.

  4. In Source: For Source provider, choose AWS CodeCommit.

  5. From Repository, choose the repository you have created for this project demo.

  6. For Reference type, choose Branch and select master branch.

  7. In Environment: choose Managed Image with Operating system as Amazon Linux 2,

  8. For Runtime(s) choose Standard and Image choose the one with Standard:2.0 with Image Version as Always use the latest image for this runtime version

  9. For Environment type choose Linux and Check mark Privileged action.

  10. For Service role choose New service role and give meaningful name codebuild-tripmgmt-demo-build-service-role

  11. Under Additional configuration, for Compute choose right spec EC2 instance type to run your build (i.e. 7 GB memory, 4 vCPUs)

  12. For Buildspec name - optional, add buildspec.yml file location - buildspec.yml

  13. For Logs, check mark CloudWatch logs and give Group name as codebuild-logs, Stream name as tripmgmtdemo-build.

  14. Click Create build project.

  15. Now edit newly created service role codebuild-tripmgmt-demo-build-service-role to allow accessing ECR repository through AmazonEC2ContainerRegistryPowerUser managed policy.

    1. Open codebuild-tripmgmt-demo-build-service-role role from IAM console.
    2. Choose Attach policies.
    3. To narrow the available policies to attach, for Filter, type AmazonEC2ContainerRegistryPowerUser
    4. Check the box to the left of the AWS managed policy and choose Attach policy and Update.
  16. Select the build project and Start build to test CodeCommit and CodeBuild integration.

  17. Post successful build, you can verify new docker image in Amazon ECR console.