为什么不能在aws_lambda_alias routing_config中使用aws_lambda_function数据源? [英] Why can I not use aws_lambda_function datasource inside aws_lambda_alias routing_config?

查看:124
本文介绍了为什么不能在aws_lambda_alias routing_config中使用aws_lambda_function数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用terraform和lambda别名为lambda设置蓝色/绿色部署设置.

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

我试图通过使用aws_lambda_function数据源并使用routing_config => Additional_version_weights内部的值来自动检索以前部署的lambda版本.这样,我就可以在先前部署的版本和刚刚部署的版本之间建立流量分配.

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.

第一个错误是当我尝试将数据源与常规变量一起使用时.在这种情况下,terraform抱怨无法解析该值.

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.

如果我进行硬编码,则terraform值将尝试运行更新,但是它将失败,因为它将尝试将路由配置中的版本设置为空值,从而导致验证错误.如果我改为输出该值,则可以看到已检索到正确的版本.

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.

推荐答案

在Terraform v0.11及更低版本中,在引入自变量或对象键的=符号的左侧不支持插值序列.

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

要生成带有动态键的地图,必须改为使用map函数:

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


在Terraform v0.12(我写这篇文章时它是beta)中,解析器现在可以区分参数(在配置中必须是常量)和映射键(可以是任意表达式),因此以下内容语法是可取的,尽管上面的方法仍然可以向后兼容.


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
  }

关键表达式周围的附加括号对于告诉Terraform应该将其理解为正则表达式而不是文字名称很重要.

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.

这篇关于为什么不能在aws_lambda_alias routing_config中使用aws_lambda_function数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆