Beanstalk on Windows: How to Prevent Command Execution on Redeployment?

I am trying to use the AWS Elastic Beanstalk tool to configure the EC2 instances that it creates. To do this, create a configuration file . Config in the .ebextensions directory .

You can specify several commands that should be executed when deploying the application to an instance. I use this to install some msi files, and also configure EC2 to give the instance a unique name. This requires a reboot.

My problem is that I want these commands to be run only when I first deploy the instance. When I deploy code-only changes to existing instances, they should not be triggered.

I tried using the "test" parameter, which should prevent the command from starting. I create the file as the last command, and then check for the presence of this file in the "test" parameter. But it doesn't seem to work.

My configuration file looks like this:

# File structure documented at http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-windows-ec2.html files: "C:\\Users\\Public\\EnableEc2SetComputerName.ps1": source: "[File Source]" commands: init-01-ec2setcomputername-enable: test: cmd /c "if exist C:\\Users\\Public\\initialised (exit 1) else (exit 0)" command: powershell.exe -ExecutionPolicy Bypass -File "C:\\Users\\Public\\EnableEc2SetComputerName.ps1" waitAfterCompletion: 0 init-05-reboot-instance: test: cmd /c "if exist C:\\Users\\Public\\initialised (exit 1) else (exit 0)" command: shutdown -r # restart to enable EC2 to set the computer name waitAfterCompletion: forever init-06-mark-initialised: test: cmd /c "if exist C:\\Users\\Public\\initialised (exit 1) else (exit 0)" command: echo initialised > C:\\Users\\Public\\initialised waitAfterCompletion: 0 

Is there an alternative way to do this? Or am I doing something stupid?

On Unix-based systems, there are touch and test commands (mentioned in this answer asking an equivalent question for Unix systems). Which Windows equivalent works best in this situation?

+6
source share
3 answers

Essentially not. Elastic Beanstalk is an abstraction and takes care of the underlying infrastructure for you. You give up a lot of control over the environment and get an easier deployment. If you explore CloudFormation - specifically metadata and cfn-init / cfn-hup, you'll see a very similar construct around beanstalk files and commands http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource- init.html

If you need to configure the instance outside of the application settings, then you are probably using the wrong tool and should set up clumsy workarounds (until the AWS Touch / test arrives). Cloud Formation scripts are likely to be better suited.

I wrote about how to set up Windows instances through cloudformation , as well as extensive documentation on Amazon itself .

Given that you have done all the hard work with the teams, I think it would be easy to switch to the Cloud Formation script and use the launch code once in the user data.

** edit - I think you could do it like this, although if you went with the elastic beanstalk command: dir initialised || powershell.exe -ExecutionPolicy Bypass -File "C:\\Users\\Public\\EnableEc2SetComputerName.ps1" command: dir initialised || powershell.exe -ExecutionPolicy Bypass -File "C:\\Users\\Public\\EnableEc2SetComputerName.ps1"

+3
source

I think the problem is that you reboot the machine before you can write the initialized file. You should be able to use the bat file, which first writes the semaphore, then reloads the instance and runs this .bat file, depending on the existence of the semaphore.

You can download the .bat file using the files:source: directive or compile it in .config with the files:content: directive.

Otherwise, your test: lines look good (I tested them locally, without rebooting).

+3
source

I recently ran into very similar problems and used the answer from Jim Flanagan above and created a PowerShell script for this.

 # restarts the server if this is not the first deployment param ( ) $strFileName="C:\Users\Public\fwinitialised.txt" If (Test-Path $strFileName){ #This file was created on a previous deployment, reboot now. Restart-Computer -Force }Else{ # this is a new instance, no need to reboot. New-Item $strFileName -type file } 

And in the .ebextensions file ...

 6-reboot-instance: command: powershell.exe -ExecutionPolicy Bypass -File "C:\\PERQ\\Deployment\\RestartServerOnRedeployment.ps1" waitAfterCompletion: forever 
+1
source

Source: https://habr.com/ru/post/952843/


All Articles