AWS CodeBuild
Tags: aws, awsdevops, devops, cloud
Published: 2024-01-24 05:39:32

Today, we will explore the AWS CodeBuild service, which is the next phase after CodeCommit. AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages ready for deployment. You don't need to provision, manage, and scale your own build servers. I hope you have read the previous blog about the CodeCommit service.

So, let's get started

-1-. Now, let's build our code. To do that, go to CodeBuild and click on 'Create build project.'

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

-2-. In the project configuration settings, provide the 'Project Name,' then select the source provider as 'AWS CodeCommit.' After that, choose the 'Branch.' Next, select the OS as 'Ubuntu,' runtime as 'Standard,' and use the latest image.

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

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

-3-. Now, we need to create a 'Service Role.' Essentially, it requires access to other services. For instance, for building, it needs access to the code repository to fetch code, and the same applies to other services. If you observe, it creates a role automatically for you. After that, you will see the 'Buildspec' file option, which is important as it is the 'Configuration' file that we need to create.

-4-. To create the file, let's go to VS Code, create a file named 'buildspec.yml,' save it, and push it to the master branch.

```
version: 0.2

phases:
  install:
    commands:
      - echo Installing NGINX
      - sudo apt-get update
      - sudo apt-get install nginx -y
  build:
    commands:
      - echo Build started on `date`
      - cp index.html /var/www/html/
  post_build:
    commands:
      - echo configuring NGINX

artifacts:
  files:
    - '**/*'
```

-5-. If you save the file as 'buildspec.yml,' you don't need to provide the file name; it will be fetched automatically from that branch. After that, click on 'Create build project.'

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

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

-6-. Next, click on the 'Start Build' button to initiate your CodeBuild. You can observe that our build is successful.

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

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

-7-. If you want to store the 'Artifacts,' you can edit the configuration and set the location to 'S3'.

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

-8-. Choose 'S3' as the option. If you already have a bucket, you can provide its name; otherwise, create one in S3. Within that bucket, create another folder where we will store our artifacts in '.zip' format. also choose Artifacts packaging as "ZIP" Copy that location and paste it, then click on 'Update artifacts.' This way, when we build next time, our artifacts will be stored in S3.

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

-9-. You can run the build again to check if your build is stored in S3. Before starting the build, go to IAM → roles, find our build role, and add the 'Access S3' permission. This step is necessary to upload the artifacts; otherwise, you will encounter an 'access denied' error. Here, I've attached that role, and our build is successful.

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

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

-10-. Now, go to the S3 location to check if the artifacts are uploaded. By mistake, I pasted the wrong location path, so that's why the path is long.

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

So, this is how we can build our code using the CodeBuild service. This series is designed for those who are starting with AWS DevOps or are new to AWS DevOps. That's why I am covering the basic pipeline steps. Once this series concludes, we will create a real scenario pipeline. At that time, we can skip the basic steps. I hope you enjoyed today's part.