Class: Terrafying::Components::Service
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::Service
- Includes:
- Usable
- Defined in:
- lib/terrafying/components/service.rb
Instance Attribute Summary collapse
-
#domain_names ⇒ Object
readonly
Returns the value of attribute domain_names.
-
#instance_profile ⇒ Object
readonly
Returns the value of attribute instance_profile.
-
#instance_set ⇒ Object
readonly
Returns the value of attribute instance_set.
-
#load_balancer ⇒ Object
readonly
Returns the value of attribute load_balancer.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ports ⇒ Object
readonly
Returns the value of attribute ports.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Class Method Summary collapse
Instance Method Summary collapse
- #allow_scrape(vpc, ports, security_group) ⇒ Object
- #create_in(vpc, name, options = {}) ⇒ Object
- #find_in(_vpc, _name) ⇒ Object
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #with_endpoint_service(options = {}) ⇒ Object
Methods included from Usable
#egress_security_group, #ingress_security_group, #path_mtu_setup!, #pingable_by, #pingable_by_cidr, #security_group, #used_by, #used_by_cidr
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
35 36 37 |
# File 'lib/terrafying/components/service.rb', line 35 def initialize super end |
Instance Attribute Details
#domain_names ⇒ Object (readonly)
Returns the value of attribute domain_names.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def domain_names @domain_names end |
#instance_profile ⇒ Object (readonly)
Returns the value of attribute instance_profile.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def instance_profile @instance_profile end |
#instance_set ⇒ Object (readonly)
Returns the value of attribute instance_set.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def instance_set @instance_set end |
#load_balancer ⇒ Object (readonly)
Returns the value of attribute load_balancer.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def load_balancer @load_balancer end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def name @name end |
#ports ⇒ Object (readonly)
Returns the value of attribute ports.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def ports @ports end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
23 24 25 |
# File 'lib/terrafying/components/service.rb', line 23 def zone @zone end |
Class Method Details
Instance Method Details
#allow_scrape(vpc, ports, security_group) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/terrafying/components/service.rb', line 175 def allow_scrape(vpc, ports, security_group) prom = Prometheus.find_in(vpc: vpc) ports.each do |port| sg_rule_ident = Digest::SHA256.hexdigest("#{vpc.name}-#{port}-#{security_group}-#{prom.security_group}") resource :aws_security_group_rule, sg_rule_ident.gsub(%r{^(\d)}, '_\1'), security_group_id: security_group, type: 'ingress', from_port: port, to_port: port, protocol: 'tcp', source_security_group_id: prom.security_group end end |
#create_in(vpc, name, options = {}) ⇒ Object
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 166 167 168 169 170 171 172 173 |
# File 'lib/terrafying/components/service.rb', line 43 def create_in(vpc, name, = {}) = { ami: aws.ami('base-image-fc-75aa2aef', owners = ['477284023816']), ignition: true, instance_type: 't3a.micro', ports: [], instances: [{}], zone: vpc.zone, cross_zone_load_balancing: false, iam_policy_statements: [], security_groups: [], keypairs: [], volumes: [], units: [], files: [], tags: {}, users: [], ssh_group: vpc.ssh_group, subnets: vpc.subnets.fetch(:private, []), startup_grace_period: 300, depends_on: [], metadata_options: nil, audit_role: "arn:aws:iam::#{aws.account_id}:role/auditd_logging", metrics_ports: [], vpc_endpoints_egress: [] }.merge() unless [:audit_role].nil? fluentd_conf = Auditd.fluentd_conf([:ignition], [:audit_role], [:tags].keys) = .merge_with_arrays_merged(fluentd_conf) end unless .key? :user_data if [:ignition] == true [:user_data] = Ignition.generate() elsif [:ignition] == false [:user_data] = Cloudconfig.generate() end end unless .key?(:loadbalancer_subnets) [:loadbalancer_subnets] = [:subnets] end unless [:instances].is_a?(Hash) || [:instances].is_a?(Array) raise 'Unknown instances option, should be hash or array' end ident = "#{tf_safe(vpc.name)}-#{name}" @name = ident @zone = [:zone] @ports = enrich_ports([:ports]) @domain_names = [[:zone].qualify(name)] depends_on = [:depends_on] + [:keypairs].map { |kp| kp[:resources] }.flatten.compact if .key? :instance_profile @instance_profile = [:instance_profile] else iam_statements = [:iam_policy_statements] + [:keypairs].map { |kp| kp[:iam_statement] }.compact @instance_profile = add! InstanceProfile.create(ident, statements: iam_statements) end = [:metadata_options] = [:tags].merge(service_name: name) set = [:instances].is_a?(Hash) ? DynamicSet : StaticSet if .key?(:loadbalancer) # explicitly requested or rejected a loadbalancer wants_load_balancer = [:loadbalancer] elsif [:cross_zone_load_balancing] # indirect request for an LB wants_load_balancer = true else # by default we want one if we are an ASG with exposed ports wants_load_balancer = set == DynamicSet && @ports.count > 0 end = { instance_profile: @instance_profile, depends_on: depends_on, metadata_options: , tags: } if wants_load_balancer && @ports.any? { |p| p.key?(:health_check) } [:health_check] = { type: 'ELB', grace_period: [:startup_grace_period] } end @instance_set = add! set.create_in(vpc, name, .merge()) @security_group = @instance_set.security_group if [:metrics_ports] && ![:metrics_ports].empty? allow_scrape(vpc, [:metrics_ports], @security_group) end if wants_load_balancer @load_balancer = add! LoadBalancer.create_in( vpc, name, .merge( subnets: [:loadbalancer_subnets], tags: , cross_zone_load_balancing: [:cross_zone_load_balancing] ) ) @load_balancer.attach(@instance_set) if @load_balancer.type == 'application' @security_group = @load_balancer.security_group @egress_security_group = @instance_set.security_group end @zone.add_alias_in(self, name, @load_balancer.alias_config) elsif set == StaticSet @zone.add_record_in(self, name, @instance_set.instances.map(&:ip_address)) @instance_set.instances.each do |i| @domain_names << vpc.zone.qualify(i.name) @zone.add_record_in(self, i.name, [i.ip_address]) end end if set == DynamicSet && [:rolling_update] == :signal @instance_profile.add_statement!( Effect: 'Allow', Action: ['cloudformation:SignalResource'], Resource: [@instance_set.stack] ) end self end |
#find_in(_vpc, _name) ⇒ Object
39 40 41 |
# File 'lib/terrafying/components/service.rb', line 39 def find_in(_vpc, _name) raise 'unimplemented' end |
#with_endpoint_service(options = {}) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/terrafying/components/service.rb', line 189 def with_endpoint_service( = {}) add! EndpointService.create_for(@load_balancer, @name, { fqdn: @domain_names[0], zone: @zone }.merge()) end |