0
votes

I'm trying to deploy a server service to AWS EC2 instance. This service is basically a consumer which runs forever.

To deploy on AWS, it seems I need to specify the following files:

  • appspec
  • beforeInstall
  • afterInstall
  • applicationStart
  • applicationStop

I'm having trouble in writing applicationStart and applicationStop scripts. My program is a java jar file and I executed it using java -jar consumer.jar. So at first I wrote applicationStart and applicationStop as follows:

ApplicationStart:

java -jar consumer.jar

ApplicationStop:

exit 0

And here's the snippet of appspec file

hooks:
  ApplicationStop:
    - location: script/deploy/stop-application.sh
      timeout: 30
      runas: root
  BeforeInstall:
    - location: script/deploy/before-install.sh
      timeout: 30
      runas: root
  AfterInstall:
    - location: script/deploy/after-install.sh
      timeout: 30
      runas: services
  ApplicationStart:
    - location: script/deploy/start-application.sh
      timeout: 30
      runas: root
  ValidateService:
    - location: script/deploy/validate-application.sh
      timeout: 30
      runas: root

When I deploy the codes to AWS, I found that the java program keeps running and the applicationStart phases finally timed out. I thought that may be because the java program won't return and thus the applicationStart doesn't finish at all. Therefore I changed the startApplication (or the script start-application.sh) to be like this

nohup java -jar consumer.jar &

This time the startApplication phase didn't time out. Instead it costs 3 mins to succeed. Why it didn't time out, given my time-out was set to be 30s? And the deployment was stuck at AllowTraffic phase and finally took 1hour to time out.

Any suggestion on how to write the applicationStart and applicationStop scripts when the program is running forever? Any suggestion will be appreciated!

1

1 Answers

0
votes

I think I find the answer finally. Basically we need to add > /dev/null 2> /dev/null < /dev/null & at the end of the commands to instruct AWS to ignore all the stderr and stdout. This way the process won't block deployment.

See References like:

  1. https://forums.aws.amazon.com/thread.jspa?threadID=181616

  2. Code Deploy ApplicationStart gets stuck on pending using node

  3. https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-long-running-processes