#!/bin/bash

# The following IAM roles are required:
#   AmazonS3ReadOnlyAccess

if [ ! -d ~magento/.ssh/ ];
then
  mkdir ~magento/.ssh/
  chown magento:magento ~magento/.ssh/
fi

while [ -z ${keybucket} ]
do
  keybucket=`/usr/local/bin/aws s3 ls | grep lsynckey\\-rax\\- | awk '{print $3}'`
  sleep 5
done

if [ ! -z ${keybucket} ]
then
  /usr/local/bin/aws s3 cp s3://${keybucket}/magento-admin.pub `getent passwd magento | cut -d: -f6`/.ssh/
  chmod 600 ~magento/.ssh/magento-admin
  chown magento:magento ~magento/.ssh/magento-admin.pub
fi

#!/bin/bash

# The following IAM roles are required:
#   AmazonS3ReadOnlyAccess
#   AmazonEC2ReadOnlyAccess

if [ ! -d ~magento/.ssh/ ];
then
  mkdir ~magento/.ssh/
  chown magento:magento ~magento/.ssh/
fi

region=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/ | sed '$ s/.$//'`
uuid=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
keybucket=`/usr/local/bin/aws ec2 describe-tags --region ${region} --filters "Name=resource-id,Values=${uuid}" "Name=key,Values=rackuuid" --query 'Tags[*].Value[]' --output text`-lsynckey

while [ -z ${bucketexist} ]
do
  bucketexist=`/usr/local/bin/aws s3 ls | grep ${keybucket}`
  sleep 5
done

if [ ! -z ${keybucket} ]
then
  /usr/local/bin/aws s3 cp s3://${keybucket}/magento-admin.pub `getent passwd magento | cut -d: -f6`/.ssh/
  chmod 600 ~magento/.ssh/magento-admin
  chown magento:magento ~magento/.ssh/magento-admin.pub
fi

Modify how to search for bucket
Modify how to search for bucket