👹 How to create a Golang λ-function with a Database Connection?
In this article we will discuss the possibility of creation a Golang λ-function with a Database Connection.
Executive Summary
As we discussed earlier in the article «How to create a λ-function for the Golang Microservice?» it's possible to implement a microservice module in the form of a separate λ-function. Now let’s consider the possibility of implementing the creation of a schema in the database using the λ-function, as well as connecting to the created schema by executing an endpoint.
The process of preparing the databases involved in the microservice is called migration. Migration scripts can include queries that create tables, keys, indexes, and much more.
Very often, migration is made part of the main code of a microservice and, as a result, migration scripts are also tested together with other service's parts, thereby mixing unit, interface and integration testing. Sometimes there are cases when migration testing is performed through Cypress testing, and in such a way that the migration scripts executed during the installation of Cypress may differ significantly from those that should be in reality. We believe that migration scripts should be defined once, in one place and executed uniformly, regardless of testing methods and programs.
The migration module is perfectly suited for execution inside the λ-function, since it
- must be performed at the beginning of the microservice operation;
- does not depend on the main microservice code;
- must execute only migration scripts specific to a particular service.
All this speaks in favor of implementing migration in the form of a λ-function, and in the current article we will show how this can be done for an arbitrary Golang microservice.
Implementation
The general storage architecture of the λ-function remained unchanged. However, there are some differences that should be taken into account when planning the connection of the λ-function to the database management system.
We will place the database in Docker Container (DC), which requires the addition of a single docker-compose.yaml
file.
It is also necessary to place all sql-queries used during the migration in a separate directory λ-function.
The general view of the λ-function architecture for database migration is presented below:
/repo root |--... |--/lambdas |----/<λ-function> |------template.yaml |------docker-compose.yaml |------<λ-function>.go |------/scripts |--------*.sql
Configuration of the λ-function and DC
To host a database inside a DC based on a postgres:10.5-alpine
image, we will need to define the container name <service-name>
, as well as the main global variables used for connection. The connection to the container will be made via an external connection <service-network>
.
Thus, the contents of the docker-compose.yaml
file will have the form:
services: <service-name>: image: postgres:10.5-alpine container_name: <service-name> environment: - DB_USER =**** - DB_PASSWORD=**** - DB_HOST =**** - DB_DBNAME =**** - DB_PORT =**** ports: - '5432:5432' networks: - <service-network> networks: <service-network>: external: true
On the other hand, we will also need to make some additions to the template.yaml
file.
t is necessary to specify the address of the DB_HOST
constant <service-network>
. By default, Docker reserves several addresses for DC placement, but we suggest fixing one of them specifically and passing it as the DB_HOST
value.
Properties: ... Environment: Variables: DB_SCHEMA: **** DB_USER: **** DB_PASSWORD: **** DB_HOST: "172.21.0.1" DB_DBNAME: **** DB_PORT: ****
When creating a λ-package, it is necessary to package not only the executable file, but also the scripts
directory, which contains all SQL queries for database migration:
zip -r <repo root>/lambdas/<λ-function>/<λ-function>.zip -j / <repo root>/lambdas/<λ-function>/<λ-function> / <repo root>/lambdas/<λ-function>/scripts
To prepare the necessary <service-network>
connection, you need to run the following commands:
docker network create / --subnet="172.21.0.0/16" / --gateway="172.21.0.1" / <service-network> docker-compose / --file <repo root>/lambdas/<λ-function>/docker-compose.yaml up -d
Repository
See the sample of a Golang λ-function with a Database Connection in my repo: