How to access an AWS Lambda environment variable from Python

0 votes

Using the new environment variable support in AWS Lambda, I've added an env var via the webui for my function.

How do I access this from Python? I tried:

import os MY_ENV_VAR = os.environ['MY_ENV_VAR']

but my function stopped working (if I hard code the relevant value for MY_ENV_VAR it works fine)

Jul 20, 2018 in AWS by bug_seeker
• 14,970 points
1,524 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Try this code hope it will work I used this code; it includes both cases, setting the variable from the handler and setting it from outside the handler.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""Trying new lambda stuff"""

import os

import configparser

class BqEnv(object):

"""Env and self variables settings"""

def __init__(self, service_account, configfile=None):

config = self.parseconfig(configfile) self.env = config self.service_account = service_account

@staticmethod

def parseconfig(configfile):

"""Connection and conf parser"""

config = configparser.ConfigParser()

config.read(configfile) env = config.get('BigQuery', 'env')

return env def variable_tests(self):

"""Trying conf as a lambda variable"""

my_env_var = os.environ['MY_ENV_VAR']

print my_env_var

print self.env

return True

def lambda_handler(event, context):

"""Trying env variables."""

print event configfile = os.environ['CONFIG_FILE']

print configfile print type(str(configfile))

bqm = BqEnv('some-json.json', configfile)

bqm.variable_tests()

return True

answered Jul 20, 2018 by Priyaj
• 56,120 points

Related Questions In AWS

0 votes
1 answer

How do I add python libraries to an AWS lambda function for Alexa?

As it is described in the Amazon ...READ MORE

answered Oct 23, 2018 in AWS by Archana
• 3,770 points
83 views
0 votes
1 answer

How to access AWS Lambda environment variables using Java?

In Spring Core change the PropertySourcesPlaceholderConfigurer class can ...READ MORE

answered Oct 24, 2018 in AWS by Priyaj
• 56,120 points
305 views
+1 vote
2 answers

How to launch and access an instance using AWS-CLI?

aws ec2 run-instances --image-id ami-id --key-name yourkeyname ...READ MORE

answered Feb 23 in AWS by Shashank
• 1,350 points
79 views
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.