Skip to content

Commit

Permalink
feat(pipes-targets): add Redshift
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Jun 26, 2024
1 parent cce10b1 commit a631c46
Show file tree
Hide file tree
Showing 14 changed files with 81,214 additions and 2,743 deletions.
43,240 changes: 43,240 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions packages/@aws-cdk/aws-pipes-targets-alpha/lib/redshift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha';
import { ICluster } from '@aws-cdk/aws-redshift-alpha';
import { Aws } from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';

/**
* Redshift target properties.
*/
export interface RedshiftTargetParameters {
/**
* The input transformation to apply to the message before sending it to the target.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate
* @default - none
*/
readonly inputTransformation?: IInputTransformation;

/**
* The name of the database.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetredshiftdataparameters.html#cfn-pipes-pipe-pipetargetredshiftdataparameters-database
*/
readonly database: string;

/**
* The database user name. Required when authenticating using temporary credentials.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetredshiftdataparameters.html#cfn-pipes-pipe-pipetargetredshiftdataparameters-dbuser
*/
// TODO: implement this
//readonly dbUser?: string;

/**
* The name or ARN of the secret that enables access to the database. Required when authenticating using Secrets Manager.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetredshiftdataparameters.html#cfn-pipes-pipe-pipetargetredshiftdataparameters-secretmanagerarn
*/
readonly secretManagerArn?: string;

/**
* The SQL statement text to run.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetredshiftdataparameters.html#cfn-pipes-pipe-pipetargetredshiftdataparameters-sqls
*/
readonly sqls: string[];

/**
* The name of the SQL statement. You can name the SQL statement when you create it to identify the query.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetredshiftdataparameters.html#cfn-pipes-pipe-pipetargetredshiftdataparameters-statementname
*/
readonly statementName?: string;

/**
* Indicates whether to send an event back to EventBridge after the SQL statement runs.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetredshiftdataparameters.html#cfn-pipes-pipe-pipetargetredshiftdataparameters-withevent
* @default False
*/
readonly withEvent?: boolean;
}

/**
* A EventBridge Pipes target that sends messages to an SQS queue.
*/
export class RedshiftTarget implements ITarget {
private redshiftParameters?: RedshiftTargetParameters;
public readonly targetArn: string;

constructor(cluster: ICluster, parameters?: RedshiftTargetParameters) {
this.targetArn = `arn:${Aws.PARTITION}:redshift:${cluster.env.region}:${cluster.env.account}:cluster:${cluster.clusterName}`;
this.redshiftParameters = parameters;
}

grantPush(grantee: iam.IRole): void {
if (this.redshiftParameters?.secretManagerArn) {
const statements = [new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['redshift-data:BatchExecuteStatement'],
resources: [this.targetArn],
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['secretsmanager:GetSecretValue'],
resources: [this.redshiftParameters.secretManagerArn],
})];

grantee.attachInlinePolicy(new iam.Policy(grantee, 'policy', { statements }));
}
}

bind(pipe: IPipe): TargetConfig {
if (!this.redshiftParameters) {
return {
targetParameters: {},
};
}

return {
targetParameters: {
inputTemplate: this.redshiftParameters.inputTransformation?.bind(pipe).inputTemplate,
redshiftDataParameters: this.redshiftParameters,
},
};
}
}
10 changes: 6 additions & 4 deletions packages/@aws-cdk/aws-pipes-targets-alpha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/aws-pipes-alpha": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/integ-tests-alpha": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^29.5.12",
"jest": "^29",
"aws-cdk-lib": "0.0.0",
"constructs": "^10.0.0",
"@aws-cdk/aws-pipes-alpha": "0.0.0",
"@aws-cdk/integ-tests-alpha": "0.0.0"
"jest": "^29"
},
"dependencies": {
"@aws-cdk/aws-redshift-alpha": "^0.0.0"
},
"dependencies": {},
"peerDependencies": {
"@aws-cdk/aws-pipes-alpha": "0.0.0",
"aws-cdk-lib": "^0.0.0",
Expand Down
Loading

0 comments on commit a631c46

Please sign in to comment.