Gunakan UpdateApplication dengan AWS SDK atau CLI - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan UpdateApplication dengan AWS SDK atau CLI

Contoh kode berikut menunjukkan cara menggunakanUpdateApplication.

CLI
AWS CLI

Untuk mengubah deskripsi aplikasi

Perintah berikut memperbarui deskripsi aplikasi bernamamy-app:

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

Output:

{ "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
SDKuntuk Ruby
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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