@@ -102,6 +102,89 @@ resource "aws_s3_bucket_lifecycle_configuration" "datasources_lifecycle" {
102102 }
103103}
104104
105+ data "aws_iam_role" "existing_replication_role" {
106+ count = var. has_sub_environment_scope ? 0 : 1
107+ name = " immunisation-batch-${ var . environment } -replication"
108+ }
109+
110+ data "aws_iam_policy_document" "replication_allow_destination" {
111+ count = var. has_sub_environment_scope ? 0 : 1
112+
113+ statement {
114+ effect = " Allow"
115+
116+ actions = [
117+ " s3:ReplicateObject" ,
118+ " s3:ReplicateDelete" ,
119+ " s3:ReplicateTags" ,
120+ ]
121+
122+ resources = [" ${ aws_s3_bucket . batch_data_source_bucket . arn } /*" ]
123+ }
124+ }
125+
126+ resource "aws_iam_policy" "replication_allow_destination" {
127+ count = var. has_sub_environment_scope ? 0 : 1
128+ name = " ${ local . batch_prefix } -destination"
129+ policy = data. aws_iam_policy_document . replication_allow_destination [0 ]. json
130+ }
131+
132+ resource "aws_iam_role_policy_attachment" "replication_allow_destination" {
133+ count = var. has_sub_environment_scope ? 0 : 1
134+ role = data. aws_iam_role . existing_replication_role [0 ]. name
135+ policy_arn = aws_iam_policy. replication_allow_destination [0 ]. arn
136+ }
137+
138+ resource "aws_s3_bucket_replication_configuration" "replication" {
139+ count = var. has_sub_environment_scope ? 0 : 1
140+ role = data. aws_iam_role . existing_replication_role [0 ]. arn
141+ bucket = " immunisation-batch-${ local . resource_scope } -data-sources"
142+
143+ rule {
144+ id = var. sub_environment
145+ priority = strcontains (var. sub_environment , " blue" ) ? 0 : 1
146+ status = " Enabled"
147+
148+ filter {
149+ prefix = " "
150+ }
151+
152+ destination {
153+ bucket = aws_s3_bucket. batch_data_source_bucket . arn
154+ storage_class = " STANDARD"
155+
156+ replication_time {
157+ status = " Enabled"
158+ time {
159+ minutes = 15
160+ }
161+ }
162+ }
163+ }
164+
165+ rule {
166+ id = strcontains (var. sub_environment , " blue" ) ? replace (var. sub_environment , " blue" , " green" ) : replace (var. sub_environment , " green" , " blue" )
167+ priority = strcontains (var. sub_environment , " blue" ) ? 1 : 0
168+ status = " Disabled"
169+
170+ filter {
171+ prefix = " "
172+ }
173+
174+ destination {
175+ bucket = strcontains (aws_s3_bucket. batch_data_source_bucket . arn , " blue" ) ? replace (aws_s3_bucket. batch_data_source_bucket . arn , " blue" , " green" ) : replace (aws_s3_bucket. batch_data_source_bucket . arn , " green" , " blue" )
176+ storage_class = " STANDARD"
177+
178+ replication_time {
179+ status = " Enabled"
180+ time {
181+ minutes = 15
182+ }
183+ }
184+ }
185+ }
186+ }
187+
105188resource "aws_s3_bucket" "batch_data_destination_bucket" {
106189 # Deliberately not using `local.batch_prefix` as we don't want separate blue / green destinations in prod.
107190 bucket = " immunisation-batch-${ local . resource_scope } -data-destinations"
0 commit comments