Skip to content

Commit

Permalink
Add basic support for stack set notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
farski committed Apr 22, 2024
1 parent 9fd9c9b commit 1fe3c39
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/sample-event/stack-set-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "0",
"id": "f8698be4-5139-e30e-e8c1-1462bbbe764e",
"detail-type": "CloudFormation StackSet Operation Status Change",
"source": "aws.cloudformation",
"account": "048723829744",
"time": "2024-04-22T03:27:25Z",
"region": "us-east-2",
"resources": [
"arn:aws:cloudformation:us-east-2:048723829744:stackset/org-sink-event-rules-management-account:76f20da2-a8b8-46bc-8556-2257dab866d0"
],
"detail": {
"stack-set-arn": "arn:aws:cloudformation:us-east-2:048723829744:stackset/org-sink-event-rules-management-account:76f20da2-a8b8-46bc-8556-2257dab866d0",
"stack-set-operation-id": "e22fb8de-5ac4-c606-e4a9-2ed951a823c5",
"action": "CREATE",
"status-details": { "status": "SUCCEEDED" }
}
}
11 changes: 9 additions & 2 deletions src/status-change-slack-notifications/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ import {
EventBridgeClient,
PutEventsCommand,
} from "@aws-sdk/client-eventbridge";
import msgFromEventBridgeEvent from "./eventbridge.mjs";
import stackMessage from "./stack-change.mjs";
import stackSetMessage from "./stack-set-change.mjs";

const eventbridge = new EventBridgeClient({ apiVersion: "2015-10-07" });

export const handler = async (event) => {
console.log(JSON.stringify(event));

const message = msgFromEventBridgeEvent(event);
let message;

if (event["detail-type"].includes("CloudFormation StackSet")) {
stackSetMessage(event);
} else {
message = stackMessage(event);
}

if (message) {
await eventbridge.send(
Expand Down
59 changes: 59 additions & 0 deletions src/status-change-slack-notifications/stack-set-change.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import regions from "./regions.mjs";
import accounts from "./accounts.mjs";

const SLACK_DEBUG_CHANNEL = "G2QHC11SM"; // #ops-debug
const SLACK_ICON = ":ops-cloudformation:";
const SLACK_USERNAME = "AWS CloudFormation";

function colorForResourceStatus(status) {
console.log(status);
return "#000000";
}

export default function message(event) {
const stackSetArn = event.detail["stack-set-arn"];

const stackSetID = stackSetArn.split("stackset/")[1];
const stackSetName = stackSetID.split(":")[0];

const { status } = event.detail["status-details"];

const { region } = event;
const stackUrl = `https://${region}.console.aws.amazon.com/cloudformation/home?region=${region}#/stacksets/${stackSetID}/info`;

const deepLinkRoleName = "ViewOnlyAccess";
const urlEncodedStackUrl = encodeURIComponent(stackUrl);
const deepStackUrl = `https://d-906713e952.awsapps.com/start/#/console?account_id=${event.account}&role_name=${deepLinkRoleName}&destination=${urlEncodedStackUrl}`;

const regionNickname = regions(region);
const accountNickname = accounts(event.account);
const header = [
`*<${deepStackUrl}|${accountNickname} - ${regionNickname} » ${stackSetName}>*`,
`Stack Set Status Change: *${status}*`,
].join("\n");

const fallback = `${accountNickname} - ${regionNickname} » Stack Set ${stackSetName} is now ${status}`;

const msg = {
username: SLACK_USERNAME,
icon_emoji: SLACK_ICON,
channel: SLACK_DEBUG_CHANNEL,
attachments: [
{
color: colorForResourceStatus(status),
fallback,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: header,
},
},
],
},
],
};

return msg;
}

0 comments on commit 1fe3c39

Please sign in to comment.