Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Terraform object into an AWS Lambda environment

Lambda functions support the environment parameter and make it easy to define a key-value pair. But what about getting an object (defined by a module variable eg) into the function's environment?

Quick example of what I'm trying to accomplish in python 3.7:

Terraform:

# variable definition

variable foo {
  type = map(any)
  default = {
    a = "b"
    c = "d"
  }
}


resource "aws_lambda_function" "lambda" {
  .
  .
  .
  environment {
    foo = jsonencode(foo)
  }
}

and then in my function:

def bar:
  for k in os.environ["foo"]:
     print(k)

Thanks !

like image 915
Dragon B Avatar asked Oct 24 '25 02:10

Dragon B


1 Answers

In python, you will have to get json string and convert it to dict:

import json

def bar:
  for k in json.loads(os.environ["foo"]):
     print(k)
like image 92
Marcin Avatar answered Oct 25 '25 15:10

Marcin