Understanding AWS CodeBuild and creating your first build project!
As the name suggests, AWS CodeBuild is a build service in the cloud. It is fully managed so there is no need to provision, manage, and scale your own build servers. It compiles your source code, runs unit tests, and produces artifacts that are ready to deploy.
You can use the AWS CodeBuild or AWS CodePipeline console to run CodeBuild. You can also automate the running of CodeBuild by using the AWS Command Line Interface (AWS CLI) or the AWS SDKs. In this blog, we will use the AWS CodeBuild console.
How it works:
- We need to provide CodeBuild with a build project as an input. A build project includes information about how to run a build, including where to get the source code, which build environment to use, which build commands to run, and where to store the build output.
- CodeBuild uses the build project to create the build environment. A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build.
- CodeBuild downloads the source code into the build environment. Source code can be stored in a repository such as AWS CodeCommit, Github, Bitbucket or it can be stored in Amazon S3 input bucket.
- CodeBuild then uses the build specification (buildspec), as defined in the build project or included directly in the source code. A buildspec file is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build.
- You can also specify tasks in the buildspec (for example, sending build notifications to an Amazon SNS topic).
- If there is any build output, the build environment uploads its output to an S3 bucket as artifacts.
- While the build is running, you can view summarised build information in AWS CodeBuild and detailed build information in Amazon CloudWatch Logs.
Enough theory, time to get our hands dirty with a project!
We will be creating a build which deploys a static website to an S3 bucket, as a part of its build output.
Getting things ready!
Step 1: Create an S3 bucket for output artifacts and enable it for website hosting:
- When you configure a bucket as a static website, you must enable website hosting, set permissions, and create and add an index document.
- Navigate to the Amazon S3 console, and then choose Create bucket.
- Enter a unique name and choose the Region you have built the rest of your resources in and then choose Create bucket.
- Open the bucket that you just created and click on Properties. Scroll down to Static Website Hosting and choose Edit.
- Choose Enable for Static Website Hosting. Type hello.html under Index document and error under Error document. Click on Save changes. For now just stay with error, however, if you want to understand about error document, read this.
- Set the bucket permissions for website address using this link https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteAccessPermissionsReqd.html. If you want to understand about IAM permissions, here’s the link to the part 1 of my 3 parts series on IAM.
Step 2: Creating a CodeCommit repository:
- Navigate to the AWS CodeCommit console, and then choose Create repository.
- Enter a name and optionally a description, and then choose Create.
- Scroll down, and then choose Create file
- You will be taken to a new screen to create a sample file (Figure 2), copy the following code into the text body, name the file hello.html, and then choose Commit changes.
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Hello world</p>
</body>
</html>
- On the left pane, choose Repositories and open the repository you just created. Expand Add file and choose Create file.
- Copy the following code into the text body, name the file buildspec.yml, and then choose Commit changes.
version: 0.2
phases:
install:
commands:
- echo Nothing to install
pre_build:
commands:
- echo pre-build
build:
commands:
- mkdir html
- cp hello.html html/.
- echo Build started on `date`
post_build:
commands:
- echo Build completed on `date`
artifacts:
files:
- '**/*'
base-directory: 'html'
discard-paths: no
- So, your ground work is done! You have created an S3 bucket for hosting the website and storing build artifacts. You have also created 2 files in your repository, hello.html which is your source code and buildspec.yml which is your buildspec file which AWS Codebuild will use to run the build. To understand the buildspec.yml syntax, read this.
Getting on the CodeBuild console!
- Navigate to the AWS CodeBuild console and choose Create project. Give a Project Name and optionally, a description of your project.
- In the Source section, select AWS CodeCommit as the Source provider, select the name of the repository you created, and then choose master as your Branch.
- For Environment image, choose Managed image, and then configure the following , as shown in the figure below:
- Operating system: Ubuntu
- Runtimes(s): Standard
- Image: aws/codebuild/standard:3.0
- Image version: Always use the latest image for this runtime version
- Environment type: Linux
- Select the checkbox under Privileged.
- Select New service role. A Role Name will be created for you.
- Choose Use a Buildspec file. By default, CodeBuild looks for a file named buildspec.yml in the source code root directory and we have created exactly that in our CodeCommit repository earlier in Step 2.
- Under Artifacts, Choose Type as Amazon S3 and select the Bucket name that you created earlier in Step 1. Put a ‘/’ under Name. Why the ‘/’? So that your hello.html doesn’t go inside a folder in the bucket!
- Select Disable artifact encryption. If the artifact is encrypted, we won’t be able to view our static website in our browser!
- Leave the rest of the things as default and choose Create Build Project. You will see a message that you have successfully created your project.
Let’s see it working!
- Navigate to Build projects in the AWS Codebuild console, open your CodeBuild project and choose Start build.
- Click on Tail logs to see the progress of your build.
- If everything has been configured correctly and there are no errors, you should see a success message as in figure below.
The final check!
If CodeBuild has done it’s job well, you should be able to see your build output (artifact) in your S3 bucket and finally see your static website!
- Navigate to the Amazon S3 console and open your bucket that you created in Step 1. You should see a hello.html file. There! Your artifacts have been promptly stored in S3.
- Click on Properties and scroll down to Static website hosting. Copy the URL under Bucket website endpoint. Paste it in your favourite web browser and press Enter. This should take you to your own static website which says Hello world.
Clean up!
To avoid service charges, you can delete your project and repository as well as your S3 bucket. Just select the project/repository/bucket and choose delete. It’s pretty easy!
Conclusion
In this blog, we learnt about the basic concepts and working of AWS CodeBuild. We created our repository in CodeCommit and added our source code and buildspec file to it. We created our first CodeBuild project which downloaded the source code from AWS CodeCommit, used the buildspec.yml to run the code and stored the build artifact to our S3 bucket. As our build output was a html file, we used S3 for static website hosting.
If you have any questions or suggestions, please comment below.