@@ -55,17 +55,7 @@ provider "aws" {
55
55
"""
56
56
TF_S3_BACKEND_CONFIG = """
57
57
terraform {
58
- backend "s3" {
59
- region = "<region>"
60
- bucket = "<bucket>"
61
- key = "<key>"
62
- dynamodb_table = "<dynamodb_table>"
63
-
64
- access_key = "test"
65
- secret_key = "test"
66
- <endpoints>
67
- skip_credentials_validation = true
68
- skip_metadata_api_check = true
58
+ backend "s3" {<configs>
69
59
}
70
60
}
71
61
"""
@@ -265,6 +255,10 @@ def generate_s3_backend_config() -> str:
265
255
"key" : "terraform.tfstate" ,
266
256
"dynamodb_table" : "tf-test-state" ,
267
257
"region" : get_region (),
258
+ "skip_credentials_validation" : True ,
259
+ "skip_metadata_api_check" : True ,
260
+ "secret_key" : "test" ,
261
+
268
262
"endpoints" : {
269
263
"s3" : get_service_endpoint ("s3" ),
270
264
"iam" : get_service_endpoint ("iam" ),
@@ -278,40 +272,44 @@ def generate_s3_backend_config() -> str:
278
272
print ("Warning: Unsupported backend option(s) detected (`endpoints`). Please make sure you always use the corresponding options to your Terraform version." )
279
273
exit (1 )
280
274
for legacy_endpoint , endpoint in legacy_endpoint_mappings .items ():
275
+ if legacy_endpoint in backend_config and backend_config .get ("endpoints" ) and endpoint in backend_config ["endpoints" ]:
276
+ del backend_config [legacy_endpoint ]
277
+ continue
281
278
if legacy_endpoint in backend_config and (not backend_config .get ("endpoints" ) or endpoint not in backend_config ["endpoints" ]):
282
279
if not backend_config .get ("endpoints" ):
283
280
backend_config ["endpoints" ] = {}
284
281
backend_config ["endpoints" ].update ({endpoint : backend_config [legacy_endpoint ]})
282
+ del backend_config [legacy_endpoint ]
285
283
# Add any missing default endpoints
286
284
if backend_config .get ("endpoints" ):
287
285
backend_config ["endpoints" ] = {
288
286
k : backend_config ["endpoints" ].get (k ) or v
289
287
for k , v in configs ["endpoints" ].items ()}
288
+ backend_config ["access_key" ] = get_access_key (backend_config ) if CUSTOMIZE_ACCESS_KEY else DEFAULT_ACCESS_KEY
290
289
configs .update (backend_config )
291
290
if not DRY_RUN :
292
291
get_or_create_bucket (configs ["bucket" ])
293
292
get_or_create_ddb_table (configs ["dynamodb_table" ], region = configs ["region" ])
294
293
result = TF_S3_BACKEND_CONFIG
295
- for key , value in configs .items ():
294
+ config_options = ""
295
+ for key , value in sorted (configs .items ()):
296
296
if isinstance (value , bool ):
297
297
value = str (value ).lower ()
298
298
elif isinstance (value , dict ):
299
299
if key == "endpoints" and is_tf_legacy :
300
- value = textwrap .indent (
301
- text = textwrap .dedent (f"""\
302
- endpoint = "{ value ["s3" ]} "
303
- iam_endpoint = "{ value ["iam" ]} "
304
- sts_endpoint = "{ value ["sts" ]} "
305
- dynamodb_endpoint = "{ value ["dynamodb" ]} "
306
- """ ),
307
- prefix = " " * 4 )
300
+ for legacy_endpoint , endpoint in legacy_endpoint_mappings .items ():
301
+ config_options += f'\n { legacy_endpoint } = "{ configs [key ][endpoint ]} "'
302
+ continue
308
303
else :
309
304
value = textwrap .indent (
310
305
text = f"{ key } = {{\n " + "\n " .join ([f' { k } = "{ v } "' for k , v in value .items ()]) + "\n }" ,
311
306
prefix = " " * 4 )
307
+ config_options += f"\n { value } "
308
+ continue
312
309
else :
313
- value = str (value )
314
- result = result .replace (f"<{ key } >" , value )
310
+ value = f'"{ str (value )} "'
311
+ config_options += f'\n { key } = { value } '
312
+ result = result .replace ("<configs>" , config_options )
315
313
return result
316
314
317
315
0 commit comments