I created cluster for learning terraform on aws and noticed that eks create the security group itself…even though I have created security group for cluster and nodegroup both …eks cluster created sg has allow rule for ALL.
What if I want to ammend this rule using terraform ?
I don;t want all ports and protocol and only suppose specific port like 8080 (except ports needed by k8s itself for its working)
I tried adding in vpc_config as
cluster_additional_security_group_ids = [aws_security_group.additional_sg.id]
but this doesn;t work
Also I am validating terraform cloud so i don;t think we have bash access on free trial account.
My requirement I can edit the security group after creation in single run
I have multiple tf script like one creates security group , one create nodegroup , one create cluster
so once the cluster.tf gets executed it will create the default security group
Now at this moment I want to get output of default security group created by EKS and run another tf script securitygroup.tf (this security group.tf is depends on cluster.tf and time_wait sleep of 5 minutes)
this securitygroup.tf edit the default security group like below
Have a data source that refers to the EKS cluster that is being deployed, like this.
From this data source you should be able to read the security group that was created by the cluster and use it in an aws_security_group_rule to add new rules. Ensure the rule resource has a depends_on to the cluster resource to ensure it is fully created first.
i made depends on on nodegroup creation complete as I want first complete cluster including nodegroup gets up and running then only security group gets change.
Can you help in correcting the above or guide me as right now with that I am getting following error :
What does this output print? It’s clearly not a single security group ID, which will be why you get the error, meaning there is something wrong wit your expression.
The output is showing you that the value you have retrieved is a list of security group IDs.
The rule resource expects a single security group.
Before you make further changes, inspect sg-029a25ca856aa96bb in the console and determine it is the SG you expect. If it is, then you should be able to pick it off with a final [0] on the end of the expression.
Error: no matching SecurityGroup found
│
│ with data.aws_security_group.cluster_security_group,
│ on ammendmentsg.tf line 1, in data “aws_security_group” “cluster_security_group”:
│ 1: data “aws_security_group” “cluster_security_group” {
It was just a case of understanding the schema of data.aws_eks_cluster to correctly target the security group ID.
You will come across this kind of problem a lot when working with terraform, especially when using resources you may not have used before. It’s simply a case of “work it out”. Putting things as outputs can help you understand what certain attributes look like - e.g. did you get a list when you expected a single item.