@@ -564,6 +564,25 @@ export class BlockPublicAccess {
564
564
}
565
565
}
566
566
567
+ /**
568
+ * Specifies a metrics configuration for the CloudWatch request metrics from an Amazon S3 bucket.
569
+ */
570
+ export interface BucketMetrics {
571
+ /**
572
+ * The ID used to identify the metrics configuration.
573
+ */
574
+ readonly id : string ;
575
+ /**
576
+ * The prefix that an object must have to be included in the metrics results.
577
+ */
578
+ readonly prefix ?: string ;
579
+ /**
580
+ * Specifies a list of tag filters to use as a metrics configuration filter.
581
+ * The metrics configuration includes only objects that meet the filter's criteria.
582
+ */
583
+ readonly tagFilters ?: { [ tag : string ] : any } ;
584
+ }
585
+
567
586
export interface BucketProps {
568
587
/**
569
588
* The kind of server-side encryption to apply to this bucket.
@@ -639,6 +658,13 @@ export interface BucketProps {
639
658
* @see https://docs.aws.amazon.com/AmazonS3/latest/dev/access-control-block-public-access.html
640
659
*/
641
660
readonly blockPublicAccess ?: BlockPublicAccess ;
661
+
662
+ /**
663
+ * The metrics configuration of this bucket.
664
+ *
665
+ * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-metricsconfiguration.html
666
+ */
667
+ readonly metrics ?: BucketMetrics [ ] ;
642
668
}
643
669
644
670
/**
@@ -720,6 +746,7 @@ export class Bucket extends BucketBase {
720
746
private readonly lifecycleRules : LifecycleRule [ ] = [ ] ;
721
747
private readonly versioned ?: boolean ;
722
748
private readonly notifications : BucketNotifications ;
749
+ private readonly metrics : BucketMetrics [ ] = [ ] ;
723
750
724
751
constructor ( scope : Construct , id : string , props : BucketProps = { } ) {
725
752
super ( scope , id ) ;
@@ -735,7 +762,8 @@ export class Bucket extends BucketBase {
735
762
versioningConfiguration : props . versioned ? { status : 'Enabled' } : undefined ,
736
763
lifecycleConfiguration : new Token ( ( ) => this . parseLifecycleConfiguration ( ) ) ,
737
764
websiteConfiguration : this . renderWebsiteConfiguration ( props ) ,
738
- publicAccessBlockConfiguration : props . blockPublicAccess
765
+ publicAccessBlockConfiguration : props . blockPublicAccess ,
766
+ metricsConfigurations : new Token ( ( ) => this . parseMetricConfiguration ( ) )
739
767
} ) ;
740
768
741
769
applyRemovalPolicy ( resource , props . removalPolicy !== undefined ? props . removalPolicy : RemovalPolicy . Orphan ) ;
@@ -752,6 +780,9 @@ export class Bucket extends BucketBase {
752
780
753
781
this . disallowPublicAccess = props . blockPublicAccess && props . blockPublicAccess . blockPublicPolicy ;
754
782
783
+ // Add all bucket metric configurations rules
784
+ ( props . metrics || [ ] ) . forEach ( this . addMetric . bind ( this ) ) ;
785
+
755
786
// Add all lifecycle rules
756
787
( props . lifecycleRules || [ ] ) . forEach ( this . addLifecycleRule . bind ( this ) ) ;
757
788
@@ -791,6 +822,15 @@ export class Bucket extends BucketBase {
791
822
this . lifecycleRules . push ( rule ) ;
792
823
}
793
824
825
+ /**
826
+ * Adds a metrics configuration for the CloudWatch request metrics from the bucket.
827
+ *
828
+ * @param metric The metric configuration to add
829
+ */
830
+ public addMetric ( metric : BucketMetrics ) {
831
+ this . metrics . push ( metric ) ;
832
+ }
833
+
794
834
/**
795
835
* Adds a bucket notification event destination.
796
836
* @param event The event to trigger the notification
@@ -942,6 +982,8 @@ export class Bucket extends BucketBase {
942
982
return undefined ;
943
983
}
944
984
985
+ const self = this ;
986
+
945
987
return { rules : this . lifecycleRules . map ( parseLifecycleRule ) } ;
946
988
947
989
function parseLifecycleRule ( rule : LifecycleRule ) : CfnBucket . RuleProperty {
@@ -958,22 +1000,40 @@ export class Bucket extends BucketBase {
958
1000
prefix : rule . prefix ,
959
1001
status : enabled ? 'Enabled' : 'Disabled' ,
960
1002
transitions : rule . transitions ,
961
- tagFilters : parseTagFilters ( rule . tagFilters )
1003
+ tagFilters : self . parseTagFilters ( rule . tagFilters )
962
1004
} ;
963
1005
964
1006
return x ;
965
1007
}
1008
+ }
966
1009
967
- function parseTagFilters ( tagFilters ?: { [ tag : string ] : any } ) {
968
- if ( ! tagFilters || tagFilters . length === 0 ) {
969
- return undefined ;
970
- }
1010
+ private parseMetricConfiguration ( ) : CfnBucket . MetricsConfigurationProperty [ ] | undefined {
1011
+ if ( ! this . metrics || this . metrics . length === 0 ) {
1012
+ return undefined ;
1013
+ }
971
1014
972
- return Object . keys ( tagFilters ) . map ( tag => ( {
973
- key : tag ,
974
- value : tagFilters [ tag ]
975
- } ) ) ;
1015
+ const self = this ;
1016
+
1017
+ return this . metrics . map ( parseMetric ) ;
1018
+
1019
+ function parseMetric ( metric : BucketMetrics ) : CfnBucket . MetricsConfigurationProperty {
1020
+ return {
1021
+ id : metric . id ,
1022
+ prefix : metric . prefix ,
1023
+ tagFilters : self . parseTagFilters ( metric . tagFilters )
1024
+ } ;
1025
+ }
1026
+ }
1027
+
1028
+ private parseTagFilters ( tagFilters ?: { [ tag : string ] : any } ) {
1029
+ if ( ! tagFilters || tagFilters . length === 0 ) {
1030
+ return undefined ;
976
1031
}
1032
+
1033
+ return Object . keys ( tagFilters ) . map ( tag => ( {
1034
+ key : tag ,
1035
+ value : tagFilters [ tag ]
1036
+ } ) ) ;
977
1037
}
978
1038
979
1039
private renderWebsiteConfiguration ( props : BucketProps ) : CfnBucket . WebsiteConfigurationProperty | undefined {
0 commit comments