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
• 15,310 points
1,831 views

1 answer to this question.

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,160 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
• 4,090 points
184 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,160 points
435 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
99 views
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer

how to access AWS S3 from Lambda in VPC

With boto3, the S3 urls are virtual by default, ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 56,160 points
1,241 views