Module: Linecook::Route53

Extended by:
Route53
Included in:
Route53
Defined in:
lib/linecook-gem/packager/route53.rb

Instance Method Summary collapse

Instance Method Details

#upsert_record(name, ami, region) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/linecook-gem/packager/route53.rb', line 7

def upsert_record(name, ami, region)
  ami_config = Linecook.config[:packager][:ami]

  zone = [ami_config[:zone], ami_config[:regions][region.to_sym], ami_config[:domain]].compact.join('.')
  record = "#{name}.#{zone}"

  resp = client.list_hosted_zones_by_name({
    dns_name: zone,
    max_items: 1,
  })

  if resp.hosted_zones.size < 1
    puts "Failed to find dns zone: #{resp.dns_name}"
    return false
  end


  zone_id = resp.hosted_zones[0].id
  client.change_resource_record_sets({
    hosted_zone_id: zone_id,
    change_batch: {
      comment: "create #{ami}",
      changes: [
        {
          action: "UPSERT",
          resource_record_set: {
            name: record,
            type: 'TXT',
            ttl: 1,
            resource_records: [
              {
                value: "\"#{ami}\"",
              },
            ],
          },
        },
      ],
    },
  })
  puts "Saved #{ami} to #{record}"
rescue Aws::Route53::Errors::ServiceError => e
  puts "AWS Error: #{e.code} #{e.context.http_response.body_contents}"
  return false
end