Class: LambdaWrap::ApiGatewayManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_wrap/api_gateway_manager.rb

Overview

The ApiGatewayManager simplifies downloading the aws-apigateway-importer binary, importing a swagger configuration, and managing API Gateway stages.

Note: The concept of an environment of the LambdaWrap gem matches a stage in AWS ApiGateway terms.

Instance Method Summary collapse

Constructor Details

#initializeApiGatewayManager

The constructor does some basic setup

  • Validating basic AWS configuration

  • Creating the underlying client to interact with the AWS SDK.

  • Defining the temporary path of the api-gateway-importer jar file



18
19
20
21
22
23
24
25
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 18

def initialize()
  AwsSetup.new.validate()
  # AWS api gateway client

  @client = Aws::APIGateway::Client.new()
  # path to apigateway-importer jar

  @jarpath = File.join(Dir.tmpdir, 'aws-apigateway-importer-1.0.3-SNAPSHOT-jar-with-dependencies.jar')
  @versionpath = File.join(Dir.tmpdir, 'aws-apigateway-importer-1.0.3-SNAPSHOT-jar-with-dependencies.s3version')
end

Instance Method Details

#download_apigateway_importer(s3_bucket, s3_key) ⇒ Object

Downloads the aws-apigateway-importer jar from an S3 bucket. This is a workaround since aws-apigateway-importer does not provide a binary. Once a binary is available on the public internet, we’ll start using this instead of requiring users of this gem to upload their custom binary to an S3 bucket.

Arguments [s3_bucket] An S3 bucket from where the aws-apigateway-importer binary can be downloaded. [s3_key] The path (key) to the aws-apigateay-importer binary on the s3 bucket.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 36

def download_apigateway_importer(s3_bucket, s3_key)
  
  s3 = Aws::S3::Client.new
  
  # current version

  current_s3_version = File.open(@versionpath, 'rb').read if File.exists?(@versionpath)
  
  # online s3 version

  desired_s3_version = s3.head_object(bucket:s3_bucket, key:s3_key).version_id
  
  # compare local with remote version

  if current_s3_version != desired_s3_version || !File.exists?(@jarpath)
    puts "Downloading aws-apigateway-importer jar with S3 version #{desired_s3_version}"
    obj = s3.get_object(response_target:@jarpath, bucket:s3_bucket, key:s3_key)
    File.write(@versionpath, desired_s3_version)
  end
  
end

#setup_apigateway(api_name, env, swagger_file, api_description = "Deployed with LambdaWrap") ⇒ Object

Sets up the API gateway by searching whether the API Gateway already exists and updates it with the latest information from the swagger file.

Arguments [api_name] The name of the API to which the swagger file should be applied to. [env] The environment where it should be published (which is matching an API gateway stage) [swagger_file] A handle to a swagger file that should be used by aws-apigateway-importer [api_description] The description of the API to be displayed.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 64

def setup_apigateway(api_name, env, swagger_file, api_description = "Deployed with LambdaWrap")
  
  # ensure API is created

  api_id = get_existing_rest_api(api_name)
  api_id = setup_apigateway_create_rest_api(api_name, api_description) if !api_id
  
  # create resources

  setup_apigateway_create_resources(api_id, swagger_file)
  
  # create stages

  create_stages(api_id, env)
  
  # return URI of created stage

  return "https://#{api_id}.execute-api.#{ENV['AWS_REGION']}.amazonaws.com/#{env}/"
  
end

#shutdown_apigateway(api_name, env) ⇒ Object

Shuts down an environment from the API Gateway. This basically deletes the stage from the API Gateway, but does not delete the API Gateway itself.

Argument [api_name] The name of the API where the environment should be shut down. [env] The environment (matching an API Gateway stage) to shutdown.



88
89
90
91
92
93
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 88

def shutdown_apigateway(api_name, env)

  api_id = get_existing_rest_api(api_name)
  delete_stage(api_id, env)
  
end