2

I want to add one condition in cron.config file. I need to check ENV_ID (environment id) if the environment ID is a match with production server then cron will set in crontab else cron will not set check.

cron.config

container_commands:
  01_remove_crontab:
    command: "crontab -r || exit 0"
  02_test:
    command: |
      ENV_ID=`{"Ref": "AWSEBEnvironmentId" }`
      ENV_NAME=`{"Ref": "AWSEBEnvironmentName" }`
  03_add_crontab:
    test:  [ $ENV_ID == "e-r19pphhp78l" ]
    command: "cat .ebextensions/crontab | crontab"
    leader_only: true

crontab

* * * * * wget https://example.com/cronrun.php >> /dev/null

Also, I check if condition but now working.

container_commands:
  01_remove_crontab:
    command: "crontab -r || exit 0"
  02_test:
    command: |
      ENV_ID=`{"Ref": "AWSEBEnvironmentId" }`
      ENV_NAME=`{"Ref": "AWSEBEnvironmentName" }`
      ENV_MYID="e-r19pphhp78l"
  03_add_crontab:
    command: |
      if [ $ENV_ID == $ENV_MYID ] then
        "cat .ebextensions/crontab | crontab"
      fi
    leader_only: true

I am not able to found what missing and what's the wrong script.

Praveen Kumar
  • 864
  • 2
  • 9
  • 33

2 Answers2

4

I found the solution to this problem. I set all environments value in Elastic Beanstalk. Below is setup

AWS console =>Open Elastic Beanstalk. Select the application and go to one of environment. There we can see all the environments names, in that we need go inside the Configuration", after that go to the software section and click on modify. Look for environment specific ENV_TYPE values, which specifies whether it is a dev, stage, demo or prod, etc. The example below uses ENV_TYPE for the explanation. You can create your variable as you need and name it.

Now we need to open cron.config file inside .ebextensions folder and add condition.

container_commands:
  01_remove_crontab:
    command: "crontab -r || exit 0"
  02_add_crontab:
    command: 'if [ "$ENV_TYPE" == "production" ]; then sudo cat .ebextensions/crontab | crontab; fi'
    command: 'if [ "$ENV_TYPE" == "development" ]; then cat .ebextensions/crontabdev | crontab; fi'
    command: 'if [ "$ENV_TYPE" == "staging" ]; then cat .ebextensions/crontabstag | crontab; fi'
    leader_only: true

For more details please check this link:- http://www.smalldaytech.com/conditional-aws-elastic-beanstack-cron-job-for-multiple-environments/

Krupa Suthar
  • 670
  • 3
  • 14
Praveen Kumar
  • 864
  • 2
  • 9
  • 33
0

You cannot use both test and leader_only simultaneously in container_commands, and if you do the leader_only will take precedence.

elastic beanstalk docs on container_commands

A command can be leader-only or have a test, but not both (leader_only takes precedence).

danimal
  • 1,567
  • 13
  • 16