Module: Enscalator::Plugins::Elasticache
- Includes:
- Helpers
- Defined in:
- lib/enscalator/plugins/elasticache.rb
Overview
Collection of methods to work with ElastiCache (Redis)
Instance Method Summary collapse
-
#elasticache_cluster_init(app_name, cache_node_type: 'cache.m1.small', num_cache_nodes: 1) ⇒ Object
Create ElastiCache cluster.
-
#elasticache_repl_group_init(app_name, cache_node_type: 'cache.t2.small', num_cache_clusters: 2, seed: nil, **properties) ⇒ Object
Create ElastiCache replication group.
-
#init_cluster_resources(app_name, cache_node_type, param_group_seed: nil, **properties) ⇒ Object
Initialize resources common for all ElastiCache instances.
-
#magic_number(input) ⇒ String
Generates magic number (sha256 hex digest) from given Hash.
Methods included from Helpers
#cfn_call_script, #create_ssh_key, #find_ami, #flatten_hash, #gen_ssh_key_name, #init_assets_dir, #init_aws_config, #read_user_data, #run_cmd
Methods included from Helpers::Dns
#get_dns_records, #upsert_dns_record
Methods included from Helpers::Stack
#cfn_create_stack, #generate_parameters, #get_resource, #get_resources, #wait_stack
Methods included from Helpers::Wrappers
#cfn_client, #cfn_resource, #ec2_client, #route53_client
Instance Method Details
#elasticache_cluster_init(app_name, cache_node_type: 'cache.m1.small', num_cache_nodes: 1) ⇒ Object
Create ElastiCache cluster
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/enscalator/plugins/elasticache.rb', line 89 def elasticache_cluster_init(app_name, cache_node_type: 'cache.m1.small', num_cache_nodes: 1) cluster_resources = init_cluster_resources(app_name, cache_node_type) resource_name = "#{app_name}RedisCluster" resource resource_name, Type: 'AWS::ElastiCache::CacheCluster', Properties: { Engine: 'redis', NumCacheNodes: "#{num_cache_nodes}", CacheNodeType: cache_node_type, CacheSubnetGroupName: ref("#{app_name}ElasticacheSubnetGroup"), CacheParameterGroupName: ref(cluster_resources[:parameter_group]), VpcSecurityGroupIds: [get_att("#{app_name}RedisSecurityGroup", 'GroupId')] } resource_name # Unable to get created resource endpoint and port with Fn::GetAtt for engine == redis # For more details see here: # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-cache-cluster.html end |
#elasticache_repl_group_init(app_name, cache_node_type: 'cache.t2.small', num_cache_clusters: 2, seed: nil, **properties) ⇒ Object
Create ElastiCache replication group
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/enscalator/plugins/elasticache.rb', line 113 def elasticache_repl_group_init(app_name, cache_node_type: 'cache.t2.small', num_cache_clusters: 2, seed: nil, **properties) if %w(t1).map { |t| cache_node_type.include?(t) }.include?(true) fail "T1 instance types are not supported, got '#{cache_node_type}'" end cluster_properties = !properties.nil? && properties.key?(:cluster_properties) ? properties[:cluster_properties] : {} cluster_resources = init_cluster_resources(app_name, cache_node_type, param_group_seed: seed, parameter_group_properties: cluster_properties) resource_name = "#{app_name}RedisReplicationGroup" resource resource_name, Type: 'AWS::ElastiCache::ReplicationGroup', Properties: { Engine: 'redis', ReplicationGroupDescription: "Redis Replication group for #{app_name}", AutomaticFailoverEnabled: num_cache_clusters > 1 ? 'true' : 'false', NumCacheClusters: num_cache_clusters, CacheNodeType: cache_node_type, CacheSubnetGroupName: ref("#{app_name}ElasticacheSubnetGroup"), CacheParameterGroupName: ref(cluster_resources[:parameter_group]), SecurityGroupIds: [ get_att("#{app_name}RedisSecurityGroup", 'GroupId'), ref_private_security_group ] }.merge(properties.reject { |k, _v| k == :cluster_properties }) output "#{app_name}RedisReplicationGroup", Description: "Redis ReplicationGroup #{app_name}", Value: ref("#{app_name}RedisReplicationGroup") output "#{app_name}RedisPrimaryEndpointAddress", Description: "Redis Primary Endpoint Address #{app_name}", Value: get_att(resource_name, 'PrimaryEndPoint.Address') output "#{app_name}RedisPrimaryEndpointPort", Description: "Redis Primary Endpoint Port #{app_name}", Value: get_att(resource_name, 'PrimaryEndPoint.Port') output "#{app_name}RedisReadOnlyEndpointAddresses", Description: "Redis ReadOnly Endpoint Addresses #{app_name}", Value: get_att(resource_name, 'ReadEndPoint.Addresses') output "#{app_name}RedisReadOnlyEndpointPorts", Description: "Redis ReadOnly Endpoint Ports #{app_name}", Value: get_att(resource_name, 'ReadEndPoint.Ports') resource_name end |
#init_cluster_resources(app_name, cache_node_type, param_group_seed: nil, **properties) ⇒ Object
Initialize resources common for all ElastiCache instances
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/enscalator/plugins/elasticache.rb', line 26 def init_cluster_resources(app_name, cache_node_type, param_group_seed: nil, **properties) subnet_group_name = "#{app_name}ElasticacheSubnetGroup" resource subnet_group_name, Type: 'AWS::ElastiCache::SubnetGroup', Properties: { Description: 'SubnetGroup for elasticache', SubnetIds: ref_resource_subnets } security_group_name = security_group_vpc "#{app_name}RedisSecurityGroup", "Redis Security Group for #{app_name}", ref_vpc_id, security_group_ingress: [ { IpProtocol: 'tcp', FromPort: '6379', ToPort: '6389', SourceSecurityGroupId: ref_application_security_group } ], tags: { Name: join('-', aws_stack_name, 'res', 'sg'), Application: aws_stack_name } parameter_group_required_props = { 'reserved-memory': Core::InstanceType.elasticache_instance_type.max_memory(cache_node_type) / 2 } parameter_group_props = ( if !properties.nil? && properties.key?(:parameter_group_properties) properties[:parameter_group_properties] else {} end).merge(parameter_group_required_props) # TODO: remove this workaround when related template gets fixed parameter_group_name = if param_group_seed "#{app_name}RedisParameterGroup#{param_group_seed}" else "#{app_name}RedisParameterGroup#{magic_number(parameter_group_props)}" end resource parameter_group_name, Type: 'AWS::ElastiCache::ParameterGroup', Properties: { Description: "#{app_name} redis parameter group", CacheParameterGroupFamily: 'redis3.2', Properties: parameter_group_props } { subnet_group: subnet_group_name, security_group: security_group_name, parameter_group: parameter_group_name } end |
#magic_number(input) ⇒ String
Generates magic number (sha256 hex digest) from given Hash
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/enscalator/plugins/elasticache.rb', line 10 def magic_number(input) str = if input.is_a?(String) input elsif input.is_a?(Array) input.flatten.map(&:to_s).sort.join('&') elsif input.is_a?(Hash) flatten_hash(input).map { |k, v| "#{k}=#{v}" }.sort.join('&') else fail("Not supported input format: #{input.class}") end Digest::SHA256.hexdigest(str) end |