Class: MovableInk::AWS

Inherits:
Object
  • Object
show all
Includes:
ApiGateway, Athena, Autoscaling, EC2, ElastiCache, Route53, S3, SNS, SSM
Defined in:
lib/movable_ink/aws.rb,
lib/movable_ink/aws/s3.rb,
lib/movable_ink/aws/ec2.rb,
lib/movable_ink/aws/sns.rb,
lib/movable_ink/aws/ssm.rb,
lib/movable_ink/version.rb,
lib/movable_ink/aws/athena.rb,
lib/movable_ink/aws/errors.rb,
lib/movable_ink/aws/route53.rb,
lib/movable_ink/aws/api_gateway.rb,
lib/movable_ink/aws/autoscaling.rb,
lib/movable_ink/aws/elasticache.rb

Defined Under Namespace

Modules: ApiGateway, Athena, Autoscaling, EC2, ElastiCache, Errors, Route53, S3, SNS, SSM

Constant Summary collapse

VERSION =
'1.0.2'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiGateway

#post_signed_gateway_request

Methods included from ElastiCache

#all_elasticache_replicas, #elasticache, #elasticache_primary, #elasticache_replica_in_my_az, #elasticache_replicas, #node_group_members, #node_group_members_in_my_az, #replication_group

Methods included from S3

#directory_exists?, #s3

Methods included from Athena

#athena

Methods included from SSM

#extract_parameters, #get_role_secrets, #get_secret, #run_with_backoff_and_client_fallback, #ssm_client, #ssm_client_failover

Methods included from Route53

#delete_resource_record_sets, #find_health_check_by_tag, #get_health_check_tags, #get_resource_record_sets_by_instance_name, #list_all_r53_resource_record_sets, #list_health_checks, #resource_record_sets, #route53

Methods included from Autoscaling

#autoscaling, #complete_lifecycle_action, #delete_role_tag, #keep_instance_alive, #mark_me_as_healthy, #mark_me_as_unhealthy, #record_lifecycle_action_heartbeat

Methods included from SNS

#add_subject_info, #notify_and_sleep, #notify_nsq_can_not_be_drained, #notify_pagerduty, #notify_slack, #sns, #sns_pagerduty_topic_arn, #sns_slack_topic_arn

Methods included from EC2

#all_instances, #assign_ip_address, #available_elastic_ips, #default_filter, #ec2, #elastic_ips, #instance_id, #instance_ip_addresses_by_role, #instance_ip_addresses_by_role_ordered, #instance_tags, #instances, #load_all_instances, #load_mi_env, #me, #mi_env, #private_ip_addresses, #private_ipv4, #redis_by_role, #statsd_host, #thopter, #thopter_filter, #thopter_instance, #unassigned_elastic_ips

Constructor Details

#initialize(environment: nil, global_service: nil) ⇒ AWS

Returns a new instance of AWS.



38
39
40
41
42
43
44
# File 'lib/movable_ink/aws.rb', line 38

def initialize(environment: nil, global_service: nil)
  @mi_env = environment
  if global_service
    @availability_zone = 'us-east-1a'
    @instance_id = global_service
  end
end

Class Method Details

.regionsObject



28
29
30
31
32
33
34
35
# File 'lib/movable_ink/aws.rb', line 28

def regions
  {
    'iad' => 'us-east-1',
    'rld' => 'us-west-2',
    'dub' => 'eu-west-1',
    'ord' => 'us-east-2'
  }
end

Instance Method Details

#availability_zoneObject



83
84
85
86
87
88
89
# File 'lib/movable_ink/aws.rb', line 83

def availability_zone
  @availability_zone ||= begin
    az = `ec2metadata --availability-zone 2>/dev/null`.chomp
    raise(MovableInk::AWS::Errors::EC2Required) if az.empty?
    az
  end
end

#datacenter(region: my_region) ⇒ Object



95
96
97
# File 'lib/movable_ink/aws.rb', line 95

def datacenter(region: my_region)
  regions.key(region)
end

#my_regionObject



91
92
93
# File 'lib/movable_ink/aws.rb', line 91

def my_region
  @my_region ||= availability_zone.chop
end

#regionsObject



79
80
81
# File 'lib/movable_ink/aws.rb', line 79

def regions
  self.class.regions
end

#run_with_backoff(quiet: false, tries: 9) ⇒ Object



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
# File 'lib/movable_ink/aws.rb', line 46

def run_with_backoff(quiet: false, tries: 9)
  tries.times do |num|
    begin
      return yield
    rescue Aws::EC2::Errors::RequestLimitExceeded,
           Aws::EC2::Errors::ResourceAlreadyAssociated,
           Aws::SNS::Errors::ThrottledException,
           Aws::AutoScaling::Errors::ThrottledException,
           Aws::S3::Errors::SlowDown,
           Aws::Route53::Errors::Throttling,
           Aws::Route53::Errors::ThrottlingException,
           Aws::Route53::Errors::PriorRequestNotComplete,
           Aws::Route53::Errors::ServiceUnavailable,
           Aws::SSM::Errors::TooManyUpdates,
           Aws::Athena::Errors::ThrottlingException,
           MovableInk::AWS::Errors::NoEnvironmentTagError
      sleep_time = (num+1)**2 + rand(10)
      if quiet
        (num >= tries - 1) ? notify_and_sleep(sleep_time, $!.class) : sleep(sleep_time)
      else
        notify_and_sleep(sleep_time, $!.class)
      end
    rescue Aws::Errors::ServiceError => e
      message = "#{e.class}: #{e.message}\nFrom `#{e.backtrace.last.gsub("`","'")}`"
      notify_slack(subject: 'Unhandled AWS API Error',
                   message: message)
      puts message
      raise MovableInk::AWS::Errors::ServiceError
    end
  end
  raise MovableInk::AWS::Errors::FailedWithBackoff
end