From da78c1277abbcf2312f0830ad6df7bce31e739f8 Mon Sep 17 00:00:00 2001 From: Ladislav Dokoupil Date: Thu, 4 Jul 2024 14:27:59 +0200 Subject: [PATCH] download AWS logs on deployment failure --- ...sCloudFormationCloudDeploymentManager.java | 9 ++++++- .../main/java/sunstone/aws/impl/AwsUtils.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java b/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java index bfaf1041..644fdec9 100644 --- a/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java +++ b/aws/src/main/java/sunstone/aws/impl/AwsCloudFormationCloudDeploymentManager.java @@ -60,7 +60,14 @@ String deploy(CloudFormationClient cfClient, String template, Map waiterResponse = waiter.waitUntilStackCreateComplete(stacksRequest); + WaiterResponse 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; } diff --git a/aws/src/main/java/sunstone/aws/impl/AwsUtils.java b/aws/src/main/java/sunstone/aws/impl/AwsUtils.java index 68a65018..35c1a815 100644 --- a/aws/src/main/java/sunstone/aws/impl/AwsUtils.java +++ b/aws/src/main/java/sunstone/aws/impl/AwsUtils.java @@ -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; @@ -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)); @@ -116,4 +120,24 @@ static Optional 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(stackName+"-eventLog.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); + } + } }