-
-
Save billyteves/bb1eaeb0d22740cf6dfb69f18c526948 to your computer and use it in GitHub Desktop.
Elasticache replication group with Cloudformation and controlled by Terraform
This file contains 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
resource "template_file" "cf" { | |
vars { | |
cluster_name = "${var.cluster_name}" | |
csg_name = "${aws_elasticache_subnet_group.default_redis_sg.name}" | |
cluster_internal_sg_id = "${module.ecs-cluster.cluster_internal_sg_id}" | |
} | |
template = <<STACK | |
{ | |
"Resources" : { | |
"replgrp": { | |
"Type" : "AWS::ElastiCache::ReplicationGroup", | |
"Properties" : { | |
"AutomaticFailoverEnabled" : "true", | |
"AutoMinorVersionUpgrade" : "true", | |
"CacheNodeType" : "cache.m3.medium", | |
"CacheSubnetGroupName" : "${csg_name}", | |
"Engine" : "redis", | |
"NumCacheClusters" : "3", | |
"Port" : "6379", | |
"ReplicationGroupDescription" : "Replication Group for ${cluster_name}", | |
"SecurityGroupIds" : ["${cluster_internal_sg_id}"] | |
} | |
} | |
}, | |
"Outputs" : { | |
"endpoint" : { | |
"Description": "Redis Cluster Endpoint", | |
"Value" : { "Fn::GetAtt" : [ "replgrp", "PrimaryEndPoint.Address"]} | |
}, | |
"clusterid" : { | |
"Description": "Cluster ID", | |
"Value" : { "Ref": "replgrp" } | |
} | |
} | |
} | |
STACK | |
} | |
resource "aws_cloudformation_stack" "elasticache" { | |
name = "${var.cluster_name}-replication-group" | |
on_failure = "ROLLBACK" | |
template_body = "${template_file.cf.rendered}" | |
lifecycle { | |
create_before_destroy = false | |
} | |
} | |
output "elasticache-endpoint" { | |
value = "${aws_cloudformation_stack.elasticache.outputs.endpoint}" | |
} | |
output "clusterid" { | |
value = "${aws_cloudformation_stack.elasticache.outputs.clusterid}" | |
} | |
resource "null_resource" "modify-replication-group" { | |
provisioner "local-exec" { | |
command = "aws elasticache modify-replication-group --replication-group-id ${aws_cloudformation_stack.elasticache.outputs.clusterid} --snapshotting-cluster-id ${aws_cloudformation_stack.elasticache.outputs.clusterid}-001 --snapshot-retention-limit 7" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment