An alternative to Wait Operations for cloudformations

0

Hi, I'm upgrading my code from aws sdk go v1 to v2 and I was wondering what the best way to implement a wait operation for cloudformation clusters. At the moment I'm using DescribeStacks and using a switch statement to check for the required stack status through iteration eg:

func waitForStackOperationComplete(ctx context.Context, client f.CloudFormationApiClient, stackName string) error {
	maxRetries := 120
	retryCount := 0

	for {
		describeStacksInput := &cloudformation.DescribeStacksInput{
			StackName: aws.String(stackName),
		}

		resp, err := client.DescribeStacks(ctx, describeStacksInput)
		if err != nil {
			return err
		}

		if len(resp.Stacks) == 0 {
			return fmt.Errorf("stack not found")
		}

		stack := resp.Stacks[0]
		switch stack.StackStatus {
		case cloudformationtypes.StackStatusCreateComplete, cloudformationtypes.StackStatusUpdateComplete,
			cloudformationtypes.StackStatusDeleteComplete, cloudformationtypes.StackStatusRollbackComplete:
			return nil
		case cloudformationtypes.StackStatusCreateFailed, cloudformationtypes.StackStatusRollbackFailed,
			cloudformationtypes.StackStatusUpdateFailed, cloudformationtypes.StackStatusDeleteFailed:
			return fmt.Errorf("stack operation failed with status: %s", string(stack.StackStatus))
		case cloudformationtypes.StackStatusCreateInProgress, cloudformationtypes.StackStatusUpdateInProgress,
			cloudformationtypes.StackStatusRollbackInProgress, cloudformationtypes.StackStatusDeleteInProgress,
			cloudformationtypes.StackStatusUpdateCompleteCleanupInProgress:
			retryCount++
			if retryCount >= maxRetries {
				return fmt.Errorf("max retry count reached")
			}
			// Stack operation is still in progress. Wait for 5 seconds before checking the status again.
			time.Sleep(5 * time.Second)
		default:
			return fmt.Errorf("unexpected stack status: %s", string(stack.StackStatus))
		}
	}
}

I was wondering if there's an easier way to implement this. Thanks

2 Answers
1

Hello,

From the description, I understand that you are using a Custom Go SDK script to perform DescribeStacks operation to make a waitForStackOperationComplete function and you would like to know if there is any other simpler/better way to implement such function to add delay while the CloudFormation stack reaches a terminal state.

I would like to inform you that your SDK approach looks valid and I do not see anything immediately incorrect. However, please do note that my expertise with Go language is little limited, however the overall approach is fine.

Further, I checked for any existing methods which can do the polling part to check the stack status implicitly and I was able to find "wait" commands reference in AWS CLI. These commands are essentially used to wait until a particular condition is satisfied where each subcommand polls an API until the listed requirement is met.

You can also find the above reference in Go SDK by searching for the methods with term "Waiters" in the SDK v1 doc and SDK v2 doc.

Kindly also note that while investigating more about the wait commands, I came across two Open GitHub issues[1][2]. These are feature requests to enable CloudFormation to wait for any final state, and display events while waiting instead of the specific waiters for each state as per the current SDK Waiter methods.

Another workaround mentioned in the comments was to use a third party Nodejs module "aws-cloudformation-wait-ready". As per the description, it is similar to aws cloudformation wait stack-update-complete but waits for any stack status where an update should be allowed. However, I could not find any Go SDK equivalent atleast as per my research. Hence, you can check their approach to handle the issue and implement the same in Go (if you find it better).

Based on all the above investigation and findings, it is safe to conclude that at the moment, a Custom Go SDK script is needed until the above feature is released and make its way into SDK eventually.

AWS
Harsha
answered 5 months ago
0

Thanks for the help

answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions