Amazon Redshift : How to Create Redshift Cluster, Copy S3 Data to Redshift and Query on the Redshift Console Using Query Editor
Tags: aws, awsredshift, s3, iam
Published: 2022-03-12 18:55:55

“Challenges faced to find the solution of how to create a redshift cluster, copy s3 data to redshift and query on the redshift console using a query editor”. I have checked various documents on how to do the configuration of the redshift cluster. I have found a way to copy the s3 bucket data to redshift using the command line interface. Also checked the redshift cluster console to run and monitor the queries using the query editor. In terms of cost and security perspective, pricing is based on compute nodes and also can be secured using the redshift encryption feature.

Amazon Redshift is a [data warehouse](https://en.wikipedia.org/wiki/Data_warehouse) product which forms part of the larger [cloud-computing](https://en.wikipedia.org/wiki/Cloud-computing) platform Amazon Web Services. It is built on top of technology from the [massive parallel processing](https://en.wikipedia.org/wiki/Massive_parallel_processing) data warehouse company [ParAccel](https://en.wikipedia.org/wiki/ParAccel), to handle large scale [data sets](https://en.wikipedia.org/wiki/Data_set) and [database migrations](https://en.wikipedia.org/wiki/Database#Migration). Redshift differs from Amazon’s other hosted database offering, [Amazon RDS](https://en.wikipedia.org/wiki/Amazon_RDS), in its ability to handle analytic workloads on [big data](https://en.wikipedia.org/wiki/Big_data) data sets stored by a column-oriented DBMS principle. Redshift allows up to 16 petabytes of data on a cluster compared to Amazon RDS’s maximum database size of 16TB.

Amazon Redshift uses SQL to analyze structured and semi-structured data across data warehouses, operational databases, and data lakes, using AWS-designed hardware and machine learning to deliver the best price performance at any scale. 

In this post, you will get to know how to create a redshift cluster, copy s3 data to redshift and query on the redshift console using a query editor. Here I have used an ec2 server, a s3 bucket and IAM role with required permissions policy. 

#Prerequisites
You’ll need an Amazon EC2 Server for this post. [Getting started with amazon EC2](https://aws.amazon.com/ec2/getting-started/) provides instructions on how to launch an EC2 Server.

You’ll need an Amazon Simple Storage Service for this post. [Getting started with Amazon Simple Storage Service](https://aws.amazon.com/s3/getting-started/) provides instructions on how to create a bucket in simple storage service. For this blog, I assume that I have created a s3 bucket and a ec2 server.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzfjx77z7mwobbqe6jz0.png)
The architecture diagram shows the overall deployment architecture with data flow, amazon redshift, ec2 server and s3 bucket.

#Solution overview
The blog post consists of the following phases:
1. Create Amazon Redshift Cluster with required IAM permissions of S3
2. Attach required permissions policy in IAM role and copy of S3 data to Redshift Cluster
3. Run Query in Redshift Cluster Console and Verify the Output with Monitoring parameters
4. Resize of Redshift Cluster using Resize option

I have one ec2 server and a s3 bucket created as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54c5ig5eks64ub9atap0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3it09712uu6jt5uc6k4c.png)

##Phase 1: Create Amazon Redshift Cluster with required IAM permissions of S3  
1. Open the amazon redshift console, click on create cluster. Give cluster name, choose production and size as dc2.large with 1 number of nodes. Give admin username, password and required IAM role. In the database configuration setting, enter the database name and port with the required parameter group. Create a subnet group with custom vpc. And leave other settings as default. Also can encrypt using encryption features and create a redshift cluster. Once gets redshift cluster is in an available state, we can check the properties and other settings. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1fjgdofu2o84wro9x8t.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tr6wkeb5h9gwu27f78h.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k4vp4myucdng759upaf2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5vn772jrxqcoo8j9adl.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lb6l78dkfl5ko4pg7ltz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg44oies5wwhj2rwdyxm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16tpyghepy06511qxbnn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t9c47saettbqo4lkkky.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94zgs1pgdt930ooo4ztk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5ptrzxk20cqeeujg5li.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ontybjqvmgzcyc8jrhe9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l41lrl6b3umffask1m88.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1eh3ujojifm3nskma4t0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po9byp30l1bqxrdxodap.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ch3yok2dwc0mcmszztyn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdsatc7pc2rl7m9gjbkd.png)

##Phase 2: Attach required permissions policy in IAM role and copy of S3 data to Redshift Cluster
1. Open the IAM role and attach required permissions policy. Login to ec2 server in terminal and install postgresql-12. Connect the redshift cluster using psql command with hostname, username, database name and port. Then create a table in a cluster using create table command. Also copy data from s3 to redshift with required IAM role arn and region. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2845hxlsp75h96qu07h.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmhddj3wef76938hsqs2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlwy188p9fscztpt4i11.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qy7jnfodo4n7muz1lv3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qc9dme8po3gv6rio39gw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uttv54s6zoq3skg5ehze.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5jxzig8gufr55uxo9zf.png)

##Phase 3: Run Query in Redshift Cluster Console and Verify the Output with Monitoring parameters
1. Open the redshift cluster console and do a query in the redshift database using the query editor. Give the database details to connect it as database name and username.. Once connected to the database, run a query to select all from the table. Output result of the query shows the rows returned. We can also check the output of execution and visualize. Also can check other parameters of monitoring from the redshift console. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/toxr9i5c0kmy0znz9tnh.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr5pdbs1yy9itd4pvew7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8rzxdaqgktvfninybva.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m17185zn774opsaq6fh8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnjwcjwac90mdxqrzhm9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me13u26a9btgftyueywo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tculldpijfr9x3ws63qk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/slz8keecalqz0sl9bhh3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i91ji8tzds4vwhovgt8r.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyiqnqpsxk91wyyyp35i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7j7xl9qkzw1v9gjj02ic.png)

##Phase 4: Resize of Redshift Cluster using Resize option
1. Open the redshift cluster console and goto actions drop down. Select the resize option and add the number of nodes as required. Click on resize cluster option and then cluster goes to modifying state then unavailable and then available.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asmob7m6obkh8ut9wgrp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s760exigbaw7a9kv15q1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kqwp05jebi2ma9kxrj3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ec9iqsyl316r13r0s5ce.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3e3w5xaqikoo6wlvtour.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi0vdlw67juagprd1i10.png)

#Clean-up
Delete EC2 server, IAM, S3 bucket, Redshift cluster.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of EC2 = $0.0124 per On Demand Linux t2.micro Instance Hour = 2.013 Hrs = $0.02

Cost of Key Management Service = $0.0

Cost of Redshift = $0.000 per Redshift Dense Compute Large (DC2.L) Compute Node = 1.957 Hrs = $0.0

Cost of Simple Storage Service = $1.0

Total Cost = $(0.02+0.0+0.0+1.0) = $1.02

#Summary
In this post, I showed “how to create a redshift cluster, copy s3 data to redshift and query on the redshift console using a query editor”.

For more details on Amazon Redshift, Checkout Get started Amazon Redshift, open the [Amazon Redshift console](https://ap-south-1.console.aws.amazon.com/redshiftv2/home?region=ap-south-1#landing). To learn more, read the [Amazon Redshift documentation](https://docs.aws.amazon.com/redshift/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)