Last active
November 20, 2016 22:27
-
-
Save atward/9573b9fbd3bfd6c453158c28356bec05 to your computer and use it in GitHub Desktop.
AWS CustomResource example - Autoscaling group suspend/resume processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: 2010-09-09 | |
Description: 'Custom resource example - ASG suspend/resume' | |
Parameters: | |
Input: | |
Type: String | |
Description: Make ASG | |
Default: true | |
AllowedValues: [true, false] | |
Conditions: | |
Enabled: !Equals [!Ref Input, true] | |
Resources: | |
Asg: | |
Type: AWS::AutoScaling::AutoScalingGroup | |
Condition: Enabled | |
Properties: | |
AvailabilityZones: | |
Fn::GetAZs: | |
Ref: "AWS::Region" | |
LaunchConfigurationName: !Ref LC | |
MaxSize: 0 | |
MinSize: 0 | |
LC: | |
Type: AWS::AutoScaling::LaunchConfiguration | |
Properties: | |
# Update to suit | |
ImageId: "ami-db704cb8" | |
InstanceType: "t2.micro" | |
AsgProcessModificationRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Action: ['sts:AssumeRole'] | |
Effect: Allow | |
Principal: | |
Service: [lambda.amazonaws.com] | |
Policies: | |
- PolicyName: AsgProcessModification | |
PolicyDocument: | |
Version: 2012-10-17 | |
Statement: | |
- Effect: Allow | |
Action: | |
- 'autoscaling:ResumeProcesses' | |
- 'autoscaling:SuspendProcesses' | |
# http://docs.aws.amazon.com/autoscaling/latest/userguide/IAM.html#AutoScaling_ARN_Format | |
Resource: '*' | |
- Effect: Allow | |
Action: | |
- logs:CreateLogGroup, | |
- logs:CreateLogStream, | |
- logs:PutLogEvents | |
Resource: 'arn:aws:logs:*:*:*' | |
AsgProcessModifierFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Description: 'Modifies ASG processes during CF stack creation' | |
Code: | |
ZipFile: | | |
import cfnresponse | |
import boto3 | |
def handler(event, context): | |
print 'event: ', event | |
print 'context: ', context | |
client = boto3.client('autoscaling') | |
props = event['ResourceProperties'] | |
if props['Action'] == 'SuspendProcesses': | |
response = client.suspend_processes(AutoScalingGroupName=props['AutoScalingGroupName'], ScalingProcesses=props['ScalingProcesses']) | |
elif props['Action'] == 'ResumeProcesses': | |
response = client.resume_processes(AutoScalingGroupName=props['AutoScalingGroupName'], ScalingProcesses=props['ScalingProcesses']) | |
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, "CustomResourcePhysicalID") | |
Handler: index.handler | |
Role: !GetAtt AsgProcessModificationRole.Arn | |
Runtime: 'python2.7' | |
ModifyAsg: | |
Type: AWS::CloudFormation::CustomResource | |
Condition: Enabled | |
Version: 1.0 | |
Properties: | |
ServiceToken: !GetAtt AsgProcessModifierFunction.Arn | |
Action: SuspendProcesses | |
AutoScalingGroupName: !Ref Asg | |
ScalingProcesses: | |
- HealthCheck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment