Skip to content

Commit

Permalink
feat(globalaccelerator): stabilize AWS Global Accelerator module (aws…
Browse files Browse the repository at this point in the history
…#13843)

There are a number of changes to this module, made in order to stabilize it. The changes are as follows:

* Endpoints as constructs would only work in TypeScript; they have been moved out as integration classes into
  `aws-globalaccelerator-endpoints` in order to support languages like Java and C#.
* The automatic naming algorithm has been changed to reduce chances of conflict.
* There are now convenience methods, `addListener()` and `addEndpointGroup()` that will create
  the appropriate objects, as alternatives to `new Listener()` and `new EndpointGroup()`.
* EndpointGroups can take a list of `endpoints` in the constructor.
* A Listener's `toPort` is optional (and defaults to `fromPort` if not supplied).
* Support all the EndpointGroup properties.
* An EndpointGroup's `region` is automatically determined from its configured endpoints, if possible.
* The looked-up SecurityGroup is no longer accessible as a full Security Group, it can just
  be reference as a Peer (modifying the rules is not recommended by AGA and should not be allowed
  from the CDK).


Changes to other libraries made to support this:

* core, elbv2: imported Load Balancers now are aware of the region and account they were actually imported from, in
  order to be able to make `region` implicit in the AGA API.

BREAKING CHANGE: automatic naming algorithm has been changed: if you have existing Accelerators you will need to pass an
explicit name to prevent them from being replaced. All endpoints are now added by calling `addEndpoint()` with a
target-specific class that can be found in `@aws-cdk/aws-globalaccelerator-endpoints`. The generated Security Group
is now looked up by calling `endpointGroup.connectionsPeer()`.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored and hollanddd committed Aug 26, 2021
1 parent b14cfce commit 40756ba
Show file tree
Hide file tree
Showing 40 changed files with 1,602 additions and 455 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@ class PrefixList implements IPeer {
public toEgressRuleConfig(): any {
return { destinationPrefixListId: this.prefixListId };
}
}
}
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ export = {
'NetworkLoadBalancedEC2Service accepts imported load balancer'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const nlbArn = 'arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer';
const nlbArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc, clusterName: 'MyCluster' });
cluster.addCapacity('Capacity', { instanceType: new ec2.InstanceType('t2.micro') });
Expand Down Expand Up @@ -1274,7 +1274,7 @@ export = {
'ApplicationLoadBalancedEC2Service accepts imported load balancer'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const albArn = 'arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer';
const albArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc, clusterName: 'MyCluster' });
cluster.addCapacity('Capacity', { instanceType: new ec2.InstanceType('t2.micro') });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ export = {
const stack1 = new cdk.Stack(app, 'MyStack');
const vpc1 = new ec2.Vpc(stack1, 'VPC');
const cluster1 = new ecs.Cluster(stack1, 'Cluster', { vpc: vpc1 });
const nlbArn = 'arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer';
const nlbArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const stack2 = new cdk.Stack(stack1, 'Stack2');
const cluster2 = ecs.Cluster.fromClusterAttributes(stack2, 'ImportedCluster', {
vpc: vpc1,
Expand Down Expand Up @@ -887,7 +887,7 @@ export = {
'passing in imported application load balancer and resources to ALB Fargate Service'(test: Test) {
// GIVEN
const stack1 = new cdk.Stack();
const albArn = 'arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer';
const albArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const vpc = new ec2.Vpc(stack1, 'Vpc');
const cluster = new ecs.Cluster(stack1, 'Cluster', { vpc, clusterName: 'MyClusterName' });
const sg = new ec2.SecurityGroup(stack1, 'SecurityGroup', { vpc });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ class ImportedApplicationLoadBalancer extends Resource implements IApplicationLo
public readonly vpc?: ec2.IVpc;

constructor(scope: Construct, id: string, private readonly props: ApplicationLoadBalancerAttributes) {
super(scope, id);
super(scope, id, {
environmentFromArn: props.loadBalancerArn,
});

this.vpc = props.vpc;
this.loadBalancerArn = props.loadBalancerArn;
this.connections = new ec2.Connections({
Expand Down Expand Up @@ -601,7 +604,9 @@ class LookedUpApplicationLoadBalancer extends Resource implements IApplicationLo
public readonly vpc?: ec2.IVpc;

constructor(scope: Construct, id: string, props: cxapi.LoadBalancerContextResponse) {
super(scope, id);
super(scope, id, {
environmentFromArn: props.loadBalancerArn,
});

this.loadBalancerArn = props.loadBalancerArn;
this.loadBalancerCanonicalHostedZoneId = props.loadBalancerCanonicalHostedZoneId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class NetworkLoadBalancer extends BaseLoadBalancer implements INetworkLoa
}
}

return new Import(scope, id);
return new Import(scope, id, { environmentFromArn: attrs.loadBalancerArn });
}

constructor(scope: Construct, id: string, props: NetworkLoadBalancerProps) {
Expand Down Expand Up @@ -306,7 +306,7 @@ class LookedUpNetworkLoadBalancer extends Resource implements INetworkLoadBalanc
public readonly vpc?: ec2.IVpc;

constructor(scope: Construct, id: string, props: cxapi.LoadBalancerContextResponse) {
super(scope, id);
super(scope, id, { environmentFromArn: props.loadBalancerArn });

this.loadBalancerArn = props.loadBalancerArn;
this.loadBalancerCanonicalHostedZoneId = props.loadBalancerCanonicalHostedZoneId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('tests', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');
const albArn = 'myArn';
const albArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const sg = new ec2.SecurityGroup(stack, 'sg', {
vpc,
securityGroupName: 'mySg',
Expand All @@ -303,7 +303,7 @@ describe('tests', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');
const albArn = 'MyArn';
const albArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const sg = new ec2.SecurityGroup(stack, 'sg', {
vpc,
securityGroupName: 'mySg',
Expand All @@ -319,6 +319,20 @@ describe('tests', () => {
expect(() => listener.addTargets('Targets', { port: 8080 })).not.toThrow();
});

test('imported load balancer knows its region', () => {
const stack = new cdk.Stack();

// WHEN
const albArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const alb = elbv2.ApplicationLoadBalancer.fromApplicationLoadBalancerAttributes(stack, 'ALB', {
loadBalancerArn: albArn,
securityGroupId: 'sg-1234',
});

// THEN
expect(alb.env.region).toEqual('us-west-2');
});

test('can add secondary security groups', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
Expand Down Expand Up @@ -364,6 +378,7 @@ describe('tests', () => {
expect(loadBalancer.loadBalancerDnsName).toEqual('my-load-balancer-1234567890.us-west-2.elb.amazonaws.com');
expect(loadBalancer.ipAddressType).toEqual(elbv2.IpAddressType.DUAL_STACK);
expect(loadBalancer.connections.securityGroups[0].securityGroupId).toEqual('sg-12345');
expect(loadBalancer.env.region).toEqual('us-west-2');
});

test('Can add listeners to a looked-up ApplicationLoadBalancer', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('tests', () => {
test('imported network load balancer with no vpc specified throws error when calling addTargets', () => {
// GIVEN
const stack = new cdk.Stack();
const nlbArn = 'arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer';
const nlbArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const nlb = elbv2.NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, 'NLB', {
loadBalancerArn: nlbArn,
});
Expand All @@ -240,7 +240,7 @@ describe('tests', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');
const nlbArn = 'arn:aws:elasticloadbalancing::000000000000::dummyloadbalancer';
const nlbArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const nlb = elbv2.NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, 'NLB', {
loadBalancerArn: nlbArn,
vpc,
Expand All @@ -250,6 +250,19 @@ describe('tests', () => {
expect(() => listener.addTargets('targetgroup', { port: 8080 })).not.toThrow();
});

test('imported load balancer knows its region', () => {
const stack = new cdk.Stack();

// WHEN
const albArn = 'arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188';
const alb = elbv2.NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, 'ALB', {
loadBalancerArn: albArn,
});

// THEN
expect(alb.env.region).toEqual('us-west-2');
});

test('Trivial construction: internal with Isolated subnets only', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -429,6 +442,7 @@ describe('tests', () => {
expect(loadBalancer.loadBalancerArn).toEqual('arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/network/my-load-balancer/50dc6c495c0c9188');
expect(loadBalancer.loadBalancerCanonicalHostedZoneId).toEqual('Z3DZXE0EXAMPLE');
expect(loadBalancer.loadBalancerDnsName).toEqual('my-load-balancer-1234567890.us-west-2.elb.amazonaws.com');
expect(loadBalancer.env.region).toEqual('us-west-2');
});

test('Can add listeners to a looked-up NetworkLoadBalancer', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const baseConfig = require('cdk-build-tools/config/eslintrc');
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
module.exports = baseConfig;
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-globalaccelerator-endpoints/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*.js
tsconfig.json
*.js.map
*.d.ts
*.generated.ts
dist
lib/generated/resources.ts
.jsii

.LAST_BUILD
.nyc_output
coverage
nyc.config.js
.LAST_PACKAGE
*.snk
.cdk.staging

lib/sdk-api-metadata.json
!.eslintrc.js
!jest.config.js

junit.xml
27 changes: 27 additions & 0 deletions packages/@aws-cdk/aws-globalaccelerator-endpoints/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Don't include original .ts files when doing `npm pack`
*.ts
!*.d.ts
coverage
.nyc_output
*.tgz

dist
.LAST_PACKAGE
.LAST_BUILD
!*.js

# Include .jsii
!.jsii

*.snk

*.tsbuildinfo

tsconfig.json
.eslintrc.js
jest.config.js

# exclude cdk artifacts
**/cdk.out
junit.xml
test/
Loading

0 comments on commit 40756ba

Please sign in to comment.