What is CodeBuild?
AWS CodeBuild is a fully managed build service in the cloud. CodeBuild compiles your source code, runs unit tests, and produces artifacts that are ready to deploy. CodeBuild eliminates the need to provision, manage, and scale your own build servers.
Task-01 :
What is Buildspec file for Codebuild?
A Buildspec file is a YAML file that defines the build process for your CodeBuild project. It contains a series of commands that CodeBuild will execute to build and package your application.
Initial configuration for AWSCodeBuildAdminAccess
Follow these steps to set up an Amazon Web Services account, create an IAM user, and configure access to CodeBuild.
To create and configure an IAM user for accessing CodeBuild
Create an IAM user, or use an existing one, in your Amazon Web Services account. Make sure you have an access key ID and a secret access key associated with that IAM user.
In the IAM console, in the navigation pane, choose Users, and then choose the IAM user you want to configure for CodeCommit access.
On the Permissions tab, choose Add Permissions.
In Grant permissions, choose Attach existing policies directly.
From the list of policies, select AWSCodeBuildAdminAccess
After you have selected the policy you want to attach, choose Next: Review to review the list of policies to attach to the IAM user. If the list is correct, choose Add Permissions.
Your User has created with Acess to AWSCodeBuild
Create a simple index.html file in the CodeCommit Repository.
Login to your AWS account and go to CodeCommit we have our repository already created. To create repository and connect it to local you can prefer my previous blog: https://kshitijaa.hashnode.dev/cicd-pipeline-on-aws-part-1
Build the index.html using nginx server
Now we are creating index.html file in the same repository and pushing it to AWS Repository
<header>
<h1>
Welcome to <a href="https://kshitijaa.hashnode.dev/" target="_blank"> Kshitija Bartakke-Malwade's Blogs!
</h1>
</header>
Save the file and commit the changes to the repository using the git add and git commit commands.
git pull
git add index.html
git commit -m "Adding index.html file"
git push
You have a your index.html file in your CodeCommit repository
Task-02 :
Let's create a buildspec.yaml
file in our repository for our index.html file.
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:
- /var/www/html/index.html
Create buildspec.yaml a file in your repository and push to your AWS Repository
git add buildspec.yaml
git commit -m "Adding buildspec.yaml file"
git push
buildspec.yaml file pushed to AWS Repository!
Now let's create a CodeBuild project in AWS Console.
Step1: To Create a CodeBuild Project search CodeBuild in AWS Services Console and click on Create build project
Step-02: Give a name
to your CodeBuild Project
.
Step-03: Select your source provider
and repository
.
Step-04: Select your environment
and operating system
.
Choose Artifacts to save to S3 and bucket name, saving the name to Nginx
Thus we have created our CodeBuild Project
.
Now we will start the build
so click on Start Build
.
Once you start the build wait for it to be completed
AWS S3 bucket to see build artifacts.
This is how Code Build work!