Description
Omitting VpcId from AWS::EC2::SecurityGroup places the group in the Region’s default VPC.
Potential impact
Group creation fails if the Region has no default VPC. Otherwise, the group may land in a different VPC from the intended resource.
Remediation
Set VpcId to the VPC where the group will be used. Changing an existing group’s VPC replaces the group, so plan for the impact on connections.
Examples
Use the target VPC for myVPC and the approved SSH source range for AdminCidr.
Before
yaml
Resources:
InstanceSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupName: My Group Name
# Allow SSH access on port 22
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref AdminCidr
After
yaml
Resources:
InstanceSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupName: My Group Name
# Allow SSH access on port 22
GroupDescription: Enable SSH access via port 22
VpcId:
Ref: myVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref AdminCidr