使用.ebextensions替换Elastic Beanstalk中Tomcat的server.xml失败求助
操作流程
- 在源码包根目录创建
.ebextensions文件夹 - 放入自定义
server.xml和server.config配置文件,server.config内容如下:
container_commands: replace-config: command: cp .ebextensions/server.xml /usr/share/tomcat/conf/server.xml
报错信息
2023-07-31 16:47:28,043 [ERROR] -----------------------BUILD
FAILED!------------------------ 2023-07-31 16:47:28,043 [ERROR]
Unhandled exception during build: Command replace-config failed
Traceback (most recent call last): File "/opt/aws/bin/cfn-init",
line 176, in
worklog.build(metadata, configSets) File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line
137, in build
Contractor(metadata).build(configSets, self) File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line
564, in build
self.run_config(config, worklog) File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line
576, in run_config
CloudFormationCarpenter(config, self._auth_config).build(worklog) File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py",
line 276, in build
self._config.commands) File "/usr/lib/python3.7/site-packages/cfnbootstrap/command_tool.py", line
127, in apply
raise ToolError(u"Command %s failed" % name)
已尝试操作与权限信息
- 在
cp命令前添加sudo,问题未解决 - 目标文件
server.xml权限:
-rw-r--r-- 1 root tomcat 8611 Jul 19 20:18 server.xml
解决方案建议
1. 确认Tomcat配置文件路径
不同Elastic Beanstalk的Tomcat环境,配置文件路径可能不同。可先添加命令验证路径:
container_commands: check-tomcat-path: command: ls -la /usr/share/tomcat/conf/ || ls -la /usr/share/tomcat9/conf/ || ls -la /var/lib/tomcat/conf/
2. 使用files指令替代拷贝命令
Elastic Beanstalk官方更推荐用files指令直接部署配置文件,避免手动拷贝的路径/权限问题:
files: "/usr/share/tomcat/conf/server.xml": mode: "0644" owner: root group: tomcat # 方式1:从S3拉取文件 source: https://s3.amazonaws.com/your-bucket/path/to/server.xml # 方式2:直接写入文件内容 # content: | # [此处粘贴你的server.xml完整内容]
3. 完善container_commands的执行逻辑
添加文件存在校验,确保拷贝命令只在文件存在时执行:
container_commands: replace-config: command: sudo /bin/cp .ebextensions/server.xml /usr/share/tomcat/conf/server.xml test: '[ -f .ebextensions/server.xml ]'
同时使用cp命令的绝对路径,避免环境变量导致的命令找不到问题。
4. 查看详细日志定位问题
登录EC2实例,查看以下日志文件获取更具体的错误原因:
/var/log/cfn-init.log/var/log/eb-engine.log
内容的提问来源于stack exchange,提问作者Victor Soares

