#!/usr/bin/env bash
set -e

AWS_CONFIG_FILE=.tmp-aws-config
AWS_PROFILE='tmp-profile'

# I've tried using env vars to configure the cli, but it complains
# everytime. The only way I can get it to work is to write a file
# and use AWS_CONFIG_FILE to point at it. The keys here used to
# create the local db must match the keys used at runtime to connect.
echo "[profile $AWS_PROFILE]" > $AWS_CONFIG_FILE
echo "aws_access_key_id=$AWS_KEY" >> $AWS_CONFIG_FILE
echo "aws_secret_access_key=$AWS_SECRET" >> $AWS_CONFIG_FILE
echo "region=us-east-1" >> $AWS_CONFIG_FILE

AWS_CLI_ARGS="--profile=$AWS_PROFILE --region=us-east-1"

# AWS_ENDPOINT is used to change to point to localstack instead of remote aws
if [ -n "$AWS_ENDPOINT" ]; then
  AWS_CLI_ARGS+=" --endpoint-url=$AWS_ENDPOINT"
fi;

export AWS_CONFIG_FILE;

echo "aws $@ $AWS_CLI_ARGS"

aws $@ $AWS_CLI_ARGS

# delete tmp file
rm $AWS_CONFIG_FILE
