与 AWS SDK或UpdateApplication一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或UpdateApplication一起使用 CLI

以下代码示例演示如何使用 UpdateApplication

CLI
AWS CLI

更改应用程序的描述

以下命令更新名为的应用程序的描述my-app

aws elasticbeanstalk update-application --application-name my-app --description "my Elastic Beanstalk application"

输出:

{ "Application": { "ApplicationName": "my-app", "Description": "my Elastic Beanstalk application", "Versions": [ "2fba-stage-150819_234450", "bf07-stage-150820_214945", "93f8", "fd7c-stage-150820_000431", "22a0-stage-150819_185942" ], "DateCreated": "2015-08-13T19:15:50.449Z", "ConfigurationTemplates": [], "DateUpdated": "2015-08-20T22:34:56.195Z" } }
Ruby
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” 中的。