Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save d-shimizu/84c52e72a0d5fb395eaf981891462f4d to your computer and use it in GitHub Desktop.
Save d-shimizu/84c52e72a0d5fb395eaf981891462f4d to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: "2010-09-09"
Description: SNS Topic and EventBridge for SpotInstance Intteruption Notify
Parameters:
Env:
Type: String
Default: dev
AllowedValues:
- dev
- stg
- prd
SlackChannelId:
Type: String
Description: Slack Channel Id
SlackWorkSpaceId:
Type: String
Description: Slack WorkSpace Id
Resources:
# ----------------------------------------------------------------
# Define of IAM
# ----------------------------------------------------------------
SpotInstanceInterruptSlackNotifyIAMRole:
Type: AWS::IAM::Role
Properties:
#RoleName: !Sub ${AWS::StackName}-iam-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: chatbot.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/CloudWatchReadOnlyAccess
# ----------------------------------------------------------------
# Define of EventBridge
# ----------------------------------------------------------------
SpotInstanceInterruptEventRule:
Type: AWS::Events::Rule
DependsOn:
- SpotInstanceInterruptSlackNotifySNSTopic
- SpotInstanceInterruptSlackNotifySNSTopicPolicy
Properties:
Description: "EventRule"
EventPattern:
source:
- "aws.ec2"
detail-type:
- "EC2 Spot Instance Interruption Warning"
State: "ENABLED"
Targets:
-
Arn:
Ref: "SpotInstanceInterruptSlackNotifySNSTopic"
Id: "SpotInstanceInterruptSlackNotifySNSTopic"
# ----------------------------------------------------------------
# Define of SNS
# ----------------------------------------------------------------
SpotInstanceInterruptSlackNotifySNSTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub ${AWS::StackName}-sns-topic
SpotInstanceInterruptSlackNotifySNSTopicPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sns:Publish
Resource: '*'
Topics:
- !Ref SpotInstanceInterruptSlackNotifySNSTopic
# ----------------------------------------------------------------
# Define of Chatbot Slack Channel
# ----------------------------------------------------------------
SpotInstanceInterruptSlackNotifySlackChannelConfiguration:
Type: AWS::Chatbot::SlackChannelConfiguration
DependsOn:
- SpotInstanceInterruptSlackNotifySNSTopic
- SpotInstanceInterruptSlackNotifySNSTopicPolicy
Properties:
ConfigurationName: !Sub ${AWS::StackName}-chatbot
IamRoleArn: !GetAtt SpotInstanceInterruptSlackNotifyIAMRole.Arn
LoggingLevel: ERROR
SlackChannelId: !Ref SlackChannelId
SlackWorkspaceId: !Ref SlackWorkspaceId
SnsTopicArns:
- !Ref SpotInstanceInterruptSlackNotifySNSTopic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment