AWS Elastic Beanstalk部署失败错误排查与解决求助
AWS Elastic Beanstalk + CodePipeline 部署失败排查
错误信息
Deployment completed, but with errors: During an aborted deployment, some instances may have deployed the new application version. To ensure all instances are running the same version, re-deploy the appropriate application version. Failed to deploy application. Unsuccessful command execution on instance id(s) 'i-0aa5817c1885fa11e'. Aborting the operation.
环境配置
- 平台:AWS Elastic Beanstalk
- 实例类型:Amazon Linux 2
- Python版本:3.11
部署相关脚本
buildspec.yml
version: 0.2 phases: install: runtime-versions: python: 3.11 commands: - echo Installing dependencies... - pip install --upgrade pip - pip install -r requirements.txt pre_build: commands: - echo Pre-build phase... build: commands: - echo Build phase... post_build: commands: - echo Post-build phase... artifacts: files: - '**/*' base-directory: .
.ebextensions/install_firefox.config
packages: yum: python3-devel: [] libX11-devel: [] libXtst-devel: [] libXScrnSaver: [] alsa-lib: [] xorg-x11-server-Xvfb: [] libxkbcommon-x11: [] at-spi2-core: [] google-noto-cjk-fonts-common: [] google-noto-sans-cjk-ttc-fonts: [] commands: 01_install_dependencies: command: yum install -y wget bzip2 02_cleanup_previous_files: command: | rm -f /opt/firefox.tar.bz2 rm -f /opt/geckodriver.tar.gz 03_download_firefox: command: | wget -O /opt/firefox.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US" tar -xjf /opt/firefox.tar.bz2 -C /opt/ chmod +x /opt/firefox/firefox ln -sf /opt/firefox/firefox /usr/bin/firefox 04_download_geckodriver: command: | wget https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-linux64.tar.gz -O /opt/geckodriver.tar.gz tar -xzf /opt/geckodriver.tar.gz -C /opt/ mv /opt/geckodriver /usr/local/bin/geckodriver chmod +x /usr/local/bin/geckodriver ln -sf /usr/local/bin/geckodriver /usr/bin/geckodriver 05_install_firefox: command: yum install -y xorg-x11-server-Xvfb 06_configure_xvfb: command: | Xvfb :99 -screen 0 1920x1080x24 & export DISPLAY=:99 07_configure_firefox_options: command: | mkdir -p /etc/firefox echo "FIREFOX_ARGS=--headless" >> /etc/firefox/default
Selenium辅助脚本
import time from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.firefox.options import Options from selenium.common.exceptions import TimeoutException from selenium.webdriver.firefox.service import Service from selenium.common.exceptions import WebDriverException def init_driver(): try: options = webdriver.FirefoxOptions() options.headless = True options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') driver_path = '/usr/local/bin/geckodriver' service = Service(driver_path) driver = webdriver.Firefox(service=service, options=options) return driver except WebDriverException as e: print(f"WebDriverException occurred...: {e}") return None except Exception as e: print(f"Error occurred...: {e}") return None def get_full_page_html(driver, base_url): if driver is None: return None driver.get(base_url) try: WebDriverWait(driver, 10).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, "body")) ) print(f"Successfully navigated to {base_url}") except TimeoutException as error: print(f"Error occurred while waiting for the page to load: {error}") return None except WebDriverException as error: print(f"WebDriverException occurred while loading the page: {error}") return None time.sleep(2) page_source = driver.page_source return page_source
已尝试操作
- 确保脚本幂等性,添加清理命令在下载前移除旧文件
- 删除所有Elastic Beanstalk应用版本,重建EB应用
- 部署基础Flask示例应用(成功)
- 检查CloudWatch及实例日志,未发现具体错误原因
- 更换ZIP文件名重新部署,无效
问题始于引入Selenium之后,此前部署正常。
排查与解决方案
1. 定位具体错误日志
EB通用错误提示不够详细,必须查看实例级部署日志:
- 登录故障实例
i-0aa5817c1885fa11e,直接查看/var/log/eb-engine.log和/var/log/cfn-init.log,这两个日志会记录.ebextensions脚本执行的每一步错误细节。 - 或通过EB控制台的「日志」选项下载完整实例日志包,重点排查
eb-engine.log中commands阶段的失败信息。
2. 修复.ebextensions脚本的核心问题
(1)移除重复的Xvfb安装
packages段已声明安装xorg-x11-server-Xvfb,但05_install_firefox命令重复执行安装操作,可能导致冗余冲突。直接删除05_install_firefox命令。
(2)持久化Xvfb进程
当前06_configure_xvfb启动的Xvfb仅在部署会话中有效,会话结束后进程会终止,后续应用启动时无法找到DISPLAY变量。需将Xvfb配置为系统服务:
在.ebextensions/install_firefox.config末尾添加:
services: sysvinit: xvfb: enabled: true ensureRunning: true commands: start env: DISPLAY: ":99"
同时修改06_configure_xvfb命令为:
06_configure_xvfb: command: | echo "DISPLAY=:99" >> /etc/environment
这样系统启动时会自动拉起Xvfb,且DISPLAY变量会全局生效。
(3)修复Firefox与Geckodriver版本兼容
你使用最新Firefox但搭配v0.34.0的Geckodriver,存在版本不兼容风险。建议:
- 固定Firefox版本(改用具体版本号的下载链接,避免每次部署下载不同版本)
- 匹配Geckodriver与Firefox的版本(参考官方兼容性对应关系)
(4)添加下载重试机制
wget下载可能因网络波动失败,给下载命令增加重试逻辑:
修改03_download_firefox命令:
03_download_firefox: command: | retry=3 while [ $retry -gt 0 ]; do wget -O /opt/firefox.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=en-US" && break retry=$((retry-1)) sleep 5 done if [ $retry -eq 0 ]; then exit 1; fi tar -xjf /opt/firefox.tar.bz2 -C /opt/ chmod +x /opt/firefox/firefox ln -sf /opt/firefox/firefox /usr/bin/firefox
同理修改04_download_geckodriver命令。
3. 调整CodePipeline构建逻辑
buildspec.yml中pip install -r requirements.txt仅在构建环境生效,EB部署时会重新安装依赖。建议:
- 在
.ebextensions中添加依赖安装命令,确保部署环境与构建环境一致 - 确认
requirements.txt中明确声明selenium及对应版本
4. 逐步验证部署
先部署不含Selenium的基础应用,确认正常后再逐步添加.ebextensions脚本和Selenium代码,每次仅修改一个环节,快速定位问题点。
内容的提问来源于stack exchange,提问作者Charuka Tharindhu
相关产品推荐
相关产品推荐

