Skip to main content

Redshift Data Sharing

The RedshiftDataSharing construct allows Redshift data sharing management for both producers and consumers.

Overview

The RedshiftDataSharing construct provides the following functionality:

  • Create a new data share
  • Grants access to the data share to another Redshift namespace or to another AWS account (provides auto data share authorization for cross-account grants)
  • Create a database from the data share (and for cross-account grants, auto association of the data share to the consumer's Redshift Namespace)

Usage

Single account data sharing:

class ExampleRedshiftDataSharingSameAccountStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id)

const dbName = 'defaultdb';

const producerNamespace = new RedshiftServerlessNamespace(this, 'ProducerNamespace', {
name: 'producer-namespace',
dbName
});

const producerWorkgroup = new RedshiftServerlessWorkgroup(this, 'ProducerRSWorkgroup', {
name: 'producer-workgroup',
namespace: producerNamespace
});

const consumerNamespace = new RedshiftServerlessNamespace(this, 'ConsumerNamespace', {
name: 'consumer-namespace',
dbName
});

const consumerWorkgroup = new RedshiftServerlessWorkgroup(this, 'ConsumerRSWorkgroup', {
name: 'consumer-workgroup',
namespace: consumerNamespace
});

const shareName = 'testshare';

const createCustomersTable = producerWorkgroup.runCustomSQL('CreateCustomerTable', dbName, 'create table public.customers (id varchar(100) not null, first_name varchar(50) not null, last_name varchar(50) not null, email varchar(100) not null)', 'drop table public.customers');

const newShare = producerWorkgroup.createShare('producer-share', dbName, shareName, 'public', ['public.customers']);
newShare.newShareCustomResource.node.addDependency(createCustomersTable);

const grantToConsumer = producerWorkgroup.grantAccessToShare('GrantToConsumer', newShare, consumerNamespace.namespaceId)

grantToConsumer.resource.node.addDependency(newShare);
grantToConsumer.resource.node.addDependency(consumerNamespace);

const consumeShare = consumerWorkgroup.createDatabaseFromShare('consume-datashare', 'db_from_share', shareName, producerNamespace.namespaceId)

consumeShare.resource.node.addDependency(grantToConsumer);
}
}
class ExampleRedshiftDataSharingCrossAccountAStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id)

const dbName = 'defaultdb';

const producerNamespace = new RedshiftServerlessNamespace(this, 'ProducerNamespace', {
name: 'producer-namespace',
dbName
});

const producerWorkgroup = new RedshiftServerlessWorkgroup(this, 'ProducerRSWorkgroup', {
name: 'producer-workgroup',
namespace: producerNamespace
});

const shareName = 'testshare';

const createCustomersTable = producerWorkgroup.runCustomSQL('CreateCustomerTable', dbName, 'create table public.customers (id varchar(100) not null, first_name varchar(50) not null, last_name varchar(50) not null, email varchar(100) not null)', 'drop table public.customers');

const newShare = producerWorkgroup.createShare('producer-share', dbName, shareName, 'public', ['public.customers']);
newShare.newShareCustomResource.node.addDependency(createCustomersTable);

const grantToConsumer = producerWorkgroup.grantAccessToShare('GrantToConsumer', newShare, undefined, "<CONSUMER-ACCOUNT-ID>", true);

grantToConsumer.resource.node.addDependency(newShare);
}
}

class ExampleRedshiftDataSharingCrossAccountBStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id)

const dbName = 'defaultdb';

const consumerNamespace = new RedshiftServerlessNamespace(this, 'ConsumerNamespace', {
name: 'consumer-namespace',
dbName
});

const consumerWorkgroup = new RedshiftServerlessWorkgroup(this, 'ConsumerRSWorkgroup', {
name: 'consumer-workgroup',
namespace: consumerNamespace
});

const shareName = 'testshare';

consumerWorkgroup.createDatabaseFromShare('consume-datashare', "db_from_share", shareName, "<PRODUCER NAMESPACE>", "<PRODUCER ACCOUNT>")
}
}