Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
0 contributors

Users who have contributed to this file

114 lines (104 sloc) 4.97 KB
###################################################################################################
#### Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
####
#### Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
#### except in compliance with the License. A copy of the License is located at
####
#### http://aws.amazon.com/apache2.0/
####
#### or in the "license" file accompanying this file. This file is distributed on an "AS IS"
#### BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#### License for the specific language governing permissions and limitations under the License.
###################################################################################################
###################################################################################################
#### This configuration file allows a cron job to run only on one Linux instance in the environment.
####
#### The script "/usr/local/bin/test_cron.sh" will sort and compare the current instances in the
#### Auto Scaling group and if it matches the first instance in the sorted list it will exit 0.
#### This will mean that this script will only exit 0 for one of the instances in your environment.
####
#### The second script is an example of how you might use the "/usr/local/bin/test_cron.sh" script
#### to execute commands and log a timestamp to "/tmp/cron_example.log".
####
#### A cron example is setup at "/etc/cron.d/cron_example" to execute the script
#### "/usr/local/bin/cron_example.sh" every minute. A command is also run upon each deployment to
#### clear any previous versions of "/etc/cron.d/cron_example" by removing
#### "/etc/cron.d/cron_example.bak".
####
#### Note that for the first script to gather the required information, additional IAM permissions
#### will be needed to be added to a policy attached to the instance profile used by the instances
#### in the environment. The policy shown below will grant the access needed. Note that the default
#### instance profile for Elastic Beanstalk is "aws-elasticbeanstalk-ec2-role".
####
#### How to create a New Policy
#### - https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html
####
#### Adding Permissions to the Default Instance Profile
#### - https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/iam-instanceprofile.html#iam-instanceprofile-addperms
####
#### NB: Please note that as the "/usr/local/bin/test_cron.sh" script makes use of the EC2 health
#### status to determine "InService" instances in the Auto Scaling Group, the cron could fail to
#### run if the "leader" instance has failed and Auto Scaling has not yet changed the EC2 instance
#### state.
###################################################################################################
####
#### {
#### "Version": "2012-10-17",
#### "Statement": [
#### {
#### "Sid": "Stmt1409855610000",
#### "Effect": "Allow",
#### "Action": [ "autoscaling:DescribeAutoScalingGroups" ],
#### "Resource": [ "*" ]
#### },
#### {
#### "Sid": "Stmt1409855649000",
#### "Effect": "Allow",
#### "Action": [ "ec2:DescribeTags" ],
#### "Resource": [ "*" ]
#### }
#### ]
#### }
####
###################################################################################################
###################################################################################################
packages:
yum:
jq: []
files:
"/usr/local/bin/test_cron.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id 2>/dev/null`
REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document 2>/dev/null | jq -r .region`
# Find the Auto Scaling Group name from the Elastic Beanstalk environment
ASG=`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" \
--region $REGION --output json | jq -r '.[][] | select(.Key=="aws:autoscaling:groupName") | .Value'`
# Find the first instance in the Auto Scaling Group
FIRST=`aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $ASG \
--region $REGION --output json | \
jq -r '.AutoScalingGroups[].Instances[] | select(.LifecycleState=="InService") | .InstanceId' | sort | head -1`
# If the instance ids are the same exit 0
[ "$FIRST" = "$INSTANCE_ID" ]
"/usr/local/bin/cron_example.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
/usr/local/bin/test_cron.sh || exit
# Now run commands that should run on only 1 instance.
echo "Cron running at "`date` > /tmp/cron_example.log
"/etc/cron.d/cron_example":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/local/bin/cron_example.sh
commands:
rm_old_cron:
command: "rm -fr /etc/cron.d/cron_example.bak"
ignoreErrors: true
You can’t perform that action at this time.