简体   繁体   English

从 terraform 中的安全组名称中提取安全组 ID

[英]Extract security group id from security group name in terraform

I am trying to write a generic script in terraform to create ALB (in AWS).我正在尝试在 terraform 中编写一个通用脚本来创建 ALB(在 AWS 中)。 The security groups are created (sg_alb) but not in the same script.安全组已创建 (sg_alb) 但不在同一脚本中。 It is pre-created I want to extract the id of the security group and pass it security_groups = ["${aws_security_group.lb_sg.id}"]它是预先创建的,我想提取安全组的 id 并将其传递给 security_groups = ["${aws_security_group.lb_sg.id}"]

I have seen examples where the above reference is made but that is assuming the security group is created along with ALB我看过上面提到的例子,但假设安全组是与 ALB 一起创建的

How can I achieve this ?我怎样才能做到这一点?

Thank you Kumar谢谢库马尔

This is what Terraform data sources are for.这就是 Terraform 数据源的用途。 If your Terraform module is getting a security group name as a variable, then you would look up the security group like this:如果您的 Terraform 模块将安全组名称作为变量获取,那么您可以像这样查找安全组:

data "aws_security_group" "lb_sg" {
  name = var.security_group_name
}

Then you could use the data source when creating your ALB, like this:然后您可以在创建 ALB 时使用数据源,如下所示:

security_groups = [data.aws_security_group.lb_sg.id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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