4

I'm trying to deploy my Node.js application to AWS ElasticBeanstalk with AWS CodePipeline. Since my application is using WebSockets, I included the following config file in .ebextensions.

container_commands:
  enable_websocket:
    command: 
      sed -i '/\s*proxy_set_header\s*Connection/c \
              proxy_set_header Upgrade $http_upgrade;\
              proxy_set_header Connection "upgrade";\
          ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

However, the CodePipeline fails on the deployment process and the ElasticBeanstalk log file cfn-init.log says follows:

2020-06-18 12:43:42,345 [INFO] -----------------------Starting build-----------------------
2020-06-18 12:43:42,352 [INFO] Running configSets: Infra-EmbeddedPostBuild
2020-06-18 12:43:42,355 [INFO] Running configSet Infra-EmbeddedPostBuild
2020-06-18 12:43:42,359 [INFO] Running config postbuild_0_drcha
2020-06-18 12:43:42,381 [ERROR] Command enable_websockets (sed -i '/\s*proxy_set_header\s*Connection/c \
        proxy_set_header Upgrade $http_upgrade;\
        proxy_set_header Connection "upgrade";\
        ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf) failed
2020-06-18 12:43:42,381 [ERROR] Error encountered during build of postbuild_0_drcha: Command enable_websockets failed
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command enable_websockets failed
2020-06-18 12:43:42,383 [ERROR] -----------------------BUILD FAILED!------------------------
2020-06-18 12:43:42,383 [ERROR] Unhandled exception during build: Command enable_websockets failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 171, in <module>
    worklog.build(metadata, configSets)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 129, in build
    Contractor(metadata).build(configSets, self)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 530, in build
    self.run_config(config, worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command enable_websockets failed

What is the reason for the failure?

김승수
  • 171
  • 2
  • 9

3 Answers3

9

The secret here is that the full logs and tracebacks from container_commands are in /var/log/cfn-init-cmd.log (on Amazon Linux 2 Elastic Beanstalk released November 2020). To read this you would run:

eb ssh [environment-name]
sudo tail -n 50 -f /var/log/cfn-init-cmd.log

This doesn't seem to be documented anywhere obvious and it's not displayed when you run eb logs. I only found it by hunting around in /var/log.

For readers looking to run Django management commands, see Running Django migrations when deploying to Elastic Beanstalk

Ben Sturmfels
  • 1,303
  • 13
  • 22
5

I had this exact same issue and came across the same configuration that also didn't work. After further sleuthing, I found the correct configuration (at least as of now).

files:
  "/etc/nginx/conf.d/websockets.conf":
    content: |

      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";

I posted a blog article that might be helpful if you need help on additional configuration.

gmolaire
  • 1,091
  • 10
  • 19
  • 1
    This saved me. I ran into this problem after moving from `Node.js running on 64bit Amazon Linux` to `64bit Amazon Linux 2 v5.2.4 running Node.js 12`. I was using the old way to setup nginx with websockets. Thank you for sharing your solution. – Chuck Taylor Jan 19 '21 at 14:39
2

I am not exactly sure why the error happens, but if you are using the Amazon Linux 2 platform (launched for Node.js on April 30th, 2020) then you can customize the nginx config more easily.

As described in Extending Elastic Beanstalk Linux platforms, you can put a custom nginx file at .platform/nginx/nginx.conf and the deployment will automatically use it. You may want to inspect the default config and base it on that.

stefansundin
  • 2,826
  • 1
  • 20
  • 28