14.4 C
Saturday, November 18, 2023

Implement information warehousing resolution utilizing dbt on Amazon Redshift

Amazon Redshift is a cloud information warehousing service that gives high-performance analytical processing primarily based on a massively parallel processing (MPP) structure. Constructing and sustaining information pipelines is a typical problem for all enterprises. Managing the SQL information, integrating cross-team work, incorporating all software program engineering ideas, and importing exterior utilities is usually a time-consuming job that requires complicated design and many preparation.

dbt (DataBuildTool) provides this mechanism by introducing a well-structured framework for information evaluation, transformation and orchestration. It additionally applies normal software program engineering ideas like integrating with git repositories, organising DRYer code, including useful check circumstances, and together with exterior libraries. This mechanism permits builders to give attention to getting ready the SQL information per the enterprise logic, and the remaining is taken care of by dbt.

On this put up, we glance into an optimum and cost-effective method of incorporating dbt inside Amazon Redshift. We use Amazon Elastic Container Registry (Amazon ECR) to retailer our dbt Docker photographs and AWS Fargate as an Amazon Elastic Container Service (Amazon ECS) job to run the job.

How does the dbt framework work with Amazon Redshift?

dbt has an Amazon Redshift adapter module named dbt-redshift that permits it to attach and work with Amazon Redshift. All of the connection profiles are configured throughout the dbt profiles.yml file. In an optimum atmosphere, we retailer the credentials in AWS Secrets and techniques Supervisor and retrieve them.

The next code exhibits the contents of profile.yml:


goal: dev
kind: redshift
host: "{{ env_var('DBT_HOST') }}"
person: "{{ env_var('DBT_USER') }}"
password: "{{ env_var('DBT_PASSWORD') }}"
port: 5439
dbname: "{{ env_var('DBT_DB_NAME') }}"
schema: dev
threads: 4
keepalives_idle: 240 # default 240 seconds
connect_timeout: 10 # default 10 seconds
sslmode: require
ra3_node: true

The next diagram illustrates the important thing elements of the dbt framework:

The first elements are as follows:

  • Fashions – These are written as a SELECT assertion and saved as a .sql file. All of the transformation queries might be written right here which might be materialized as a desk or view. The desk refresh might be full or incremental primarily based on the configuration. For extra data, refer SQL fashions.
  • Snapshots – These implements type-2 slowly altering dimensions (SCDs) over mutable supply tables. These SCDs establish how a row in a desk modifications over time.
  • Seeds – These are CSV information in your dbt challenge (usually in your seeds listing), which dbt can load into your information warehouse utilizing the dbt seed command.
  • Checks – These are assertions you make about your fashions and different assets in your dbt challenge (corresponding to sources, seeds, and snapshots). If you run dbt check, dbt will inform you if every check in your challenge passes or fails.
  • Macros – These are items of code that may be reused a number of occasions. They’re analogous to “capabilities” in different programming languages, and are extraordinarily helpful if you end up repeating code throughout a number of fashions.

These elements are saved as .sql information and are run by dbt CLI instructions. In the course of the run, dbt creates a Directed Acyclic Graph (DAG) primarily based on the interior reference between the dbt elements. It makes use of the DAG to orchestrate the run sequence accordingly.

A number of profiles might be created throughout the profiles.yml file, which dbt can use to focus on completely different Redshift environments whereas working. For extra data, check with Redshift arrange.

Resolution overview

The next diagram illustrates our resolution structure.

The workflow incorporates the next steps:

  1. The open supply dbt-redshift connector is used to create our dbt challenge together with all the required fashions, snapshots, exams, macros and profiles.
  2. A Docker picture is created and pushed to the ECR repository.
  3. The Docker picture is run by Fargate as an ECS job triggered through AWS Step Features. All of the Amazon Redshift credentials are saved in Secrets and techniques Supervisor, which is then utilized by the ECS job to attach with Amazon Redshift.
  4. In the course of the run, dbt converts all of the fashions, snapshots, exams and macros to Amazon Redshift compliant SQL statements and it orchestrates the run primarily based on the interior information lineage graph maintained. These SQL instructions are run immediately on the Redshift cluster and subsequently the workload is pushed to Amazon Redshift immediately.
  5. When the run is full, dbt will create a set of HTML and JSON information to host the dbt documentation, which describes the information catalog, compiled SQL statements, information lineage graph, and extra.


It is best to have the next stipulations:

  • A very good understanding of the dbt ideas and implementation steps.
  • An AWS account with person position permission to entry the AWS providers used on this resolution.
  • Safety teams for Fargate to entry the Redshift cluster and Secrets and techniques Supervisor from Amazon ECS.
  • A Redshift cluster. For creation directions, check with Create a cluster.
  • An ECR repository: For directions, check with Creating a personal repository
  • A Secrets and techniques Supervisor secret containing all of the credentials for connecting to Amazon Redshift. This consists of the host, port, database title, person title, and password. For extra data, check with Create an AWS Secrets and techniques Supervisor database secret.
  • An Amazon Easy Storage (Amazon S3) bucket to host documentation information.

Create a dbt challenge

We’re utilizing dbt CLI so all instructions are run within the command line. Due to this fact, set up pip if not already put in. Check with set up for extra data.

To create a dbt challenge, full the next steps:

  1. Set up dependent dbt packages:
    pip set up dbt-redshift
  2. Initialize a dbt challenge utilizing the dbt init <project_name> command, which creates all of the template folders robotically.
  3. Add all of the required DBT artifacts.
    Check with the dbt-redshift-etlpattern repo which features a reference dbt challenge. For extra details about constructing tasks, check with About dbt tasks.

Within the reference challenge, we’ve got carried out the next options:

  • SCD kind 1 utilizing incremental fashions
  • SCD kind 2 utilizing snapshots
  • Seed look-up information
  • Macros for including reusable code within the challenge
  • Checks for analyzing inbound information

The Python script is ready to fetch the credentials required from Secrets and techniques Supervisor for accessing Amazon Redshift. Check with the export_redshift_connection.py file.

  1. Put together the run_dbt.sh script to run the dbt pipeline sequentially. This script is positioned within the root folder of the dbt challenge as proven in pattern repo.
-- Import the dependent exterior libraries
dbt deps --profiles-dir . --project-dir .

-- Create tables primarily based on the seed information
dbt seed --profiles-dir . --project-dir .

-- Run all of the mannequin information
dbt run --profiles-dir . --project-dir .

-- Run all of the snapshot information
dbt snapshot --profiles-dir . --project-dir .

-- Run all inbuilt and customized check circumstances ready
dbt check --profiles-dir . --project-dir .

-- Generate dbt documentation information
dbt docs generate --profiles-dir . --project-dir .

--Copying dbt outputs to s3 bucket - for internet hosting
aws s3 cp --recursive --exclude="*" --include="*.json" --include="*.html" dbt/goal/ s3://<bucketName>/REDSHIFT_POC/

  1. Create a Docker file within the guardian listing of the dbt challenge folder. This step builds the picture of the dbt challenge to be pushed to the ECR repository.
FROM python:3

ADD dbt_src /dbt_src

RUN pip set up -U pip

# Set up DBT libraries
RUN pip set up --no-cache-dir dbt-core

RUN pip set up --no-cache-dir dbt-redshift

RUN pip set up --no-cache-dir boto3

RUN pip set up --no-cache-dir awscli

WORKDIR /dbt_src

RUN chmod -R 755 .

ENTRYPOINT [ "/bin/sh", "-c" ]

CMD ["./run_dbt.sh"]

Add the picture to Amazon ECR and run it as an ECS job

To push the picture to the ECR repository, full the next steps:

  1. Retrieve an authentication token and authenticate your Docker shopper to your registry:
    aws ecr get-login-password --region <region_name> | docker login --username AWS --password-stdin <repository_name>

  2. Construct your Docker picture utilizing the next command:
docker construct -t <picture tag> .

  1. After the construct is full, tag your picture so you possibly can push it to the repository:
docker tag <picture tag>:newest <repository_name>:newest

  1. Run the next command to push the picture to your newly created AWS repository:
docker push <repository_name>/<picture tag>:newest

  1. On the Amazon ECS console, create a cluster with Fargate as an infrastructure possibility.
  2. Present your VPC and subnets as required.
  3. After you create the cluster, create an ECS job and assign the created dbt picture as the duty definition household.
  4. Within the networking part, select your VPC, subnets, and safety group to attach with Amazon Redshift, Amazon S3 and Secrets and techniques Supervisor.

This job will set off the run_dbt.sh pipeline script and run all of the dbt instructions sequentially. When the script is full, we are able to see the leads to Amazon Redshift and the documentation information pushed to Amazon S3.

  1. You may host the documentation through Amazon S3 static web site internet hosting. For extra data, check with Internet hosting a static web site utilizing Amazon S3.
  2. Lastly, you possibly can run this job in Step Features as an ECS job to schedule the roles as required. For extra data, check with Handle Amazon ECS or Fargate Duties with Step Features.

The dbt-redshift-etlpattern repo now has all of the code samples required.

Price for executing dbt jobs in AWS Fargate as an Amazon ECS job with minimal operational necessities would take round $1.5 (cost_link) per thirty days.

Clear up

Full the next steps to wash up your assets:

  1. Delete the ECS Cluster you created.
  2. Delete the ECR repository you created for storing the picture information.
  3. Delete the Redshift Cluster you created.
  4. Delete the Redshift Secrets and techniques saved in Secrets and techniques Supervisor.


This put up coated the essential implementation of utilizing dbt with Amazon Redshift in a cost-efficient method by utilizing Fargate in Amazon ECS. We described the important thing infrastructure and configuration set-up with a pattern challenge. This structure might help you reap the benefits of the advantages of getting a dbt framework to handle your information warehouse platform in Amazon Redshift.

For extra details about dbt macros and fashions for Amazon Redshift inner operation and upkeep, check with the next GitHub repo. In subsequent put up, we’ll discover the standard extract, rework, and cargo (ETL) patterns that you may implement utilizing the dbt framework in Amazon Redshift. Take a look at this resolution in your account and supply suggestions or options within the feedback.

In regards to the Authors

Seshadri Senthamaraikannan is a knowledge architect with AWS skilled providers crew primarily based in London, UK. He’s effectively skilled and specialised in Knowledge Analytics and works with clients specializing in constructing progressive and scalable options in AWS Cloud to satisfy their enterprise objectives. In his spare time, he enjoys spending time together with his household and play sports activities.

Mohamed Hamdy is a Senior Large Knowledge Architect with AWS Skilled Companies primarily based in London, UK. He has over 15 years of expertise architecting, main, and constructing information warehouses and large information platforms. He helps clients develop huge information and analytics options to speed up their enterprise outcomes by way of their cloud adoption journey. Exterior of labor, Mohamed likes travelling, working, swimming and taking part in squash.

Latest news
Related news


Please enter your comment!
Please enter your name here