Home > Mobile >  terraform retrieve resource id into variable to use in creation of lambda with environment variables
terraform retrieve resource id into variable to use in creation of lambda with environment variables

Time:03-19

I am trying to use terraform to standup aws cognito, and dynamically pass some output value of the created resource as environment variables to a lambda resource that terraform will also create.

I have a lambda function that handles authentication with cognito, and requires the cognito client app id and client app secret to function.

Wondering if there is a way to get this metadata within terraform and reference it when the lambda resource gets created.

CodePudding user response:

The Terraform aws_cognito_user_pool_client resource, which you will use to create the Cognito user pool client via Terraform, has those values you mention as outputs. All you need to do is reference those values in your Lambda resource. Like so:

resource "aws_cognito_user_pool_client" "my_app_client" {
 ...
}

resource "aws_lambda_function" "my_lambda_function" { 
 ...

 environment {
  variables = {
    "COGNITO_CLIENT_ID"     = aws_cognito_user_pool_client.my_app_client.id,
    "COGNITO_CLIENT_SECRET" = aws_cognito_user_pool_client.my_app_client.client_secret
  }
 }
}
  • Related