AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
用于 Ruby 的 Elastic Beanstal SDK k 示例
以下代码示例向您展示了如何在 Elastic Beanstalk 中 AWS SDK for Ruby 使用来执行操作和实现常见场景。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示如何使用 DescribeApplications
。
- SDK对于 Ruby
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 # 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
-
有关API详细信息,请参阅 “AWS SDK for Ruby API参考 DescribeApplications” 中的。
-
以下代码示例演示如何使用 ListAvailableSolutionStacks
。
- SDK对于 Ruby
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 # 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
-
有关API详细信息,请参阅 “AWS SDK for Ruby API参考 ListAvailableSolutionStacks” 中的。
-
以下代码示例演示如何使用 UpdateApplication
。
- SDK对于 Ruby
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 # 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
-
有关API详细信息,请参阅 “AWS SDK for Ruby API参考 UpdateApplication” 中的。
-