I have created a new security group in the Autoscaling module in terraform. I want to add this security group to an existing security group in the module Database. The security group used in the database module is already created through the console. How to add a new security group to an existing security group in terraform?
Asked
Active
Viewed 1,197 times
-1
-
1Do you have any TF code to show what you want to do? – Marcin May 19 '21 at 07:42
1 Answers
0
If you want to change the SG created by console, you have to edit it manually.
If you change the SG created by terraform, you can get the id of the existed SG manually and add it to a rule of SG
EDIT:
You need to output:
output "security_group_id" {
description = "Security group ID attached to the EKS workers."
value = module.auto_scaling_module.security_group_id
}
Than import by using data in data.tf
data "terraform_remote_state" "autoscaling" {
backend = "s3"
config = {
region = "us-west-1"
bucket = "wherestatelocate"
key = "path"
}
}
Than use it in database module
security_groups = [data.terraform_remote_state.autoscaling.outputs.security_group_id
]
This is what i am using

Chuong Nguyen
- 1,077
- 6
- 15
-
Can you tell me how to extract the id of the security group created in the autoscaling module so that I can add it to the security group of the database module? – user104471 May 19 '21 at 08:51
-
Can you tell me which module are you using? You can write an outputs.tf and export your SG id. In database module, you can import by using data and use it in database module – Chuong Nguyen May 19 '21 at 08:55
-
I have two folders. In folder, I am creating an Autoscaling module in which I am creating a security group through terraform module. In second folder, I am creating Database module in which vpc_security_group_ids = ["sg-476ef04a"] is used in which sg-476ef04a is already created and I want to add security group of auto scaling to this security group – user104471 May 19 '21 at 08:57
-
In first folder, you need to output your state by outputs resources. In second folder, you need to import data by using data resources. And you can you use that output to in vpc_security_group_ids field – Chuong Nguyen May 19 '21 at 09:03
-
I am sorry. But can you explain the last line to me? What do you mean by "And you can you use that output to in vpc_security_group_ids field ". I am confused about what to do after taking the output from one module. – user104471 May 19 '21 at 09:10
-
I will write an sample code in the Edit command so that you can understand – Chuong Nguyen May 19 '21 at 09:16
-
@user104471 if I helped you, answer's acceptance and vote will be very appriciated – Chuong Nguyen May 21 '21 at 09:50