Skip to content

Commit

Permalink
feat(elb): set accessLoggingPolicy property with L2 LoadBalancer (aws…
Browse files Browse the repository at this point in the history
…#14983)

Using cdk we can enable access logs for elb.Loadbalancer
fixes aws#14972

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ramancloudsmartz authored and hollanddd committed Aug 26, 2021
1 parent f14abc8 commit 940619b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ export interface LoadBalancerProps {
* @default - Public subnets if internetFacing, Private subnets otherwise
*/
readonly subnetSelection?: SubnetSelection;

/**
* Enable Loadbalancer access logs
* Can be used to avoid manual work as aws console
* Required S3 bucket name , enabled flag
* Can add interval for pushing log
* Can set bucket prefix in order to provide folder name inside bucket
* @default - disabled
*/
readonly accessLoggingPolicy?: CfnLoadBalancer.AccessLoggingPolicyProperty;

}

/**
Expand Down Expand Up @@ -262,6 +273,10 @@ export class LoadBalancer extends Resource implements IConnectable {
this.elb.node.addDependency(selectedSubnets.internetConnectivityEstablished);
}

if (props.accessLoggingPolicy !== undefined) {
this.elb.accessLoggingPolicy = props.accessLoggingPolicy;
}

ifUndefined(props.listeners, []).forEach(b => this.addListener(b));
ifUndefined(props.targets, []).forEach(t => this.addTarget(t));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,52 @@ describe('tests', () => {
sslCertificateId: sslCertificateArn,
})).toThrow(/"sslCertificateId" is deprecated, please use "sslCertificateArn" only./);
});

test('enable load balancer access logs', () => {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VCP');

// WHEN
new LoadBalancer(stack, 'LB', {
vpc,
accessLoggingPolicy: {
enabled: true,
s3BucketName: 'fakeBucket',
},
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancing::LoadBalancer', {
AccessLoggingPolicy: {
Enabled: true,
S3BucketName: 'fakeBucket',
},
});
});

test('disable load balancer access logs', () => {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VCP');

// WHEN
new LoadBalancer(stack, 'LB', {
vpc,
accessLoggingPolicy: {
enabled: false,
s3BucketName: 'fakeBucket',
},
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancing::LoadBalancer', {
AccessLoggingPolicy: {
Enabled: false,
S3BucketName: 'fakeBucket',
},
});
});
});

class FakeTarget implements ILoadBalancerTarget {
Expand Down

0 comments on commit 940619b

Please sign in to comment.