Why can I not use aws_lambda_function datasource inside aws_lambda_alias routing_config?

十年热恋 提交于 2019-12-11 16:57:28

问题


I am experimenting with a blue/green deployment setup for lambdas using terraform and lambda aliases.

I am trying to automatically retrieve the previously deployed version of the lambda by using the aws_lambda_function data source and using the value inside the routing_config => additional_version_weights. This would allow me to set up a traffic split between the previously deployed version and the version that has just been deployed.

However, I have run into 2 errors I don't quite understand.

The first error is when I try and use the data source in conjunction with a regular variable. In this case terraform complains about being unable to parse the value.

If I hard code the value terraform will attempt to run the update, however, it will fail as it tries to set the version in the routing configuration to an empty value which causes a validation error. If I instead output the value I can see that the correct version is retrieved.

Example code and steps to reproduce can be found on link below.

https://github.com/jaknor/terraform-lambda-data-source-issue

Is anyone able to explain why this isn't working?

Please note, while I appreciate that there are other ways of achieving my goal, at the moment I am only interested in understanding these particular errors.


回答1:


In Terraform v0.11 and prior, interpolation sequences are not supported on the left side of an = symbol introducing an argument or object key.

To generate a map with dynamic keys, you must instead use the map function:

  additional_version_weights = "${map(data.aws_lambda_function.existing_lambda_func.version, var.lambda_previous_version_percentage)}"

In Terraform v0.12 (which is in beta as I write this) the parser is now able to distinguish between arguments (which must be constants in the configuration) and map keys (which can be arbitrary expressions) and so the following syntax is preferable, although the above will still work for backward compatibility.

  additional_version_weights = {
    (data.aws_lambda_function.existing_lambda_func.version) = var.lambda_previous_version_percentage
  }

The additional parentheses around the key expression are important to tell Terraform that this should be understood as a normal expression rather than as a literal name.



来源:https://stackoverflow.com/questions/55890700/why-can-i-not-use-aws-lambda-function-datasource-inside-aws-lambda-alias-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!