Skip to content

Instantly share code, notes, and snippets.

@MrPink
Created August 11, 2016 19:27
Show Gist options
  • Save MrPink/393840d460c0cb1caa5074d3fafd5657 to your computer and use it in GitHub Desktop.
Save MrPink/393840d460c0cb1caa5074d3fafd5657 to your computer and use it in GitHub Desktop.
Elasticache replication group with Cloudformation and controlled by Terraform
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