Skip to content

Commit 095446d

Browse files
humanzzElad Ben-Israel
authored and
Elad Ben-Israel
committed
feat(glue): Connection construct (#12444)
Closes #12442 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 87aa337 commit 095446d

File tree

7 files changed

+975
-0
lines changed

7 files changed

+975
-0
lines changed

Diff for: packages/@aws-cdk/aws-glue/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323

2424
This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.
2525

26+
## Connection
27+
28+
A `Connection` allows Glue jobs, crawlers and development endpoints to access certain types of data stores. For example, to create a network connection to connect to a data source within a VPC:
29+
30+
```ts
31+
new glue.Connection(stack, 'MyConnection', {
32+
connectionType: glue.ConnectionTypes.NETWORK,
33+
// The security groups granting AWS Glue inbound access to the data source within the VPC
34+
securityGroups: [securityGroup],
35+
// The VPC subnet which contains the data source
36+
subnet,
37+
});
38+
```
39+
40+
If you need to use a connection type that doesn't exist as a static member on `ConnectionType`, you can instantiate a `ConnectionType` object, e.g: `new glue.ConnectionType('NEW_TYPE')`.
41+
42+
See [Adding a Connection to Your Data Store](https://docs.aws.amazon.com/glue/latest/dg/populate-add-connection.html) and [Connection Structure](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-catalog-connections.html#aws-glue-api-catalog-connections-Connection) documentation for more information on the supported data stores and their configurations.
43+
2644
## Database
2745

2846
A `Database` is a logical grouping of `Tables` in the Glue Catalog.

Diff for: packages/@aws-cdk/aws-glue/lib/connection.ts

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import * as ec2 from '@aws-cdk/aws-ec2';
2+
import * as cdk from '@aws-cdk/core';
3+
import * as constructs from 'constructs';
4+
import { CfnConnection } from './glue.generated';
5+
6+
/**
7+
* The type of the glue connection
8+
*
9+
* If you need to use a connection type that doesn't exist as a static member, you
10+
* can instantiate a `ConnectionType` object, e.g: `new ConnectionType('NEW_TYPE')`.
11+
*/
12+
export class ConnectionType {
13+
14+
/**
15+
* Designates a connection to a database through Java Database Connectivity (JDBC).
16+
*/
17+
public static readonly JDBC = new ConnectionType('JDBC');
18+
19+
/**
20+
* Designates a connection to an Apache Kafka streaming platform.
21+
*/
22+
public static readonly KAFKA = new ConnectionType('KAFKA');
23+
24+
/**
25+
* Designates a connection to a MongoDB document database.
26+
*/
27+
public static readonly MONGODB = new ConnectionType('MONGODB');
28+
29+
/**
30+
* Designates a network connection to a data source within an Amazon Virtual Private Cloud environment (Amazon VPC).
31+
*/
32+
public static readonly NETWORK = new ConnectionType('NETWORK');
33+
34+
/**
35+
* The name of this ConnectionType, as expected by Connection resource.
36+
*/
37+
public readonly name: string;
38+
39+
constructor(name: string) {
40+
this.name = name;
41+
}
42+
43+
/**
44+
* The connection type name as expected by Connection resource.
45+
*/
46+
public toString(): string {
47+
return this.name;
48+
}
49+
}
50+
51+
/**
52+
* Interface representing a created or an imported {@link Connection}
53+
*/
54+
export interface IConnection extends cdk.IResource {
55+
/**
56+
* The name of the connection
57+
* @attribute
58+
*/
59+
readonly connectionName: string;
60+
61+
/**
62+
* The ARN of the connection
63+
* @attribute
64+
*/
65+
readonly connectionArn: string;
66+
}
67+
68+
/**
69+
* Base Connection Options
70+
*/
71+
export interface ConnectionOptions {
72+
/**
73+
* The name of the connection
74+
* @default cloudformation generated name
75+
*/
76+
readonly connectionName?: string;
77+
78+
/**
79+
* The description of the connection.
80+
* @default no description
81+
*/
82+
readonly description?: string;
83+
84+
/**
85+
* Key-Value pairs that define parameters for the connection.
86+
* @default empty properties
87+
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-connect.html
88+
*/
89+
readonly properties?: { [key: string]: string };
90+
91+
/**
92+
* A list of criteria that can be used in selecting this connection.
93+
* This is useful for filtering the results of https://awscli.amazonaws.com/v2/documentation/api/latest/reference/glue/get-connections.html
94+
* @default no match criteria
95+
*/
96+
readonly matchCriteria?: string[];
97+
98+
/**
99+
* The list of security groups needed to successfully make this connection e.g. to successfully connect to VPC.
100+
* @default no security group
101+
*/
102+
readonly securityGroups?: ec2.ISecurityGroup[];
103+
104+
/**
105+
* The VPC subnet to connect to resources within a VPC. See more at https://docs.aws.amazon.com/glue/latest/dg/start-connecting.html.
106+
* @default no subnet
107+
*/
108+
readonly subnet?: ec2.ISubnet;
109+
}
110+
111+
/**
112+
* Construction properties for {@link Connection}
113+
*/
114+
export interface ConnectionProps extends ConnectionOptions {
115+
/**
116+
* The type of the connection
117+
*/
118+
readonly type: ConnectionType;
119+
}
120+
121+
/**
122+
* An AWS Glue connection to a data source.
123+
*/
124+
export class Connection extends cdk.Resource implements IConnection {
125+
126+
/**
127+
* Creates a Connection construct that represents an external connection.
128+
*
129+
* @param scope The scope creating construct (usually `this`).
130+
* @param id The construct's id.
131+
* @param connectionArn arn of external connection.
132+
*/
133+
public static fromConnectionArn(scope: constructs.Construct, id: string, connectionArn: string): IConnection {
134+
class Import extends cdk.Resource implements IConnection {
135+
public readonly connectionName = cdk.Arn.extractResourceName(connectionArn, 'connection');
136+
public readonly connectionArn = connectionArn;
137+
}
138+
139+
return new Import(scope, id);
140+
}
141+
142+
/**
143+
* Creates a Connection construct that represents an external connection.
144+
*
145+
* @param scope The scope creating construct (usually `this`).
146+
* @param id The construct's id.
147+
* @param connectionName name of external connection.
148+
*/
149+
public static fromConnectionName(scope: constructs.Construct, id: string, connectionName: string): IConnection {
150+
class Import extends cdk.Resource implements IConnection {
151+
public readonly connectionName = connectionName;
152+
public readonly connectionArn = Connection.buildConnectionArn(scope, connectionName);
153+
}
154+
155+
return new Import(scope, id);
156+
}
157+
158+
private static buildConnectionArn(scope: constructs.Construct, connectionName: string) : string {
159+
return cdk.Stack.of(scope).formatArn({
160+
service: 'glue',
161+
resource: 'connection',
162+
resourceName: connectionName,
163+
});
164+
}
165+
166+
/**
167+
* The ARN of the connection
168+
*/
169+
public readonly connectionArn: string;
170+
171+
/**
172+
* The name of the connection
173+
*/
174+
public readonly connectionName: string;
175+
176+
private readonly properties: {[key: string]: string};
177+
178+
constructor(scope: constructs.Construct, id: string, props: ConnectionProps) {
179+
super(scope, id, {
180+
physicalName: props.connectionName,
181+
});
182+
183+
this.properties = props.properties || {};
184+
185+
const physicalConnectionRequirements = props.subnet || props.securityGroups ? {
186+
availabilityZone: props.subnet ? props.subnet.availabilityZone : undefined,
187+
subnetId: props.subnet ? props.subnet.subnetId : undefined,
188+
securityGroupIdList: props.securityGroups ? props.securityGroups.map(sg => sg.securityGroupId) : undefined,
189+
} : undefined;
190+
191+
const connectionResource = new CfnConnection(this, 'Resource', {
192+
catalogId: cdk.Stack.of(this).account,
193+
connectionInput: {
194+
connectionProperties: cdk.Lazy.any({ produce: () => Object.keys(this.properties).length > 0 ? this.properties : undefined }),
195+
connectionType: props.type.name,
196+
description: props.description,
197+
matchCriteria: props.matchCriteria,
198+
name: props.connectionName,
199+
physicalConnectionRequirements,
200+
},
201+
});
202+
203+
const resourceName = this.getResourceNameAttribute(connectionResource.ref);
204+
this.connectionArn = Connection.buildConnectionArn(this, resourceName);
205+
this.connectionName = resourceName;
206+
}
207+
208+
/**
209+
* Add additional connection parameters
210+
* @param key parameter key
211+
* @param value parameter value
212+
*/
213+
public addProperty(key: string, value: string): void {
214+
this.properties[key] = value;
215+
}
216+
}

Diff for: packages/@aws-cdk/aws-glue/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// AWS::Glue CloudFormation Resources:
22
export * from './glue.generated';
33

4+
export * from './connection';
45
export * from './data-format';
56
export * from './database';
67
export * from './schema';

Diff for: packages/@aws-cdk/aws-glue/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"pkglint": "0.0.0"
8181
},
8282
"dependencies": {
83+
"@aws-cdk/aws-ec2": "0.0.0",
8384
"@aws-cdk/aws-iam": "0.0.0",
8485
"@aws-cdk/aws-kms": "0.0.0",
8586
"@aws-cdk/aws-s3": "0.0.0",
@@ -88,6 +89,7 @@
8889
},
8990
"homepage": "https://github.com/aws/aws-cdk",
9091
"peerDependencies": {
92+
"@aws-cdk/aws-ec2": "0.0.0",
9193
"@aws-cdk/aws-iam": "0.0.0",
9294
"@aws-cdk/aws-kms": "0.0.0",
9395
"@aws-cdk/aws-s3": "0.0.0",

0 commit comments

Comments
 (0)