diff --git a/src/main/java/hudson/plugins/ec2/EC2Cloud.java b/src/main/java/hudson/plugins/ec2/EC2Cloud.java index d7c43c659..6d5d1bbb8 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/EC2Cloud.java @@ -1047,14 +1047,22 @@ public Collection provision(final Label label, int excessWorkload) } } catch (AwsServiceException e) { LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e); - if (e.awsErrorDetails().errorCode().equals("RequestExpired") - || e.awsErrorDetails().errorCode().equals("ExpiredToken")) { - // A RequestExpired or ExpiredToken error can indicate that credentials have expired so reconnect - LOGGER.log(Level.INFO, "Reconnecting to EC2 due to RequestExpired or ExpiredToken error"); + if (e.awsErrorDetails().errorCode().equals("RequestExpired")) { + // A RequestExpired error can indicate that request has expired so reconnect + LOGGER.log(Level.INFO, "Reconnecting to EC2 due to RequestExpired error"); try { reconnectToEc2(); } catch (IOException e2) { - LOGGER.log(Level.WARNING, "Failed to reconnect ec2", e2); + LOGGER.log(Level.WARNING, "Failed to reconnect EC2", e2); + } + } else if (e.awsErrorDetails().errorCode().equals("ExpiredToken")) { + // A ExpiredToken error can indicate that token has expired so re-save the cloud + LOGGER.log(Level.INFO, "Re-saving EC2 cloud due to ExpiredToken error"); + try { + Jenkins.get().clouds.replace(this, this); + Jenkins.get().save(); + } catch (IOException e2) { + LOGGER.log(Level.WARNING, "Failed to re-save EC2 cloud: " + getCloudName(), e2); } } } catch (SdkException | IOException e) {