Skip to content

Commit

Permalink
Merge pull request #223 from laDok8/aws_event_log
Browse files Browse the repository at this point in the history
download AWS logs on deployment failure
  • Loading branch information
istraka authored Jul 25, 2024
2 parents 1e825b2 + 5104369 commit e0d6467
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ String deploy(CloudFormationClient cfClient, String template, Map<String, String
.stackName(stackName)
.build();

WaiterResponse<DescribeStacksResponse> waiterResponse = waiter.waitUntilStackCreateComplete(stacksRequest);
WaiterResponse<DescribeStacksResponse> waiterResponse;
try {
waiterResponse = waiter.waitUntilStackCreateComplete(stacksRequest);
} catch (Exception e) {
LOGGER.error("Stack {} failed to create", stackName);
AwsUtils.downloadStackEvents(cfClient, stackName);
throw e;
}
LOGGER.debug("Stack {} is ready {}", stackName, waiterResponse.matched().response().orElse(null));
return stackName;
}
Expand Down
24 changes: 24 additions & 0 deletions aws/src/main/java/sunstone/aws/impl/AwsUtils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sunstone.aws.impl;


import org.slf4j.Logger;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.DescribeStackEventsRequest;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
Expand All @@ -21,9 +23,11 @@

import sunstone.core.SunstoneConfigResolver;

import java.io.FileWriter;
import java.util.Optional;

class AwsUtils {
static Logger LOGGER = AwsLogger.DEFAULT;

private static AwsCredentialsProvider getCredentialsProvider() {
AwsBasicCredentials credentials = AwsBasicCredentials.create(SunstoneConfigResolver.getString(AwsConfig.ACCESS_KEY_ID), SunstoneConfigResolver.getString(AwsConfig.SECRET_ACCESS_KEY));
Expand Down Expand Up @@ -116,4 +120,24 @@ static Optional<DBInstance> findRdsInstanceByNameTag(RdsClient rdsClient, String

return response.dbInstances().stream().filter(db -> db.dbInstanceStatus().equals("available")).findFirst();
}

static void downloadStackEvents(CloudFormationClient cfClient, String stackName) {
try (FileWriter writer = new FileWriter("cloudformation-events-" + stackName + ".log")) {
cfClient.describeStackEvents(DescribeStackEventsRequest.builder().stackName(stackName).build()).stackEvents().forEach(event -> {
try {
String log = String.format("%s\t%s\t%s\t%s",
event.timestamp(),
event.logicalResourceId(),
event.resourceStatus(),
event.resourceStatusReason());
writer.write(log);
writer.write(System.lineSeparator());
} catch (Exception e) {
LOGGER.error("Error downloading stack events", e);
}
});
} catch (Exception e) {
LOGGER.error("Error downloading stack events", e);
}
}
}

0 comments on commit e0d6467

Please sign in to comment.