简体   繁体   中英

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.

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
  }
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM