diff --git a/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts b/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts index d60d91e0a9a51..e4029c9dc55c6 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancing/lib/load-balancer.ts @@ -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; + } /** @@ -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)); } diff --git a/packages/@aws-cdk/aws-elasticloadbalancing/test/loadbalancer.test.ts b/packages/@aws-cdk/aws-elasticloadbalancing/test/loadbalancer.test.ts index 9cac87e057e87..aa6c1c8b88ad0 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancing/test/loadbalancer.test.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancing/test/loadbalancer.test.ts @@ -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 {