ElastiCacheCluster作成(aws-cli)

# aws-cliでのElastiCacheClusterの作成

[公式](https://docs.aws.amazon.com/cli/latest/reference/elasticache)
## Create ParameterGroup

#### memcached

```sh
aws elasticache create-cache-parameter-group \
    --cache-parameter-group-name test-memcached  \
    --cache-parameter-group-family memcached1.4 \
    --description "test parameter group for memcached1.4"
```

#### Redis

```sh
aws elasticache create-cache-parameter-group \
    --cache-parameter-group-name test-redis  \
    --cache-parameter-group-family  redis4.0\
    --description "test parameter group for redis4.0"
```

## Create SubnetGroup

```sh
aws elasticache create-cache-subnet-group \
    --cache-subnet-group-name test-elasticache-subnet-group \
    --cache-subnet-group-description "test elasticache subnet group" \
    --subnet-ids "subnet-*******" "subnet-******"
```

## Create cache Cluster Memcached

```ini
ECACHE_CLUSTER_ID="test-cache-mem"
ECACHE_ENGINE_NAME="memcached" 
ECACHE_ENGINE_VERSION="1.4.33"
ECACHE_PARAM_GROUP_NAME="test-memcached"
ECACHE_SUBNET_GROUP_NAME="test-elasticache-subnet-group"
ECACHE_NODE_TYPE="cache.t2.micro"
ECACHE_NODE_COUNT=2
ARRAY_VPC_SG_IDS="sg-********"
AZ_MODE="cross-az"
```

```sh
aws elasticache create-cache-cluster \
        --cache-cluster-id ${ECACHE_CLUSTER_ID} \
        --engine ${ECACHE_ENGINE_NAME} \
        --engine-version ${ECACHE_ENGINE_VERSION} \
        --cache-parameter-group-name ${ECACHE_PARAM_GROUP_NAME} \
        --cache-subnet-group-name ${ECACHE_SUBNET_GROUP_NAME} \
        --cache-node-type ${ECACHE_NODE_TYPE} \
        --num-cache-nodes ${ECACHE_NODE_COUNT} \
        --security-group-ids ${ARRAY_VPC_SG_IDS} \
        --az-mode ${AZ_MODE}
```

## Create cache Cluster Redis

```ini
ECACHE_CLUSTER_ID="test-redis-001"
ECACHE_ENGINE_NAME="redis" 
ECACHE_ENGINE_VERSION="4.0.10"
ECACHE_PARAM_GROUP_NAME="test-redis"
ECACHE_SUBNET_GROUP_NAME="test-elasticache-subnet-group"
ECACHE_NODE_TYPE="cache.t2.micro"
ECACHE_NODE_COUNT=1
ARRAY_VPC_SG_IDS="sg-*********"
AZ_MODE="cross-az"
SNAP_RETENTION=1
PREFERRED_AZ="ap-northeast-1a"
```

```sh
aws elasticache create-cache-cluster \
        --cache-cluster-id ${ECACHE_CLUSTER_ID} \
        --engine ${ECACHE_ENGINE_NAME} \
        --engine-version ${ECACHE_ENGINE_VERSION} \
        --cache-parameter-group-name ${ECACHE_PARAM_GROUP_NAME} \
        --cache-subnet-group-name ${ECACHE_SUBNET_GROUP_NAME} \
        --cache-node-type ${ECACHE_NODE_TYPE} \
        --num-cache-nodes ${ECACHE_NODE_COUNT} \
        --security-group-ids ${ARRAY_VPC_SG_IDS} \
        --snapshot-retention-limit ${SNAP_RETENTION} \
        --preferred-availability-zone ${PREFERRED_AZ}
```

#### Create Repliaction Group
```ini
REPLICA_GROUP_ID="test-replica-group"
```

```sh
aws elasticache create-replication-group \
  --replication-group-id ${REPLICA_GROUP_ID} \
  --primary-cluster-id ${ECACHE_CLUSTER_ID} \
  --replication-group-description 'test replication group'
```

#### Add replica node
```ini
REPLICA_CLUSTER_ID="test-redis-002"
REPLICA_PREFERRED_AZ="ap-northeast-1c"
```

```sh
aws elasticache create-cache-cluster \
  --cache-cluster-id ${REPLICA_CLUSTER_ID} \
  --replication-group-id ${REPLICA_GROUP_ID} \
  --preferred-availability-zone ${REPLICA_PREFERRED_AZ}
```

#### Enable Auto Failover
```sh
aws elasticache modify-replication-group \
    --replication-group-id ${REPLICA_GROUP_ID} \
    --automatic-failover-enabled \
    --apply-immediately
```

## Test

#### memcached

```console
$ telnet endpoint 11211
ping
+PONG
```

#### Redis

```console
$ telnet endpoint 6379
ping
+PONG
```