Skip to content

Commit

Permalink
Remove ASG notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
farski committed Jun 3, 2024
1 parent a7ae9a9 commit e98bbcd
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 176 deletions.
1 change: 0 additions & 1 deletion spire/templates/root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ Resources:
Properties:
Parameters:
EnvironmentType: !Ref EnvironmentType
EnvironmentTypeAbbreviation: !Ref EnvironmentTypeAbbreviation
RegionMode: !FindInMap [RegionModeMap, !Ref "AWS::Region", !Ref EnvironmentType]
RootStackName: !Ref AWS::StackName
RootStackId: !Ref AWS::StackName
Expand Down
175 changes: 0 additions & 175 deletions spire/templates/shared-ecs/asg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Parameters:
Default: /aws/service/ecs/optimized-ami/amazon-linux-2/amzn2-ami-ecs-hvm-2.0.20240312-x86_64-ebs/image_id
########
EnvironmentType: { Type: String }
EnvironmentTypeAbbreviation: { Type: String }
RegionMode: { Type: String }
RootStackName: { Type: String }
RootStackId: { Type: String }
Expand Down Expand Up @@ -448,180 +447,6 @@ Resources:
Weight: 1
CapacityProvider: !Ref DefaultCapacityProvider

EventsNotificationsFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub >-
Handles auto scaling events from the ${EnvironmentType} shared ASG
Environment:
Variables:
SLACK_MESSAGE_RELAY_TOPIC_ARN: !Ref SlackMessageRelaySnsTopicArn
Events:
AsgEventsRule:
# https://docs.aws.amazon.com/autoscaling/ec2/userguide/cloud-watch-events.html
Type: EventBridgeRule
Properties:
Pattern:
detail:
AutoScalingGroupName:
- !Ref Asg
source:
- aws.autoscaling
Handler: index.handler
InlineCode: |
const { SNS } = require('@aws-sdk/client-sns');
const sns = new SNS({
apiVersion: '2010-03-31',
region: process.env.SLACK_MESSAGE_RELAY_TOPIC_ARN.split(':')[3],
});
const regions = {
'us-east-2': 'Ohio',
'us-east-1': 'N. Virginia',
'us-west-1': 'N. California',
'us-west-2': 'Oregon',
'af-south-1': 'Cape Town',
'ap-east-1': 'Hong Kong',
'ap-south-1': 'Mumbai',
'ap-northeast-3': 'Osaka',
'ap-northeast-2': 'Seoul',
'ap-southeast-1': 'Singapore',
'ap-southeast-2': 'Sydney',
'ap-northeast-1': 'Tokyo',
'ca-central-1': 'Central',
'eu-central-1': 'Frankfurt',
'eu-west-1': 'Ireland',
'eu-west-2': 'London',
'eu-south-1': 'Milan',
'eu-west-3': 'Paris',
'eu-north-1': 'Stockholm',
'me-south-1': 'Bahrain',
'sa-east-1': 'São Paulo',
};
exports.handler = async (event) => {
console.log(JSON.stringify(event));
const asgUrl = `https://console.aws.amazon.com/ec2autoscaling/home?region=${event.region}#/details/${event.detail.AutoScalingGroupName}?view=details`;
const instanceUrl = `https://console.aws.amazon.com/ec2/v2/home?region=${event.region}#InstanceDetails:instanceId=${event.detail.EC2InstanceId}`;
let environment = '????';
if (event.detail.AutoScalingGroupName.includes('prod')) {
environment = 'prod';
} else if (event.detail.AutoScalingGroupName.includes('stag')) {
environment = 'stag';
}
const lines = [];
let inOut = '';
let az = '';
if (event?.detail?.Details?.['Availability Zone']) {
az = ` in \`${event.detail.Details['Availability Zone']}\``;
}
lines.push(`*Instance:* <${instanceUrl}|${event.detail.EC2InstanceId}>${az}`);
if (/capacity from [0-9]+ to [0-9]+/.test(event.detail.Cause)) {
const m = event.detail.Cause.match(/capacity from ([0-9]+) to ([0-9]+)/);
lines.push(`*Capacity change:* \`${m[1]}\` to \`${m[2]}\``);
inOut = +m[1] > +m[2] ? ' IN' : ' OUT';
} else if (/an instance was taken out of service in response to a user health-check/.test(event.detail.Cause)) {
lines.push('Taken out of service in response to a user health-check.');
} else if (/was taken out of service in response to a user request/.test(event.detail.Cause)) {
lines.push('Taken out of service in response to a user request.');
}
await sns.publish({
TargetArn: process.env.SLACK_MESSAGE_RELAY_TOPIC_ARN,
Message: JSON.stringify({
channel: 'G2QHC11SM', // #ops-debug
username: 'AWS Auto Scaling',
icon_emoji: ':ops-autoscaling:',
attachments: [{
color: "#53adfb",
fallback: `SCALE${inOut} | ${regions[event.region]} » ASG &lt;${environment}&gt; ${event['detail-type'].toUpperCase()}`,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `*<${asgUrl}|SCALE${inOut} | ${regions[event.region]} » ASG &lt;${environment}&gt; ${event['detail-type'].toUpperCase()}>*`,
}
}, {
type: "section",
text: {
type: "mrkdwn",
text: lines.join('\n'),
}
}
]
}]
}),
});
};
MemorySize: 128
Policies:
- Statement:
- Action: sns:Publish
Effect: Allow
Resource: !Ref SlackMessageRelaySnsTopicArn
Version: "2012-10-17"
Runtime: nodejs18.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
prx:cloudformation:stack-id: !Ref AWS::StackId
prx:cloudformation:root-stack-name: !Ref RootStackName
prx:cloudformation:root-stack-id: !Ref RootStackId
prx:ops:environment: !Ref EnvironmentType
prx:dev:application: Common
Timeout: 10
EventsNotificationsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${EventsNotificationsFunction}
RetentionInDays: 7
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:cloudformation:root-stack-name, Value: !Ref RootStackName }
- { Key: prx:cloudformation:root-stack-id, Value: !Ref RootStackId }
- { Key: prx:ops:environment, Value: !Ref EnvironmentType }
- { Key: prx:dev:application, Value: Common }
EventsNotificationsFunctionErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub WARN [Infrastructure] ASG Events Notifications <${EnvironmentTypeAbbreviation}> FUNCTION ERRORS (${RootStackName})
AlarmDescription: !Sub >-
The events notification function for the ${EnvironmentType} auto
scaling group has experienced some errors, so notifications may not
be getting sent to Slack.
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref EventsNotificationsFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:cloudformation:root-stack-name, Value: !Ref RootStackName }
- { Key: prx:cloudformation:root-stack-id, Value: !Ref RootStackId }
- { Key: prx:ops:environment, Value: !Ref EnvironmentType }
- { Key: prx:dev:application, Value: Infrastructure }
Threshold: 0
TreatMissingData: notBreaching

Outputs:
AsgName:
Value: !Ref Asg

0 comments on commit e98bbcd

Please sign in to comment.