-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
Terraform Core Version
1.5.7
AWS Provider Version
5.21.0
Affected Resource(s)
- aws_ec2_client_vpn_endpoint
Expected Behavior
Adding security groups to a aws_ec2_client_vpn_endpoint
resource using the security_group_ids
parameter should leave the client vpn service in a state where the new security group controls access.
Actual Behavior
The security groups are updated in the AWS management console under: "Details", "Target Network Associations", and "Security Groups"; however the underlying security group that is used by the client VPN to control network access remains the group that was set prior to terraform running.
Relevant Error/Panic Output Snippet
No response
Terraform Configuration Files
Start with a config like the example config:
resource "aws_ec2_client_vpn_endpoint" "example" {
description = "terraform-clientvpn-example"
server_certificate_arn = aws_acm_certificate.cert.arn
client_cidr_block = "10.0.0.0/16"
authentication_options {
type = "certificate-authentication"
root_certificate_chain_arn = aws_acm_certificate.root_cert.arn
}
}
Then add a security group via the security_group_ids parameter.
Steps to Reproduce
tl;dr: The way terraform changes security groups appears to leave the old security group as the active control for network connections on the modified client vpn instance without any indication that this has happened via terraform or the aws console.
Sorry for the verbosity of this report, this bug took me quite a bit of time to track down.
I am updating a client vpn service in our AWS account to move away from the default group for the vpc and instead use a dedicated, new security group. When applying the change to security_group_ids everything appears successful, however the default security group that was initially associated with the VPN connection remains the group that controls network access. For my test I have a EC2 instance in the same VPC as the client vpn endpoint, which allows all all networking (0.0.0.0/0 on all protocols). I setup the client vpn to be fully usable such that I can connect to this ec2 instance prior to running terraform. I create a new security group (sg-035b783b847b571b0) which allows egress on all protocols to 0.0.0.0/0, same as the default group.
This change is then applied:
# module.networking.module.vpn[0].aws_ec2_client_vpn_endpoint.vpn will be updated in-place
~ resource "aws_ec2_client_vpn_endpoint" "vpn" {
id = "cvpn-endpoint-02c340048f94f91ad"
~ security_group_ids = [
+ "sg-032b73d4686bcff2b",
- "sg-035b783b847b571b0",
]
# (13 unchanged attributes hidden)
# (4 unchanged blocks hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
I then disconnect and reconnect from the VPN to ensure that everything is fully up to date. Everything still works, I can connect to the EC2 instance just fine. The Client VPN instance in the management console shows the security group to be the new group (sg-032b73d4686bcff2b) and all appears well. However if I remove the egress rule from the security group that no longer should be associated with the CVPN endpoint (sg-035b783b847b571b0) the EC2 instance becomes inaccessible. If I add it back and remove the egress rule from the new group such that the old groups allows egress, and the new group does not then the EC2 instance remains accessible even though the group that the client VPN is associated with shouldn't be allowing egress at all.
Now, the interesting thing is that I can re-associate the old security group through the management console, then associate the new group through the management console and it the security group appears to work as expected. Thus the management console does something different than terraform when changing security groups.
Checking CloudTrail shows that the working AWS Management Console uses the ApplySecurityGroupsToClientVpnTargetNetwork
API call with the following request data:
"requestParameters": {
"ApplySecurityGroupsToClientVpnTargetNetworkRequest": {
"ClientVpnEndpointId": "cvpn-endpoint-02c340048f94f91ad",
"VpcId": "vpc-xxxxxxxxxxxxxxxxx",
"SecurityGroupId": {
"tag": 1,
"content": "sg-032b73d4686bcff2b"
}
}
},
While terraform uses the ModifyClientVpnEndpoint
API call with the following request data:
"requestParameters": {
"ModifyClientVpnEndpointRequest": {
"ClientVpnEndpointId": "cvpn-endpoint-02c340048f94f91ad",
"VpcId": "vpc-xxxxxxxxxxxxxxxxx",
"SecurityGroupId": {
"tag": 1,
"content": "sg-032b73d4686bcff2b"
}
}
},
This call comes from this section. From what I can tell the terraform use case appears to be correct, and the bug maybe in the AWS API directly (this has been reported to them already) however I figured I would report it here for documentation and to highlight it just in case it requires a fix in the terraform AWS provider as well..
Debug Output
No response
Panic Output
No response
Important Factoids
No response
References
No response
Would you like to implement a fix?
None