Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use conditional in .ebextensions config (AWS Elastic Beanstalk)

I wish I could use conditional for .ebextensions configuration, but I don't know how to use it, my current case are :

One of .ebextensions configuration content are create a folder, actually the folder that must be created it's only once, because if I'm deploying app for second times or more I've got error, and the error said "the folder already exist".

So I need to give conditional, if the folder already exist it's not necessary to run again the command for create a folder.

If anyone has any insight or direction on how this can be achieved, I would greatly appreciate it. Thank you!

like image 321
Khalid Avatar asked Jun 18 '13 08:06

Khalid


People also ask

What is .ebextensions in AWS Elastic Beanstalk?

You can add AWS Elastic Beanstalk configuration files ( . ebextensions ) to your web application's source code to configure your environment and customize the AWS resources that it contains. Configuration files are YAML- or JSON-formatted documents with a . config file extension that you place in a folder named .

Which of the following can be used to customize your Elastic Beanstalk environment?

Customize your Elastic Beanstalk environment Use the option_settings key to modify the environment configuration. You can choose from general options for all environments and platform-specific options.

What two types of environments can be created when using Elastic Beanstalk?

In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment. The type of environment that you require depends on the application that you deploy.


2 Answers

The .ebextensions config files allow conditional command execution by using the test: directive on a command. Then the command only runs if the test is true (returns 0).

Example .ebextensions/create_dir.config file:

commands:
  01_create_dir:
    test: test ! -d "${DIR}"
    command: mkdir "${DIR}"

Another example (actually tested on EB) to conditionally run a script if a directory is not there:

commands:
  01_intall_foo:
    test: test ! -d /home/ec2-user/foo
    command: "/home/ec2-user/install-foo.sh"
    cwd: "/home/ec2-user/"

The sparse documentation from AWS is here:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-commands

PS. If you just need to conditionally create the directory, you can do it without conditionals, using the -p option for mkdir to conditionally create the directory like so:

commands:
  01_create_dir:
    command: mkdir -p "${DIR}"
like image 115
mjul Avatar answered Oct 14 '22 11:10

mjul


I think that the only way to do it is with shell conditions:

commands:
  make-directory:
    command: |
      if [ ! -f "${DIR}" ]; then
        mkdir "${DIR}"
      fi

See bigger example in jcabi-beanstalk-maven-plugin.

like image 33
yegor256 Avatar answered Oct 14 '22 12:10

yegor256