Ruddra.com

Use Docker for Accessing Database in AWS CodeBuild

Use Docker for Accessing Database in AWS CodeBuild

AWS CodeBuild is an extraordinary tool for building your code. Recently I have been using it to build projects and store docker images in AWS ECR. During those CodeBuild processes, I needed Database for running tests. As the need of the Database was for a limited time and I didn’t want to pay extra for it(that is why I did not use AWS RDS), I decided to create database using docker inside CodeBuild. In this post, I am going to describe how I did it.

Create ‘buildspec.yml’ file

You need to create a buildspec.yml file in the root of the project. There you need to define instruction on how to pull docker image of the database you want to run. Here is an example:

version: 0.2
env:
  variables:
    PGDATABASE: "XXXXX"
    PGUSER: "XXXXX"
    PGPASSWORD: "XXXXX"
phases:
  install:
    runtime-versions:
      python: 3.8
    commands:
      - docker pull postgres:latest
  pre_build:
    commands:
      - echo Running postgres
      - docker run -e PGPASSWORD -e PGUSER -e PGDATABASE -d -p 5432:5432 postgres
  build:
    commands:
      - echo Build started on `date`
      - echo Installing dependencies
      - pip install -r requirements.txt
      - echo Running tests
      - python manage.py test
  post_build:
    commands:
      - echo Build completed on `date`
reports:
  myReport:
    files:
      - "**/*"
cache:
  paths:
    - "/root/.cache/pip/*"

As you can see in the install section of buildspec.yml, I pulled the Postgres docker image. Then in pre_build section, I ran the image with environment variables which were defined in env section. After that, I ran tests in django.

Configure ‘CodeBuild’

Now its time to configure the CodeBuild in AWS console. I think the configuration is quite straightforward. But you need to make sure to tick mark the privileged section of the settings. It should look like this:

AWS Code

Start build

After configuring CodeBuild, now it is time to start the build process from the dashboard. It can be either triggered from git(if you configured it like this), or you need to start the build manually. That is it, inside CodeBuild processes, it will start a Postgres Database, and allow django to run migrations.

In conclusion

Spinning up a docker container for database, seems to me like a very efficient solution. Similarly you can spin up any docker container to use as a temporary service, for example run redis, memcache etc.

Last updated: Apr 05, 2024


← Previous
Basic Networking in Docker

We already know that Docker is used for containerization. But networking in docker makes it even …

Next →
Maintain Authentication Layer from Redux Middleware Using React Router

Maintaining authentication layer in ReactJS can be painful. Back in the old days(even now-a-days), …

Share Your Thoughts
M↓ Markdown