Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beanstalk on Windows: How do I prevent commands running on re-deployment?

I'm trying to take advantage of AWS Elastic Beanstalk's facility to customize the EC2 instances it creates. This requires creating a .config file in the .ebextensions directory.

You can specify a number of commands which should be executed when the application is deployed to an instance. I'm using that to install some msi files, and also to configure EC2 to assign the instance a unique name. This then requires a reboot.

My problem is that I only want these commands to be run when an instance is first deployed. When I deploy a code-only change to existing instances they shouldn't be run.

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

My config file is 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 accomplish this? Or am I doing something stupid?

On Unix-based systems, there are the touch and test commands (referred to in this answer asking the equivalent question for Unix systems). What's the equivalent in Windows which will work best in this situation?

like image 779
Samuel Jack Avatar asked Aug 30 '13 15:08

Samuel Jack


People also ask

When should you not use Elastic Beanstalk?

Elastic Beanstalk isn't great if you need a lot of environment variables. The simple reason is that Elastic Beanstalk has a hard limit of 4KB to store all key-value pairs. The environment had accumulated 74 environment variables — a few of them had exceedingly verbose names.

What are the Elastic Beanstalk deployment modes?

AWS Elastic Beanstalk provides several options for how deployments are processed, including deployment policies (All at once, Rolling, Rolling with additional batch, Immutable, and Traffic splitting) and options that let you configure batch size and health check behavior during deployments.

What is rolling deployment in Elastic Beanstalk?

Rolling deployments occur whenever you deploy your application and can typically be performed without replacing instances in your environment. Elastic Beanstalk takes each batch out of service, deploys the new application version, and then places it back in service.


1 Answers

I think the problem lies in the fact that you are rebooting the machine before you can write the initialized file. You should be able use a bat file which first writes the semaphore, then reboots the instance, and run that .bat file contingently on the existence of semaphore.

You can either download the .bat file with a files:source: directive or compose it in the .config with a files:content: directive.

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

like image 161
Jim Flanagan Avatar answered Sep 19 '22 04:09

Jim Flanagan