#!/usr/bin/env bash
set -veExo pipefail
aws_meta_data () {
curl -sSL "http://169.254.169.254/latest/meta-data/$*"
}
aws_region () {
declare az=""
az="$(aws_meta_data placement/availability-zone)"
echo "$${az%[a-z]}"
}
aws_instance_id () {
aws_meta_data instance-id
}
aws_ssm_get_parameter () {
declare region=""; region="$(aws_region)"
aws --region "$region" ssm get-parameter --with-decryption --name "$*"
}
aws_ec2_get_tags () {
declare instance_id="$1"; shift
aws ec2 describe-tags --filters "Name=resource-id,Values=$instance_id" "$@"
}
main () {
declare password="" region="" function="${function}" instance_id=""
instance_id="$(aws_instance_id)"
if [[ -z "$${function:-}" ]]
then
if ! function="$(aws_meta_data iam/security-credentials)" || [[ -z "$function" ]]
then
yum install -y jq
function="$(aws_ec2_get_tags "$instance_id" | jq -r '.Tags[]|select(.Key=="function")|.Value')"
fi
fi
jq -r ".+{function: \\"$function\\"}" /etc/myconfig.json | tee -a /etc/myconfig.json.new
mv -v /etc/myconfig.json.new /etc/myconfig.json
if password="$(aws_ssm_get_parameter "/myconfig/$function/instance/password/ec2-user/encrypted")"
then
chpasswd -e <<< "ec2-user:$password"
else
chpasswd <<< "ec2-user:$function$instance_id"
fi
sed -i r 's/^(PasswordAuthentication)[[:space:]]+.*/\1 yes/' /etc/ssh/sshd_config
service sshd restart
}