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

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

问题描述

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

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.

但是,我遇到了 2 个我不太明白的错误.

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-源问题

有人能解释为什么这不起作用吗?

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(我写这篇文章时它处于测试阶段)中,解析器现在能够区分参数(必须是配置中的常量)和映射键(可以是任意表达式),因此以下语法更可取,尽管上述方法仍然适用于向后兼容.


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天全站免登陆