Skip to content

Commit

Permalink
feat(cfn): use exponential backoff with jitter for cfn api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarneyjr committed May 14, 2024
1 parent ca140c2 commit 4296eba
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# cfn-changeset-viewer

## 0.0.5

### Patch Changes

- add exponential backoff to cfn api call

## 0.0.4

### Patch Changes
Expand Down
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
description = "cfn-changeset-viewer";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-23.11";
};
outputs = { nixpkgs, ... }:
let
shell = { system }:
let
pkgs = import nixpkgs {
system = system;
};
in
pkgs.mkShell {
buildInputs = [
pkgs.awscli2
pkgs.nodejs_20
];
};
in
{
devShells.aarch64-darwin.default = shell { system = "aarch64-darwin"; };
devShells.x86_64-darwin.default = shell { system = "x86_64-darwin"; };
devShells.aarch64-linux.default = shell { system = "aarch64-linux"; };
devShells.x86_64-linux.default = shell { system = "x86_64-linux"; };
};
}
27 changes: 20 additions & 7 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,26 @@ async function printChangeSet(changeSetId, stackName, path, showUnchangedPropert
Dynamic: 0,
};
if (!path) path = "";
const response = await cfn.send(
new DescribeChangeSetCommand({
ChangeSetName: changeSetId,
StackName: stackName,
IncludePropertyValues: true,
}),
);
let response;
let attempts = 0;

while (!response) {
try {
response = await cfn.send(
new DescribeChangeSetCommand({
ChangeSetName: changeSetId,
StackName: stackName,
IncludePropertyValues: true,
}),
);
} catch (err) {
if (!(err instanceof Error)) throw err;
if (err.name !== "Throttling") throw err;
if (attempts++ > 5) throw err;
// exponential backoff sleep with jitter
await new Promise((resolve) => setTimeout(resolve, 2 ** attempts * 1000 + Math.random() * 1000));
}
}

for (let change of response.Changes ?? []) {
const resourceChange = change.ResourceChange;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cfn-changeset-viewer",
"version": "0.0.4",
"version": "0.0.5",
"description": "View the details of a CloudFormation ChangeSet (including nested ones!) in a human-friendly way",
"main": "index.mjs",
"type": "module",
Expand Down

0 comments on commit 4296eba

Please sign in to comment.