async function ensureLambdaUsEast1Stack()

in src/cfn-custom-resources/us-east-1-lambda-stack/index.ts [218:305]


async function ensureLambdaUsEast1Stack(props: {
  stackId: string;
  stackName: string;
  newTemplate: string;
}) {
  console.log(
    "Creating change set for adding lambda functions to us-east-1 stack ..."
  );
  const { Id: changeSetArn } = await CFN_CLIENT_US_EAST_1.createChangeSet({
    StackName: props.stackName,
    ChangeSetName: props.stackName,
    TemplateBody: props.newTemplate,
    ChangeSetType: "UPDATE",
    ResourceTypes: ["AWS::Lambda::Function"],
  }).promise();
  if (!changeSetArn)
    throw new Error(
      "Failed to create change set for lambda handlers deployment"
    );
  console.log(
    "Waiting for completion of change set for adding lambda functions to us-east-1 stack ..."
  );
  await CFN_CLIENT_US_EAST_1.waitFor("changeSetCreateComplete", {
    ChangeSetName: changeSetArn,
  })
    .promise()
    .catch((err) =>
      console.log(
        `Caught exception while waiting for change set create completion: ${err}`
      )
    );
  const { Status: status, StatusReason: reason } =
    await CFN_CLIENT_US_EAST_1.describeChangeSet({
      ChangeSetName: changeSetArn,
    }).promise();
  if (status === "FAILED") {
    // The only reason we'll allow a FAILED change set is if there were no changes
    if (!reason?.includes("didn't contain changes")) {
      throw new Error(`Failed to create change set: ${reason}`);
    } else {
      // No changes to make to the Lambda@Edge functions, clean up the change set then
      await CFN_CLIENT_US_EAST_1.deleteChangeSet({
        ChangeSetName: changeSetArn,
      }).promise();

      // Need to get the outputs (Lambda ARNs) from the existing stack then
      const { Stacks: existingStacks } =
        await CFN_CLIENT_US_EAST_1.describeStacks({
          StackName: props.stackName,
        }).promise();
      const existingOutputs = extractOutputsFromStackResponse(existingStacks);
      console.log(
        `us-east-1 stack unchanged. Stack outputs: ${JSON.stringify(
          existingOutputs,
          null,
          2
        )}`
      );
      return existingOutputs as { [key: string]: string };
    }
  }

  // Execute change set and wait for completion
  console.log(
    "Executing change set for adding lambda functions to us-east-1 stack ..."
  );
  await CFN_CLIENT_US_EAST_1.executeChangeSet({
    ChangeSetName: changeSetArn,
  }).promise();
  console.log(
    "Waiting for completion of execute change set for adding lambda functions to us-east-1 stack ..."
  );
  const { Stacks: updatedStacks } = await CFN_CLIENT_US_EAST_1.waitFor(
    "stackUpdateComplete",
    {
      StackName: props.stackName,
    }
  ).promise();
  const outputs = extractOutputsFromStackResponse(updatedStacks);
  console.log(
    `us-east-1 stack succesfully updated. Stack outputs: ${JSON.stringify(
      outputs,
      null,
      2
    )}`
  );
  return outputs as { [key: string]: string };
}