Вы можете создать лямбда-функцию для изменения ASG в том виде, в котором она создается с помощью CustomResource. Для этого также требуется IAM :: Role, поскольку лямбда-функции требуется ссылка на единицу как часть ее определения.
кредит https://gist.github.com/atward/9573b9fbd3bfd6c453158c28356bec05 для большей части этого:
ASG:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
DesiredCapacity: 1
MinSize: 1
MaxSize: 2
LaunchConfigurationName: !Ref LaunchConfigurationName
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
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):
props = event['ResourceProperties']
client = boto3.client('autoscaling')
try:
response = client.suspend_processes(AutoScalingGroupName=props['AutoScalingGroupName'], 'ReplaceUnhealthy'])
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
except Exception as e:
cfnresponse.send(event, context, cfnresponse.FAILED, {})
Handler: index.handler
Role:
Fn::GetAtt:
- AsgProcessModificationRole
- Arn
Runtime: python2.7
ModifyAsg:
Type: AWS::CloudFormation::CustomResource
Version: 1
Properties:
ServiceToken:
Fn::GetAtt:
- AsgProcessModifierFunction
- Arn
AutoScalingGroupName:
Ref: ASG
ScalingProcesses:
- ReplaceUnhealthy