-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement]: Terraform Resources Call AWS EC2 Security Group APIs Inconsistently #27079
Comments
Community NoteVoting for Prioritization
Volunteering to Work on This Issue
|
@aishwarya-88 Thank you for the very thorough write-up of the inconsistency 👏. I have opened #27642 to do as you suggest in your first bullet - Not pass any We are considering adding a "new" security group rule resource to address #20104 and will ensure that this issue is take into account during implementation. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Description
Introduction
Terraform provides two resources that allow a user to specify a security group rule--
aws_security_group
andaws_security_group_rule
resources.From the perspective of a customer, for both resources, Terraform states that the
from_port
andto_port
fields are required parameters[1][2]. Terraform also uses values of0
for ports whenprotocol
is-1
orall
in the documentation as examples[3]. For this reason, users always pass in some value for port parameters when making requests via Terraform and are more likely to pass in 0 as the value. In contrast, AWS EC2 Security Group APIs specify the port fields to be optional[4].On the other hand, behind the scenes, both Terraform resources make (Authorize|Revoke)SecurityGroup(Ingress|Egress) API calls to AWS EC2 in order to authorize or revoke a rule for a security group. However the parameters used by Terraform to make AWS EC2 API calls for each resource is different. This results in inconsistent call patterns from Terraform to AWS EC2.
Current Behavior
Current behavior of
aws_security_group_rule
resource:Today, if a Terraform
aws_security_group_rule
resource is specified with any combination ofprotocol=all
orprotocol=-1
and any value forfrom_port
andto_port
:Special logic for this behavior:
terraform-provider-aws/internal/service/ec2/vpc_security_group_rule.go
Lines 65 to 72 in 4a7da7e
Current behavior of
aws_security_group
resource:Based on different combinations of input parameters passed in, the Terraform
aws_security_group
resource specified withprotocol=-1
orprotocol=all
and withfrom_port
andto_port
values passed in results in:aws_security_group
resource is specified with the parameter combination ofprotocol=-1
and non-zerofrom_port
andto_port
values, then the Terraform request fails. The error message states that the port values “must be 0” but instead, if a value must be passed into the field then it should be-1
for the ports (indicating all ports since specifying port 0 means that the intention is to allow traffic only on port 0). For all other combinations of input, the requests succeed (see this outlined in the table below).No special logic for this behavior:
terraform-provider-aws/internal/service/ec2/vpc_security_group.go
Lines 137 to 140 in 4a7da7e
Proposed Solution
There are two changes we want to propose here:
aws_security_group
resource mimic the behavior of theaws_security_group_rule
resource when calling AWS EC2 APIs. This means that in cases whereprotocol=-1
orprotocol=all
is passed in with anyfrom_port
andto_port
parameter values, theaws_security_group
resource must make AWS EC2 (Authorize|Revoke)SecurityGroup(Ingress|Egress) API calls without specifying the port parameters in the request.aws_security_group
resource is specified with the parameter combination ofprotocol=-1
and non-zerofrom_port
andto_port
values. Instead of the error message stating port values should be0
, the error message should state port values should be-1
.Therefore, the error message should be:-1
if protocol is-1
orall
.The solutions outlined here are chosen to be the least backwards breaking changes and are in line with AWS public documentation[5] which states any port values will be ignored when protocol is specified as all or -1.
: https://github.com/hashicorp/terraform-provider-aws/blame/main/internal/service/ec2/vpc_security_group_rule.go#L65-L72
Affected Resource(s) and/or Data Source(s)
aws_security_group
Potential Terraform Configuration
References
No response
Would you like to implement a fix?
No
The text was updated successfully, but these errors were encountered: