Post

AWS - CodeDevelop - CodeBuild


CodeBuild

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# docker command to build, tag and push you docker image to the ECR reporitry

# build docker image
docker build -t mydockerrepo .

# Identify the image to push. list the images
docker images

# Tag the image with the Amazon ECR registry, repository
# tags image with the ID e9ae3c220b23 as aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app.
docker tag e9ae3c220b23 aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app

# Push the image using the docker push command:
docker push aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app

3 benefits:

  1. Fully managed
    • fully managed build service in the cloud.
    • compiles the source code, runs unit tests, and produces artifacts that are ready to deploy.
    • eliminates the need to provision, set up, patch, update, manage and scale your own build servers.
  2. On demand
    • scales on demand to meet your build needs.
    • pay only for the build minutes you consume.
  3. Out of the box
    • provides preconfigured / prepackaged build environments for popular programming languages and build tools
      • such as Apache Maven, Gradle, and more.
    • can also customize build environments in CodeBuild to use your own build tools.
    • CodeBuild scales automatically to meet peak build requests.
    • All you need to do is point to your build script to start your first build.
  4. The CodeBuild console also provides a way to quickly search for your resources, such as repositories, build projects, deployment applications, and pipelines. Choose Go to resource or press the / key, and then enter the name of the resource. Any matches appear in the list. Searches are case insensitive. You only see resources that you have permissions to view. For more information, see Viewing resources in the console.
buildspec file
  • collection of build commands and related settings

    CodeBuild uses to run a build.

  • YAML format
  • can overwrite the settings in buildspec.tml by adding own commands in the console when launch the build.
  • if build fail, check thebuild logs in the codebuild console or log in cloudwatch.

How to run CodeBuild

  • use the AWS CodeBuild or AWS CodePipeline console to run CodeBuild.
  • automate the running of CodeBuild by using the AWS Command Line Interface (AWS CLI) or the AWS SDKs.

How to run CodeBuild

pipeline

create a pipeline and add CodeBuild as a build or test action to the build or test stage of a pipeline in AWS CodePipeline.

arch

  1. provide CodeBuild with a build project
    • A build project includes information about
      • how to run a build,
      • where to get the source code,
      • which build environment to use,
      • which build commands to run,
      • and where to store the build output.
    • A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build.
    • CodeBuild uses the build project to create the build environment
  2. CodeBuild use build specification file to start build
    • CodeBuild downloads the source code into the build environment
    • and then uses the build specification file (buildspec)
  3. If there is any build output, the build environment uploads its output to an S3 bucket
    • The build environment can also perform tasks that you specify in the buildspec
    • (for example, sending build notifications to an Amazon SNS topic)
  4. While the build is running, the build environment sends information to CodeBuild and Amazon CloudWatch Logs

  5. While the build is running
    • use the AWS CodeBuild console, AWS CLI, or AWS SDKs to get summarized build information from CodeBuild and detailed build information from Amazon CloudWatch Logs.
    • If you use AWS CodePipeline to run builds, you can get limited build information from CodePipeline.

manually docker build process

CodeBuild automate this process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# prepare
1. get CodeCommit repository
2. get the docker file
3. get user and attach with policy for CodeCommit
4. configure the aws cli

# upload
5. download the CodeCommit repository
6. add the file and push the file to CodeCommit
7. file is stored in the codecommit now

# start build
7. create ECS
   - get a container run in ec2
8. create ECR new repository
   - push docker image in to the repository
9. create new Task Definition
   - setup the container
   - setup ECR repository image location
     - xxx.dkr.ecr.eu-central-1.amazonaws.com/myrepository:latest
10. create service (Task):
    - give it a service name
    - Whether you need load balancing, auto scaling...
11. now check ec2 instance ip
    - web application on docker in ec2

detailed step

  1. create a CodeCommit repository
    • clone URL
    • connection steps
  2. get the docker file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
     # dockerfile
    
     FROM ubuntu:12.04
     # Install dependencies
     RUN apt-get update -y
     RUN apt-get install -y apache2
    
     # Install apache and write hello world message
     RUN echo "Hello Cloud Gurus!!!! This web page is running in a Docker container!" > /var/www/index.html
    
     # Configure apache
     RUN a2enmod rewrite
     RUN chown -R www-data:www-data /var/www
     ENV APACHE_RUN_USER www-data
     ENV APACHE_RUN_GROUP www-data
     ENV APACHE_LOG_DIR /var/log/apache2
    
     EXPOSE 80
    
     CMD ["/usr/sbin/apache2", "-D",  "FOREGROUND"]
    
  3. set user and attach with policy for CodeCommit
    • ec2ecsregistry
  4. configure the aws cli

  5. connect to codecommit repo and clone it locally:
    • setup SSH connection to CodeCommit repository
    • generate ssh key
    • see public key
    • service > IAM > User > security credential
    • SSH Keys for CodeCommit
    • create a ./config file, chmod 600.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
     sourcecoderepo
     - dockerfile
     - buildspec.yml
    
     # Set Up the Credential Helper
     git config --global credential.helper '!aws codecommit credential-helper $@'
     git config --global credential.UseHttpPath true
    
     # connect
     ssh git-codecommit.use-east-2.amazonaws.com
    
     # Clone the repository with the git clone command:
     git clone https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/mysourcecoderepo
     git clone SSH_Clone_URL
    
  6. push the file to CodeCommit
    1
    2
    3
    
     git add .
     git commit -m "Adding file"
     git push
    
  7. file is stored in the codecommit now

  8. create ECS
    • create cluster:
      • ec2linux+networking
      • cluster name
    • select cluster
      • have one container and ec2 instance
  9. create ECR repository
    • push docker image in to repository
    • push commands for the repository:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      
       # retrieve the login command to use to authenticate the Docker client to registry by AWS CLI
       $(aws ecr get-login --no-include-email --region eu-central-1)
      
       # build docker image
       docker build -t mydockerrepo .
      
       # Identify the image to push. list the images
       docker images
      
       # Tag the image with the Amazon ECR registry, repository
       # tags image with the ID e9ae3c220b23 as aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app.
       docker tag e9ae3c220b23 aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app
      
       # Push the image using the docker push command:
       docker push aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app
      
  10. task definition
    • create new Task Definition
      • EC2 or Fargate
    • default
    • add container: specify the container and docker image
      • add docker image from ECR
      • port mapping: host port 80
    • action: create service
  11. ec2 instance - application on docker

codebuild auto build docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# prepare
1. get CodeCommit repository
2. get the docker file
3. get user and attach with policy for CodeCommit
4. configure the aws cli

# upload
5. download the CodeCommit repository
6. add the file and push the file to CodeCommit
   - dockerfile
   - buildspec.yml
7. file is stored in the codecommit now

# start build
# 7. create ECS
#    - get a container run in ec2
# 8. create ECR new repository
#    - push docker image in to the repository
# 9. create new Task Definition
#    - setup the container
#    - setup ECR repository image location
#      - xxx.dkr.ecr.eu-central-1.amazonaws.com/myrepository:latest
# 10. create service (> Task):
#     - give it a service name
#     - if you need load balancing, auto scaling...
# 10. now check ec2 instance ip
#     - web application on docker in ec2

7. create project
   - setup the CodeCommit repository
   - setup the service role
   - setup/build(diy) the buildspec file
8. start build
9. build log


.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.