Sub topic 12
Managing rules with the AWS CLI and Terraform
This chapter assumes you know CIDR notation, ports, and what a security group is and attaches to. You also need a working AWS CLI and a VPC to try the commands in. Here you will create, inspect, and remove rules from the command line, then declare the same rules in Terraform.
Creating a group and a first rule
A security group belongs to one VPC, so create-security-group needs a VPC ID. The new group starts with no inbound rules. It gets one default outbound rule that allows all traffic. Inbound rules are added in a separate call.
aws ec2 create-security-group \
--group-name web-sg \
--description "Web tier" \
--vpc-id vpc-0123456789abcdef0
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0The first command prints the new group's ID. Use that ID in the second command. The rule allows HTTPS from any IPv4 address.
Choosing a different source
The --cidr option is one kind of source. To allow traffic only from members of another group, use --source-group. Here the application tier accepts port 8080 only from instances that have the web group attached:
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0This rule keeps working when instances are replaced or their IP addresses change. A CIDR rule would need editing.
Adding a description
The shorthand options cannot set a description. For that, use --ip-permissions, which describes the whole rule in one structure:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=203.0.113.25/32,Description="Admin workstation"}]'This allows SSH from one administrator address, using a /32 so only that single address matches. The description tells a later reader why the rule exists.
Inspecting and revoking rules
describe-security-groups shows the current rules. To remove a rule, call revoke-security-group-ingress with the same protocol, port, and source that the rule was created with.
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0
aws ec2 revoke-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0A revoke that does not match an existing rule exactly fails with an error. It does not remove a similar rule. Outbound rules use the matching authorize-security-group-egress and revoke-security-group-egress commands.
Declaring the same rules in Terraform
Infrastructure as code (IaC) keeps rules in version control and shows changes before they are applied. The Terraform AWS provider offers one resource for the group and one resource per rule:
resource "aws_security_group" "web" {
name = "web-sg"
description = "Web tier"
vpc_id = var.vpc_id
}
resource "aws_vpc_security_group_ingress_rule" "https" {
security_group_id = aws_security_group.web.id
cidr_ipv4 = "0.0.0.0/0"
ip_protocol = "tcp"
from_port = 443
to_port = 443
description = "HTTPS from anywhere"
}Each rule is its own resource, so you can add, change, or delete one without touching the others. Use cidr_ipv4 for an IPv4 range. For a group reference, set referenced_security_group_id instead.
Avoiding drift
Rules added by hand in the console or CLI are not in your Terraform state. Terraform reports a difference the next time you run plan. Pick one tool to manage each group, and use the CLI on Terraform-managed groups only for read-only inspection.
Exercise
You run aws ec2 revoke-security-group-ingress with --port 443 --cidr 10.0.0.0/16, but the rule was created with --cidr 0.0.0.0/0. What happens, and why?
Show answer
The call fails with an error and no rule is removed. Revoke must match an existing rule's protocol, port range, and source exactly. A different CIDR does not match the rule that was created.
Exercise
Which CLI option form do you need to attach a description to a CIDR rule, and why does Terraform handle this more simply?
Show answer
Use --ip-permissions with a Description inside IpRanges, because the shorthand options such as --cidr cannot set one. In Terraform, each rule resource has a description argument.