AWSTemplateFormatVersion: '2010-09-09' Parameters: DMSReplicationInstanceName: Description: DMS Replication Instance name that you want to enable autoscaling Type: String Default: "" NotificationSNSTopic: Description: SNS Topic ARN that will be used for notifying autoscaling events Type: String Default: "" BucketName: Description: Name of the s3 bucket that holds configuration file Type: String Default: aws-database-blog S3KeyName: Description: Name of the configuration file Type: String Default: artifacts/auto_scale_DMS_replication_instance/instance_types.json Timeout: Description: Total timeout value after which lambda function notifies the user that replication instance modification timed out Type: String Default: artifacts/auto_scale_DMS_replication_instance/instance_types.json Resources: iamRole: Type: AWS::IAM::Role Properties: AssumeRolePolicfyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - sts:AssumeRole Policies: - PolicyName: dmsautoscaler PolicyDocument: Version: '2012-10-17' Statement: - Sid: Stmt1528434137827 Action: - s3:GetObject Effect: Allow Resource: "*" - Sid: Stmt1528434233173 Action: - dms:DescribeReplicationInstances - dms:DescribeReplicationTasks - dms:ModifyReplicationInstance Effect: Allow Resource: "*" - Sid: Stmt1528473909949 Action: - logs:CreateLogGroup - logs:CreateLogStream - logs:PutLogEvents Effect: Allow Resource: arn:aws:logs:*:*:* - Sid: Stmt1528473909943 Action: - cloudwatch:DeleteAlarms - cloudwatch:EnableAlarmActions - cloudwatch:GetMetricData - cloudwatch:PutMetricAlarm Effect: Allow Resource: ["arn:aws:cloudwatch:::alarm:dms_cpu_high", "arn:aws:cloudwatch:::alarm:dms_cpu_low"] - Sid: Stmt1528473949712 Action: - events:DeleteRule - events:EnableRule - events:PutRule - events:PutTargets - events:RemoveTargets Effect: Allow Resource: "arn:aws:events::107995894928:rule/dms-scheduled-event*" - Sid: Stmt1528474058666 Action: - lambda:AddPermission - lambda:RemovePermission Effect: Allow Resource: arn:aws:lambda:*:*:function:dms_autoscaler_cf - Sid: Stmt1528474139639 Action: - sns:Publish Effect: Allow Resource: "*" RoleName: dmsautoscalerrole lambdaFunction: Type: AWS::Lambda::Function Properties: Code: S3Bucket: aws-database-blog S3Key: artifacts/auto_scale_DMS_replication_instance/dms.py.zip FunctionName: dms_autoscaler_cf Handler: dms.lambda_handler Environment: Variables: TOPIC_ARN: Ref: NotificationSNSTopic BUCKET_NAME: Ref: BucketName KEY_NAME: Ref: S3KeyName MODFICATION_TIMEOUT: Ref: Timeout Runtime: python3.7 MemorySize: 128 Timeout: 30 Description: DMS Autoscaler Role: Fn::GetAtt: - iamRole - Arn lambdaPermission: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: dms_autoscaler_cf Principal: sns.amazonaws.com SourceArn: Ref: snsTopic snsTopic: Type: AWS::SNS::Topic Properties: DisplayName: dmsautoscaler_cf Subscription: - Endpoint: Fn::GetAtt: - lambdaFunction - Arn Protocol: lambda TopicName: dmsautoscaler_cf cloudWatchHighCPUAlarm: Type: AWS::CloudWatch::Alarm Properties: MetricName: CPUUtilization Namespace: AWS/DMS ComparisonOperator: GreaterThanOrEqualToThreshold EvaluationPeriods: '1' Period: '60' Statistic: Average Threshold: '80' ActionsEnabled: 'true' AlarmActions: - Ref: snsTopic Dimensions: - Name: ReplicationInstanceIdentifier Value: Ref: DMSReplicationInstanceName AlarmDescription: High CPU usage by DMS instance. AlarmName: dms_cpu_high cloudWatchLowCPUAlarm: Type: AWS::CloudWatch::Alarm Properties: MetricName: CPUUtilization Namespace: AWS/DMS ComparisonOperator: LessThanOrEqualToThreshold EvaluationPeriods: '1' Period: '60' Statistic: Average Threshold: '30' ActionsEnabled: 'true' AlarmActions: - Ref: snsTopic Dimensions: - Name: ReplicationInstanceIdentifier Value: Ref: DMSReplicationInstanceName AlarmDescription: Low CPU usage by DMS instance. AlarmName: dms_cpu_low Outputs: IAMRole: Description: IAM Role for the lambda function Value: Fn::GetAtt: - iamRole - Arn LambdaFunction: Description: DMS Autoscaler lambda function Value: Fn::GetAtt: - lambdaFunction - Arn SNSTopic: Description: SNS Topic that will act as bridge between cloudwatch alarm and lambda function Value: Ref: snsTopic