Skip to content

Commit

Permalink
Implements SNS on CloudFormation Template
Browse files Browse the repository at this point in the history
  • Loading branch information
Higherings committed Feb 21, 2020
1 parent 6fe1f42 commit 96938dc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
52 changes: 45 additions & 7 deletions autoComputeOptimizer-template.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# igarcia 2020-02
# Version 0.6
# Version 0.7
# Automation for Compute Optimizer Recommendations
# Create a Scheduled Lambda with CloudWatch Event to implements Compute Optimizer Recommendations changes on EC2 Instances
# Resources to create: Lambda, CloudWatch Event, IAM Role
# Resources to create: Lambda, CloudWatch Event, IAM Role, SNS Topic
# The AWS Account should be already signed up to AWS Compute Optimizer
# IMPORTANT: your EC2 instance should endure a restart!
# IMPORTANT: The EC2 instances that you want to affect should endure a restart!

AWSTemplateFormatVersion: 2010-09-09
Description: Template to automate the Compute Optimizer Recommendatios changes
Expand All @@ -27,6 +27,7 @@ Metadata:
- pHora
- pTagBusqueda
- pTagValor
- pCorreo
ParameterLabels: #Define Friendly Names
TemplateAmbiente:
default: "Value for CloudFormation Resources Env Tag"
Expand All @@ -42,6 +43,8 @@ Metadata:
default: "Selection Tag Key, sets the Tag used to identified Instances for Automation of Compute Optimizer"
pTagValor:
default: "Selection Tag Value, sets the Value of the Tag to identified instances"
pCorreo:
default: "E-Mail Address to receive Notifications about changes applied. Leave default value for NO Notifications."

Parameters: #Default Parameters Values
TemplateAmbiente:
Expand Down Expand Up @@ -90,10 +93,31 @@ Parameters: #Default Parameters Values
MaxLength: 20
MinLength: 1
ConstraintDescription: "Please set a simple value without spaces"
pCorreo:
Type: String
Description: "E-mail address for Notifications"
Default: "myemail@mydomain.com"
MaxLength: 50
MinLength: 5
ConstraintDescription: "Please write a valid e-mail address."

Resources:
TemplateRole: #Rol para ejecucion de Lambda
TemplateSNS:
Type: AWS::SNS::Topic
Properties:
DisplayName: !Join [ "-", [ "AutoComputeOptimizerTopic", !Ref TemplateAmbiente ] ]
TopicName: !Join [ "-", [ "AutoComputeOptimizerTopic", !Ref TemplateAmbiente ] ]
Subscription:
-
Endpoint: !Ref pCorreo
Protocol: email
Tags:
- Key: Env
Value: !Ref TemplateAmbiente

TemplateRole: #Role for Lambda
Type: AWS::IAM::Role
DependsOn: TemplateSNS
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Expand Down Expand Up @@ -138,6 +162,16 @@ Resources:
Action:
- "compute-optimizer:*"
Resource: "*"
-
PolicyName: !Join [ "-", [ "PolicyLambdaSNS", !Ref TemplateAmbiente ] ]
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "sns:*"
Resource: !Ref TemplateSNS
-
PolicyName: !Join [ "-", [ "PolicyLambdaKMS", !Ref TemplateAmbiente ] ]
PolicyDocument:
Expand All @@ -160,7 +194,7 @@ Resources:
- Key: Env
Value: !Ref TemplateAmbiente

TemplateLambda: #Lambda que copia los backups
TemplateLambda: #Lambda to implement Recommendations
Type: AWS::Lambda::Function
DependsOn: TemplateRole
Properties:
Expand All @@ -175,6 +209,7 @@ Resources:
TYPE: !Ref pType
TAGBUSQUEDA: !Ref pTagBusqueda
TAGVALOR: !Ref pTagValor
TOPIC: !Ref TemplateSNS
FunctionName: !Join [ "-", [ "AutoComputeOptimizerLambda", !Ref TemplateAmbiente ] ]
Role: !GetAtt TemplateRole.Arn
Timeout: 800
Expand All @@ -186,7 +221,7 @@ Resources:
Key: Env
Value: !Ref TemplateAmbiente

TemplateEventLambda: #Crea Event para invocar la Lambda
TemplateEventLambda: #Event to invoke Lambda
Type: AWS::Events::Rule
DependsOn: TemplateLambda
Properties:
Expand All @@ -208,7 +243,7 @@ Resources:
Principal: events.amazonaws.com
SourceArn: !GetAtt TemplateEventLambda.Arn

Outputs: #Salida a mostrarse en la pantalla
Outputs: #Outputs of the Template
FrequencyAutoCO:
Description: Frequency for Auto implement Compute Optimizer recommendations
Value: !Join [ "", [ "Every ", !Ref pFrecuencia, " days, at ", !Ref pHora, " UTC" ] ]
Expand All @@ -218,3 +253,6 @@ Outputs: #Salida a mostrarse en la pantalla
TolerableRisk:
Description: Tolerable Risk for the Recommendations (0 = very low - 5 = very high)
Value: !Ref pRisk
NotificationsEmail:
Description: Email address for Notifications
Value: !Ref pCorreo
2 changes: 1 addition & 1 deletion autocomputeoptimizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# igarcia 2020-02
# Version 0.6
# Version 0.7
# Automation for Compute Optimizer Recommendations
# It will change the EC2 Instance Type to a Recommendation of the AWS Compute Optimizer Service
# It won't do anything to AutoScaling Group's Instances
Expand Down

0 comments on commit 96938dc

Please sign in to comment.