Elastic Beanstalk examples using SDK for Ruby - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Elastic Beanstalk examples using SDK for Ruby

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Ruby with Elastic Beanstalk.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use DescribeApplications.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Class to manage Elastic Beanstalk applications class ElasticBeanstalkManager def initialize(eb_client, logger: Logger.new($stdout)) @eb_client = eb_client @logger = logger end # Lists applications and their environments def list_applications @eb_client.describe_applications.applications.each do |application| log_application_details(application) list_environments(application.application_name) end rescue Aws::ElasticBeanstalk::Errors::ServiceError => e @logger.error("Elastic Beanstalk Service Error: #{e.message}") end private # Logs application details def log_application_details(application) @logger.info("Name: #{application.application_name}") @logger.info("Description: #{application.description}") end # Lists and logs details of environments for a given application def list_environments(application_name) @eb_client.describe_environments(application_name: application_name).environments.each do |env| @logger.info(" Environment: #{env.environment_name}") @logger.info(" URL: #{env.cname}") @logger.info(" Health: #{env.health}") end rescue Aws::ElasticBeanstalk::Errors::ServiceError => e @logger.error("Error listing environments for application #{application_name}: #{e.message}") end end

The following code example shows how to use ListAvailableSolutionStacks.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Manages listing of AWS Elastic Beanstalk solution stacks # @param [Aws::ElasticBeanstalk::Client] eb_client # @param [String] filter - Returns subset of results based on match # @param [Logger] logger class StackLister # Initialize with AWS Elastic Beanstalk client def initialize(eb_client, filter, logger: Logger.new($stdout)) @eb_client = eb_client @filter = filter.downcase @logger = logger end # Lists and logs Elastic Beanstalk solution stacks def list_stacks stacks = @eb_client.list_available_solution_stacks.solution_stacks orig_length = stacks.length filtered_length = 0 stacks.each do |stack| if @filter.empty? || stack.downcase.include?(@filter) @logger.info(stack) filtered_length += 1 end end log_summary(filtered_length, orig_length) rescue Aws::Errors::ServiceError => e @logger.error("Error listing solution stacks: #{e.message}") end private # Logs summary of listed stacks def log_summary(filtered_length, orig_length) if @filter.empty? @logger.info("Showed #{orig_length} stack(s)") else @logger.info("Showed #{filtered_length} stack(s) of #{orig_length}") end end end

The following code example shows how to use UpdateApplication.

SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

# Manages deployment of Rails applications to AWS Elastic Beanstalk class RailsAppDeployer def initialize(eb_client, s3_client, app_name, logger: Logger.new($stdout)) @eb_client = eb_client @s3_client = s3_client @app_name = app_name @logger = logger end # Deploys the latest application version to Elastic Beanstalk def deploy create_storage_location zip_file_name = create_zip_file upload_zip_to_s3(zip_file_name) create_and_deploy_new_application_version(zip_file_name) end private # Creates a new S3 storage location for the application def create_storage_location resp = @eb_client.create_storage_location @logger.info("Created storage location in bucket #{resp.s3_bucket}") rescue Aws::Errors::ServiceError => e @logger.error("Failed to create storage location: #{e.message}") end # Creates a ZIP file of the application using git def create_zip_file zip_file_basename = SecureRandom.urlsafe_base64 zip_file_name = "#{zip_file_basename}.zip" `git archive --format=zip -o #{zip_file_name} HEAD` zip_file_name end # Uploads the ZIP file to the S3 bucket def upload_zip_to_s3(zip_file_name) zip_contents = File.read(zip_file_name) key = "#{@app_name}/#{zip_file_name}" @s3_client.put_object(body: zip_contents, bucket: fetch_bucket_name, key: key) rescue Aws::Errors::ServiceError => e @logger.error("Failed to upload ZIP file to S3: #{e.message}") end # Fetches the S3 bucket name from Elastic Beanstalk application versions def fetch_bucket_name app_versions = @eb_client.describe_application_versions(application_name: @app_name) av = app_versions.application_versions.first av.source_bundle.s3_bucket rescue Aws::Errors::ServiceError => e @logger.error("Failed to fetch bucket name: #{e.message}") raise end # Creates a new application version and deploys it def create_and_deploy_new_application_version(zip_file_name) version_label = File.basename(zip_file_name, ".zip") @eb_client.create_application_version( process: false, application_name: @app_name, version_label: version_label, source_bundle: { s3_bucket: fetch_bucket_name, s3_key: "#{@app_name}/#{zip_file_name}" }, description: "Updated #{Time.now.strftime('%d/%m/%Y')}" ) update_environment(version_label) rescue Aws::Errors::ServiceError => e @logger.error("Failed to create or deploy application version: #{e.message}") end # Updates the environment to the new application version def update_environment(version_label) env_name = fetch_environment_name @eb_client.update_environment( environment_name: env_name, version_label: version_label ) rescue Aws::Errors::ServiceError => e @logger.error("Failed to update environment: #{e.message}") end # Fetches the environment name of the application def fetch_environment_name envs = @eb_client.describe_environments(application_name: @app_name) envs.environments.first.environment_name rescue Aws::Errors::ServiceError => e @logger.error("Failed to fetch environment name: #{e.message}") raise end end