Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
  }
 }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading