Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a lambda environmental variable with terraform?

I want my lambda to call APIs, and that requires an API token. I want to place the API token into a lambda environment variable. How can I have terraform do this instead? Or am I approaching this the wrong way?

like image 252
Strawberry Avatar asked Oct 27 '18 13:10

Strawberry


People also ask

How do you apply an environment variable in Terraform?

Additionally, input variable values can also be set using Terraform environment variables. To do so, simply set the environment variable in the format TF_VAR_<variable name> . The variable name part of the format is the same as the variables declared in the variables.tf file.

How do I import Lambda into Terraform?

Open your Lambda in the AWS console, and select Actions > Export Function > Download deployment package. Download the file to the directory containing the Terraform file that you just created with the name lambda. zip . Next, run terraform init and terraform plan .


1 Answers

The Documentation here gives a pretty good example. Basically it's a environment block with a variables block. Then whatever key value pairs you want. Assuming you're using nodejs you can then refer to these variables in your lambda code by doing process.env.api_key. These values would be stored in plain text in your terraform code as well as the terraform state file. AWS encrypts the environment variables but you do need to concern yourself with how those values get there. If you are uncomfortable with them being stored in git and whatever storage you use for your state file then you can add them in manually through the console.

resource "aws_lambda_function" "test_lambda" {
  filename         = "lambda_function_payload.zip"
  function_name    = "lambda_function_name"
  runtime          = "nodejs8.10"
  ...

  environment {
    variables = {
      api_key = "super_secret"
    }
  }
}
like image 174
Jarred Olson Avatar answered Sep 27 '22 23:09

Jarred Olson