I know I can put commands in my source code in .ebextensions/*.config using the commands
array. These are executed on every deploy however. What about if I want to execute a configuration command only once when spinning up a new instance?
Asked
Active
Viewed 4,392 times
20

Gabe Kopley
- 16,281
- 5
- 47
- 60
2 Answers
39
Commands can be run conditionally using the test:
modifier. You specify a test to be done. If the test returns 0, the command is run, otherwise it is not.
If the last command in your config file touches a file, and the commands above that you only want to run once check for the existence of that file, then those commands will only run the first time.
commands:
01-do-always:
command: run_my_script
02-do-on-boot:
command: script_to_run_once
test: test ! -f .semaphore
99-signal-startup-complete:
command: touch .semaphore
On Windows it would be something like this
commands:
01-do-always:
command: run_my_script
02-do-on-boot:
command: script_to_run_once
test: if exists c:\\path\\to\\semaphore.txt (exit 0) else (exit 1)
99-signal-startup-complete:
command: date > c:\\path\\to\\semaphore.txt

Jim Flanagan
- 2,129
- 15
- 19
-
How would you do the same thing but on a windows server? How does the syntax differ? Thanks. – SSED Jun 29 '16 at 22:52
-
Updated the post with a Windows example. – Jim Flanagan Jul 05 '16 at 23:02
-
So I guess this applies to the `commands` section, whereas for `container_commands` you could use the `leader-only` option instead? Or did I misinterpret the question? – djvg Sep 17 '18 at 13:52
-
@djvg `leader-only` only executes the command on 1 instance within a target group. The question here is about executing a command on every instance but only once. – Manuel May 03 '20 at 03:38
-
`.semaphore` didn't work for me, I think the current directly is the temporary deployment directory which is erased on every deployment. It worked with a different directory, I decided to use `/var/elasticbeanstalk/...`. – Manuel May 03 '20 at 15:16
0
On Windows this should work:
commands:
01-do-always:
command: run_my_script
02-do-on-boot:
command: script_to_run_once
test: cmd /c "if exist c:\\semaphore.txt (exit 1) else (exit 0)"
99-signal-startup-complete:
command: echo %date% %time% > c:\\semaphore.txt
Note that I had to change the test
command from Jim Flanagan's answer.

jmosbech
- 1,128
- 9
- 8