@@ -55,17 +55,7 @@ provider "aws" {
5555"""
5656TF_S3_BACKEND_CONFIG = """
5757terraform {
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>
6959 }
7060}
7161"""
@@ -265,6 +255,10 @@ def generate_s3_backend_config() -> str:
265255 "key" : "terraform.tfstate" ,
266256 "dynamodb_table" : "tf-test-state" ,
267257 "region" : get_region (),
258+ "skip_credentials_validation" : True ,
259+ "skip_metadata_api_check" : True ,
260+ "secret_key" : "test" ,
261+
268262 "endpoints" : {
269263 "s3" : get_service_endpoint ("s3" ),
270264 "iam" : get_service_endpoint ("iam" ),
@@ -278,40 +272,44 @@ def generate_s3_backend_config() -> str:
278272 print ("Warning: Unsupported backend option(s) detected (`endpoints`). Please make sure you always use the corresponding options to your Terraform version." )
279273 exit (1 )
280274 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
281278 if legacy_endpoint in backend_config and (not backend_config .get ("endpoints" ) or endpoint not in backend_config ["endpoints" ]):
282279 if not backend_config .get ("endpoints" ):
283280 backend_config ["endpoints" ] = {}
284281 backend_config ["endpoints" ].update ({endpoint : backend_config [legacy_endpoint ]})
282+ del backend_config [legacy_endpoint ]
285283 # Add any missing default endpoints
286284 if backend_config .get ("endpoints" ):
287285 backend_config ["endpoints" ] = {
288286 k : backend_config ["endpoints" ].get (k ) or v
289287 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
290289 configs .update (backend_config )
291290 if not DRY_RUN :
292291 get_or_create_bucket (configs ["bucket" ])
293292 get_or_create_ddb_table (configs ["dynamodb_table" ], region = configs ["region" ])
294293 result = TF_S3_BACKEND_CONFIG
295- for key , value in configs .items ():
294+ config_options = ""
295+ for key , value in sorted (configs .items ()):
296296 if isinstance (value , bool ):
297297 value = str (value ).lower ()
298298 elif isinstance (value , dict ):
299299 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
308303 else :
309304 value = textwrap .indent (
310305 text = f"{ key } = {{\n " + "\n " .join ([f' { k } = "{ v } "' for k , v in value .items ()]) + "\n }" ,
311306 prefix = " " * 4 )
307+ config_options += f"\n { value } "
308+ continue
312309 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 )
315313 return result
316314
317315
0 commit comments