set -e -o pipefail

FUNCTION_NAME=$1
IMAGE_URI=437795906767.dkr.ecr.us-east-1.amazonaws.com/lambda-sanitise-rds-data:"$GIT_COMMIT"

echo Publishing new function version for "$FUNCTION_NAME" using image "$IMAGE_URI"
VERSION=$(aws lambda update-function-code \
              --region us-east-1 \
              --function-name "$FUNCTION_NAME" \
              --image-uri "$IMAGE_URI" \
              --publish | jq -r .Version)

echo Published function version "$VERSION". Creating alias "$GIT_COMMIT"
aws lambda create-alias \
    --region us-east-1 \
    --function-name "$FUNCTION_NAME" \
    --name "$GIT_COMMIT" \
    --function-version "$VERSION"

echo Successfully created alias
