Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created October 4, 2023 12:09
Show Gist options
  • Save gjohnson/765ef118d641c7cfdd460f9d890c6ea4 to your computer and use it in GitHub Desktop.
Save gjohnson/765ef118d641c7cfdd460f9d890c6ea4 to your computer and use it in GitHub Desktop.
Example Glue CDK app
import * as cdk from 'aws-cdk-lib';
import * as glue from 'aws-cdk-lib/aws-glue';
import * as redshift from 'aws-cdk-lib/aws-redshift';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as core from 'aws-cdk-lib/core';
export class YourCdkStack extends core.Stack {
constructor(scope: core.Construct, id: string, props?: core.StackProps) {
super(scope, id, props);
// Create an S3 bucket for your JSON files
const dataBucket = new s3.Bucket(this, 'DataBucket', {
removalPolicy: core.RemovalPolicy.DESTROY, // Only for dev/test purposes
});
// Create a Redshift cluster
const redshiftCluster = new redshift.Cluster(this, 'RedshiftCluster', {
masterUser: {
masterUsername: 'your-master-username',
masterPassword: cdk.SecretValue.plainText('your-master-password'),
},
clusterType: redshift.ClusterType.SINGLE_NODE, // You can configure this as needed
defaultDatabaseName: 'your-database-name',
removalPolicy: core.RemovalPolicy.DESTROY, // Only for dev/test purposes
});
// Create a Glue job
const glueJob = new glue.CfnJob(this, 'GlueJob', {
name: 'YourGlueJob',
role: new iam.Role(this, 'GlueJobRole', {
assumedBy: new iam.ServicePrincipal('glue.amazonaws.com'),
}).roleArn,
command: {
name: 'glueetl',
scriptLocation: `s3://${dataBucket.bucketName}/your-glue-job-script.py`, // Provide the path to your Glue job script
},
defaultArguments: {
'--job-language': 'python',
'--job-bookmark-option': 'job-bookmark-enable',
'--TempDir': `s3://${dataBucket.bucketName}/temp/`, // S3 temporary directory for Glue
'--enable-continuous-cloudwatch-log': 'true',
},
connections: {
connections: ['your-connection-name'], // If you have a connection to Redshift
},
maxRetries: 0, // You can configure this as needed
timeout: 2880, // You can configure this as needed
});
// Grant Glue job permissions to access S3 and Redshift
dataBucket.grantReadWrite(glueJob);
redshiftCluster.grantReadWrite(glueJob);
// Add any other necessary configurations, triggers, or dependencies
// ...
// Output the Redshift cluster endpoint
new core.CfnOutput(this, 'RedshiftClusterEndpoint', {
value: redshiftCluster.clusterEndpoint.hostname,
});
}
}
const app = new cdk.App();
new YourCdkStack(app, 'YourCdkStack');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment