1. creating auto scaling policies for Replication Groups in ElastiCache

    TypeScript

    Sure, in order to create auto scaling policies for replication groups in ElastiCache, you should focus on two of AWS services: ElastiCache to manage the replication group and CloudWatch to create the scaling policies.

    In your Pulumi program, you are going to use aws.elasticache.ReplicationGroup to create replication group and aws.cloudwatch.MetricAlarm to create auto-scaling policies. After creating the aws.elasticache.ReplicationGroup, you will use its arn to attach alarms for it.

    Let's take a look at the TypeScript program which accomplishes the goal:

    import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; const exampleReplicationGroup = new aws.elasticache.ReplicationGroup("example", { automaticFailoverEnabled: true, clusterMode: { numNodeGroups: 2, replicasPerNodeGroup: 2, }, engine: "redis", nodeType: "cache.t2.small", parameterGroupName: "default.redis3.2.cluster.on", port: 6379, replicationGroupDescription: "test description", replicationGroupId: "example", subnetGroupName: "example", securityGroupIds: ["sg-12345678"], }); const cpuMetricAlarmHigh = new aws.cloudwatch.MetricAlarm("cpuHigh", { alarmDescription: "This metric alarms when CPU usage exceeds 70%", comparisonOperator: "GreaterThanOrEqualToThreshold", evaluationPeriods: "2", metricName: "CPUUtilization", namespace: "AWS/ElastiCache", period: "120", statistic: "Average", threshold: "70", alarmActions: [exampleReplicationGroup.arn], dimensions: { CacheClusterId: exampleReplicationGroup.clusterId, }, }); const cpuMetricAlarmLow = new aws.cloudwatch.MetricAlarm("cpuLow", { alarmDescription: "This metric alarms when CPU usage drops below 30%", comparisonOperator: "LessThanOrEqualToThreshold", evaluationPeriods: "2", metricName: "CPUUtilization", namespace: "AWS/ElastiCache", period: "120", statistic: "Average", threshold: "30", alarmActions: [exampleReplicationGroup.arn], dimensions: { CacheClusterId: exampleReplicationGroup.clusterId, }, }); export const replicationGroupId = exampleReplicationGroup.replicationGroupId;

    In this code, first, an ElastiCache replication group is created with the provided configuration options. The Group includes two node groups, and each group has two replicas. They're using the "cache.t2.small" instance type and running the Redis engine.

    Then, aws.cloudwatch.MetricAlarm is used to create two alarms for the replication group, one for high CPU usage and one for low CPU usage. The cpuMetricAlarmHigh alarms when CPU utilization exceeds 70% for 2 periods of 120 seconds and cpuMetricAlarmLow alarms when CPU utilization falls below 30% for the same period.

    The replication group identifier is exported as a stack output for use in other programs or for querying with the Pulumi CLI.

    The metric alarms could be triggered to execute the respective actions when the CPU usage goes above or below the specified thresholds. However, the action pointers in these cases are just referencing the replication group which is not a meaningful operation. Likely you will want to adjust these to point to the appropriate Auto Scaling Policy. Unfortunately, ElastiCache doesn't natively support auto-scaling in AWS. But an AWS Lambda function can be used as a workaround to resize the ElastiCache cluster upon certain CloudWatch alarm triggers.

    Please refer to the aws.elasticache.ReplicationGroup and aws.cloudwatch.MetricAlarm for more information.