Module: Jets::AwsServices

Instance Method Summary collapse

Instance Method Details

#cfnObject



16
17
18
# File 'lib/jets/aws_services.rb', line 16

def cfn
  @cfn ||= Aws::CloudFormation::Client.new
end

#lambdaObject



20
21
22
# File 'lib/jets/aws_services.rb', line 20

def lambda
  @lambda ||= Aws::Lambda::Client.new
end

#logsObject



28
29
30
# File 'lib/jets/aws_services.rb', line 28

def logs
  @logs ||= Aws::CloudWatchLogs::Client.new
end

#s3Object



8
9
10
# File 'lib/jets/aws_services.rb', line 8

def s3
  @s3 ||= Aws::S3::Client.new
end

#s3_resourceObject



12
13
14
# File 'lib/jets/aws_services.rb', line 12

def s3_resource
  @s3_resource ||= Aws::S3::Resource.new
end

#stack_exists?(stack_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jets/aws_services.rb', line 32

def stack_exists?(stack_name)
  return false if ENV['TEST']

  exist = nil
  begin
    # When the stack does not exist an exception is raised. Example:
    # Aws::CloudFormation::Errors::ValidationError: Stack with id blah does not exist
    resp = cfn.describe_stacks(stack_name: stack_name)
    exist = true
  rescue Aws::CloudFormation::Errors::ValidationError => e
    if e.message =~ /does not exist/
      exist = false
    elsif e.message.include?("'stackName' failed to satisfy constraint")
      # Example of e.message when describe_stack with invalid stack name
      # "1 validation error detected: Value 'instance_and_route53' at 'stackName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]*"
      puts "Invalid stack name: #{stack_name}"
      puts "Full error message: #{e.message}"
      exit 1
    else
      raise # re-raise exception  because unsure what other errors can happen
    end
  end
  exist
end

#stack_in_progress?(stack_name) ⇒ Boolean

All CloudFormation states listed here: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html

Returns resp so we can use it to grab data about the stack without calling api again.

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jets/aws_services.rb', line 60

def stack_in_progress?(stack_name)
  return true if !stack_exists?(stack_name)

  # Assumes stack exists
  resp = cfn.describe_stacks(stack_name: stack_name)
  status = resp.stacks[0].stack_status
  if status =~ /_IN_PROGRESS$/
    puts "The '#{stack_name}' stack status is #{status}. " \
         "Please wait until the stack is ready and try again.".colorize(:red)
    exit 0
  elsif resp.stacks[0].outputs.empty?
    # This Happens when the miminal stack fails at the very beginning.
    # There is no s3 bucket at all.  User should delete the stack.
    puts "The minimal stack failed to create. Please delete the stack first and try again." \
    "You can delete the CloudFormation stack or use the `jets delete` command"
    exit 0
  else
    true
  end
end

#stsObject



24
25
26
# File 'lib/jets/aws_services.rb', line 24

def sts
  @sts ||= Aws::STS::Client.new
end