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,520 points
5,096 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
• 58,090 points

Related Questions In AWS

0 votes
2 answers

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

If you get lucky (it depends what ...READ MORE

answered Nov 3, 2020 in AWS by anonymous
15,157 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
• 58,090 points
2,545 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, 2019 in AWS by Shashank
• 1,370 points
1,606 views
0 votes
1 answer
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
• 58,090 points
9,624 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP