#!/bin/bash

# This is set by default when running in Fargate
if [ -z "${AWS_REGION}" ]; then
  AWS_REGION='us-east-1'
fi

DD_API_KEY=$(aws secretsmanager get-secret-value --secret-id "${Environment}/datadog/DD_API_KEY" --region "${AWS_REGION}" --query 'SecretString' --output text)

# Check for Datadog API key, and replace fluent plugin config file with API key
if [ -z "${DD_API_KEY}" ]; then
  echo "Could not retrieve Datadog API key" && exit 1
else
  sed -i "s/REPLACE_ME/${DD_API_KEY}/g" /fluentd/etc/conf.d/copy_forward.conf
  exec su fluent -c "fluentd -v --config /fluentd/etc/${FLUENTD_CONF} --plugin /fluentd/plugins"
fi
