Class: AwsXRegionSync::Ec2AmiSync

Inherits:
AwsSync
  • Object
show all
Defined in:
lib/aws_xregion_sync/ec2_ami_sync.rb

Instance Attribute Summary

Attributes inherited from AwsSync

#config, #sync_name

Instance Method Summary collapse

Methods inherited from AwsSync

#aws_config, #create_sync_tag, #discover_aws_account_id, #initialize, #parse_config_filters, #parse_sync_tag_value, #sync_tag_indicator, #validate_config

Constructor Details

This class inherits a constructor from AwsXRegionSync::AwsSync

Instance Method Details

#syncObject



4
5
6
7
8
# File 'lib/aws_xregion_sync/ec2_ami_sync.rb', line 4

def sync
  setup = pre_sync_setup

  sync_ec2_image_to_region setup[:destination_region], setup[:source_region], setup[:image]
end

#sync_ec2_image_to_region(destination_region, source_region, image) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aws_xregion_sync/ec2_ami_sync.rb', line 15

def sync_ec2_image_to_region destination_region, source_region, image
  destination_ami_id = nil

  if needs_sync?(destination_region, image)
    # At this point, we know the sync has not happened (or the destination image has been removed)
    # Initiate the image copy command (we're not using client_token to ensure indempotency since we're already just using the Sync- identifier tag to ensure we're not 
    # copying the image multiple times to the destination region)
    results = destination_region.client.copy_image source_region: source_region.name, source_image_id: image.id, name: image.name

    destination_ami_id = results[:image_id]

    tag = create_sync_tag destination_region.name, destination_ami_id
    # We want to log the destination AMI-ID and the time we started the sync back to the source image
    image.tags[tag[:key]] = tag[:value]
  end
  
  destination_ami_id
end

#sync_required?Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/aws_xregion_sync/ec2_ami_sync.rb', line 10

def sync_required?
  setup = pre_sync_setup
  needs_sync? setup[:destination_region], setup[:image]
end