import json
import os
import subprocess

from dotenv import load_dotenv
from subprocess import check_output, Popen, PIPE

load_dotenv()

# Accessing variables.
CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_SECRET = os.environ.get('CLIENT_SECRET')
USERNAME = os.environ.get('USERNAME')
BUCKET_NAME = os.environ.get('BUCKET_NAME')


def get_lambda_functions():
    function_dict = {}
    res = subprocess.Popen(
        ["aws", "lambda", "list-functions"],
        stdout=subprocess.PIPE
    )
    output = res.communicate()
    function_dict.update(json.loads(output[0]))

    return function_dict['Functions']


lambda_functions = get_lambda_functions()
for lambda_function in lambda_functions:
    function_name = lambda_function['FunctionName']

    subprocess.run([
        "aws", "lambda", "update-function-configuration",
        "--function-name", f"{function_name}", "--environment",
        f"Variables={{CLIENT_ID={CLIENT_ID},CLIENT_SECRET={CLIENT_SECRET},USERNAME={USERNAME},BUCKET_NAME={BUCKET_NAME}}}"
    ])

[((122, 135), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (133, 135), False, 'from dotenv import load_dotenv\n'), ((172, 199), 'os.environ.get', 'os.environ.get', (['"""CLIENT_ID"""'], {}), "('CLIENT_ID')\n", (186, 199), False, 'import os\n'), ((216, 247), 'os.environ.get', 'os.environ.get', (['"""CLIENT_SECRET"""'], {}), "('CLIENT_SECRET')\n", (230, 247), False, 'import os\n'), ((259, 285), 'os.environ.get', 'os.environ.get', (['"""USERNAME"""'], {}), "('USERNAME')\n", (273, 285), False, 'import os\n'), ((300, 329), 'os.environ.get', 'os.environ.get', (['"""BUCKET_NAME"""'], {}), "('BUCKET_NAME')\n", (314, 329), False, 'import os\n'), ((393, 470), 'subprocess.Popen', 'subprocess.Popen', (["['aws', 'lambda', 'list-functions']"], {'stdout': 'subprocess.PIPE'}), "(['aws', 'lambda', 'list-functions'], stdout=subprocess.PIPE)\n", (409, 470), False, 'import subprocess\n'), ((753, 1004), 'subprocess.run', 'subprocess.run', (["['aws', 'lambda', 'update-function-configuration', '--function-name',\n    f'{function_name}', '--environment',\n    f'Variables={{CLIENT_ID={CLIENT_ID},CLIENT_SECRET={CLIENT_SECRET},USERNAME={USERNAME},BUCKET_NAME={BUCKET_NAME}}}'\n    ]"], {}), "(['aws', 'lambda', 'update-function-configuration',\n    '--function-name', f'{function_name}', '--environment',\n    f'Variables={{CLIENT_ID={CLIENT_ID},CLIENT_SECRET={CLIENT_SECRET},USERNAME={USERNAME},BUCKET_NAME={BUCKET_NAME}}}'\n    ])\n", (767, 1004), False, 'import subprocess\n'), ((549, 570), 'json.loads', 'json.loads', (['output[0]'], {}), '(output[0])\n', (559, 570), False, 'import json\n')]