@@ -97,33 +97,26 @@ def aws_load_credentials(verbose=False):
9797 return creds
9898
9999
100- def _aws_sts_check (
101- aws_access_key_id = None ,
102- aws_secret_access_key = None ,
103- aws_session_token = "" ,
104- region = "us-east-1" ,
105- verbose = False ,
106- ):
100+ def _aws_clear_credentials (verbose = False ):
107101 """
108- Check AWS credentials by calling sts get-caller-identity.
109- If explicit credentials are provided, they are passed via env vars.
110- Otherwise, relies on the default AWS credential chain (supports SSO login).
111- Returns True if credentials are valid, False otherwise.
102+ Remove explicit credentials from ~/.aws/credentials so the default
103+ credential chain falls through to SSO.
112104 """
113- if aws_access_key_id and aws_secret_access_key :
114- env_override = (
115- f"AWS_ACCESS_KEY_ID='{ aws_access_key_id } '"
116- f" AWS_SECRET_ACCESS_KEY='{ aws_secret_access_key } '"
117- f" AWS_DEFAULT_REGION='{ region } '"
118- )
119- if aws_session_token :
120- env_override += f" AWS_SESSION_TOKEN='{ aws_session_token } '"
121- cmd = f"{ env_override } aws sts get-caller-identity"
122- else :
123- cmd = f"AWS_DEFAULT_REGION='{ region } ' aws sts get-caller-identity"
105+ creds_file = Path (AWS_STATE_DIR ) / "credentials"
106+ if creds_file .exists ():
107+ if verbose :
108+ click .echo (colorize_info ("* Clearing stale credentials file..." ))
109+ creds_file .unlink ()
110+
124111
112+ def _aws_sts_check (region = "us-east-1" , verbose = False ):
113+ """
114+ Check if current AWS credentials are valid by calling sts get-caller-identity
115+ using the default credential chain.
116+ Returns True if credentials are valid, False otherwise.
117+ """
125118 res = shell_command (
126- cmd ,
119+ f"AWS_DEFAULT_REGION=' { region } ' aws sts get-caller-identity" ,
127120 verbose = verbose ,
128121 exit_on_error = False ,
129122 capture_output = True ,
@@ -185,108 +178,80 @@ def _aws_export_credentials(verbose=False):
185178 return True
186179
187180
188- def aws_run_configure (verbose = False ):
181+ def _aws_sso_login (verbose = False ):
189182 """
190- Run `aws configure` interactively to let the user set credentials .
183+ Run `aws login --remote` to authenticate via SSO .
191184 """
192185 Path (AWS_STATE_DIR ).mkdir (parents = True , exist_ok = True )
193- click .echo (colorize_info ("* Running `aws configure `..." ))
186+ click .echo (colorize_info ("* Running `aws login --remote `..." ))
194187 shell_command (
195- "aws configure " ,
188+ "aws login --remote " ,
196189 verbose = verbose ,
197190 exit_on_error = False ,
198191 )
199192
200193
201194def aws_validate_credentials (deployment_name = None , region = None , verbose = False ):
202195 """
203- Validate stored AWS credentials. If invalid/expired, run `aws configure`
204- to let the user re-enter them, then validate again.
196+ Validate AWS credentials using SSO login flow.
197+
198+ Clears any stale exported credentials, checks the default credential chain,
199+ and if invalid, runs `aws login --remote` to authenticate. After successful
200+ login, exports resolved credentials so Terraform and Packer can use them.
205201
206202 Since ~/.aws is symlinked to state/.aws/, credentials persist across
207- container restarts and are automatically used by terraform and AWS CLI .
203+ container restarts.
208204
209205 If region is provided, it will be used for validation and saved to config.
210206 """
211207 click .echo (colorize_info ("* Validating AWS credentials..." ))
212208
213- current_creds = aws_load_credentials (verbose = verbose )
214-
215- # use provided region, or fall back to stored region, or default
216- effective_region = region or current_creds .get ("region" ) or "us-east-1"
209+ stored_region = _aws_cli_get ("region" , verbose = verbose )
210+ effective_region = region or stored_region or "us-east-1"
217211
218212 if verbose :
219213 click .echo (colorize_info (f"* Using region: { effective_region } " ))
220214
221- # first, try the default credential chain (covers SSO login, instance profiles, etc.)
222- if _aws_sts_check (region = effective_region , verbose = verbose ):
223- click .echo (colorize_info ("* AWS credentials are valid!" ))
224- # if no explicit credentials are configured, export resolved
225- # credentials (e.g. from SSO) so Terraform and Packer can use them
226- if not current_creds .get ("aws_access_key_id" ):
227- _aws_export_credentials (verbose = verbose )
228- # ensure region is set in AWS config
229- if region and region != current_creds .get ("region" ):
230- if verbose :
231- click .echo (colorize_info (f"* Updating stored region to: { region } " ))
232- aws_cli_set ("region" , region , verbose = verbose )
233- elif not current_creds .get ("region" ) and effective_region :
234- if verbose :
235- click .echo (colorize_info (f"* Setting region to: { effective_region } " ))
236- aws_cli_set ("region" , effective_region , verbose = verbose )
237- return
215+ # clear stale exported credentials so the default chain uses SSO
216+ _aws_clear_credentials (verbose = verbose )
238217
239- # fall back to explicit credentials from aws configure
240- if current_creds .get ("aws_access_key_id" ) and _aws_sts_check (
241- current_creds ["aws_access_key_id" ],
242- current_creds ["aws_secret_access_key" ],
243- current_creds .get ("aws_session_token" , "" ),
244- effective_region ,
245- verbose = verbose ,
246- ):
218+ # check if we have a valid SSO session
219+ if _aws_sts_check (region = effective_region , verbose = verbose ):
247220 click .echo (colorize_info ("* AWS credentials are valid!" ))
248- if region and region != current_creds .get ("region" ):
249- if verbose :
250- click .echo (colorize_info (f"* Updating stored region to: { region } " ))
251- aws_cli_set ("region" , region , verbose = verbose )
252- elif not current_creds .get ("region" ) and effective_region :
253- if verbose :
254- click .echo (colorize_info (f"* Setting region to: { effective_region } " ))
255- aws_cli_set ("region" , effective_region , verbose = verbose )
221+ _aws_export_credentials (verbose = verbose )
222+ _aws_set_region (region , stored_region , effective_region , verbose )
256223 return
257224
258- click .echo (
259- colorize_info ("* AWS credentials are invalid, expired, or not configured." )
260- )
225+ # no valid session — run SSO login
226+ click .echo (colorize_info ("* AWS credentials are invalid, expired, or not configured." ))
261227
262228 while True :
263- aws_run_configure (verbose = verbose )
264-
265- # reload and validate
266- new_creds = aws_load_credentials (verbose = verbose )
267- effective_region = region or new_creds .get ("region" ) or effective_region
229+ _aws_sso_login (verbose = verbose )
268230
269- click .echo (colorize_info ("* Validating new credentials..." ))
270-
271- # try default credential chain first, then explicit credentials
231+ click .echo (colorize_info ("* Validating credentials..." ))
272232 if _aws_sts_check (region = effective_region , verbose = verbose ):
273233 click .echo (colorize_info ("* AWS credentials are valid!" ))
274- break
275- elif new_creds .get ("aws_access_key_id" ) and _aws_sts_check (
276- new_creds ["aws_access_key_id" ],
277- new_creds ["aws_secret_access_key" ],
278- new_creds .get ("aws_session_token" , "" ),
279- effective_region ,
280- verbose = verbose ,
281- ):
282- click .echo (colorize_info ("* AWS credentials are valid!" ))
234+ _aws_export_credentials (verbose = verbose )
235+ _aws_set_region (region , stored_region , effective_region , verbose )
283236 break
284237 else :
285238 click .echo (
286239 colorize_error ("* AWS credentials are still invalid. Please try again." )
287240 )
288241
289242
243+ def _aws_set_region (region , stored_region , effective_region , verbose = False ):
244+ """Save region to AWS config if needed."""
245+ if region and region != stored_region :
246+ if verbose :
247+ click .echo (colorize_info (f"* Updating stored region to: { region } " ))
248+ aws_cli_set ("region" , region , verbose = verbose )
249+ elif not stored_region and effective_region :
250+ if verbose :
251+ click .echo (colorize_info (f"* Setting region to: { effective_region } " ))
252+ aws_cli_set ("region" , effective_region , verbose = verbose )
253+
254+
290255def aws_stop_instance (instance_id , verbose = False ):
291256 shell_command (
292257 f"aws ec2 stop-instances --instance-ids '{ instance_id } '" ,
0 commit comments