Skip to content

Instantly share code, notes, and snippets.

@alanwill
Last active January 18, 2024 17:00
Show Gist options
  • Select an option

  • Save alanwill/9254414 to your computer and use it in GitHub Desktop.

Select an option

Save alanwill/9254414 to your computer and use it in GitHub Desktop.
AWS CloudFormation example that allows a security group rule to reference the same security group as the source.
{
"Description": "Create a VPC with a SG which references itself",
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"vpctester": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "172.16.0.0/23",
"EnableDnsSupport": false,
"EnableDnsHostnames": false,
"InstanceTenancy": "default",
"Tags": [ { "Key": "Name", "Value": "vpctester" } ]
}
},
"sgtester": {
"Type": "AWS::EC2::SecurityGroup",
"DependsOn": "vpctester",
"Properties": {
"GroupDescription": "vpc tester sg",
"VpcId": { "Ref": "vpctester" }
}
},
"sgtesteringress": {
"Type": "AWS::EC2::SecurityGroupIngress",
"DependsOn": "sgtester",
"Properties": {
"GroupId": { "Ref": "sgtester" },
"IpProtocol": "tcp",
"FromPort": "0",
"ToPort": "65535",
"SourceSecurityGroupId": { "Ref": "sgtester" }
}
}
}
}
@ffxsam
Copy link
Copy Markdown

ffxsam commented Feb 3, 2019

Thanks, this helped me!

@tkang007
Copy link
Copy Markdown

Thanks

@unacceptable
Copy link
Copy Markdown

Thanks!

I would just like to point out that there are unnecessary DependsOn portions though. If !Ref is used the dependency is automatically added and you don't have to explicitly state the dependancy.

@maldalx
Copy link
Copy Markdown

maldalx commented Sep 12, 2019

Thank you.

@aderbique
Copy link
Copy Markdown

Say that the security group "sgtester" already had an ingress rule associated with the group, would "sgtesteringress" overwrite the existing rules or append the new rules to the group?

@msonowal
Copy link
Copy Markdown

msonowal commented Nov 1, 2019

@alan thanks saved my night

@climberjase
Copy link
Copy Markdown

Thank you :)

@mdalvi
Copy link
Copy Markdown

mdalvi commented Jul 21, 2020

What does it mean to ingress on the self-security group? What does it do security-wise?

@john-aws
Copy link
Copy Markdown

What does it mean to ingress on the self-security group? What does it do security-wise?

It allows compute nodes in that security group to communicate with other compute nodes in the same security group.

@john-aws
Copy link
Copy Markdown

And the (untested) YAML equivalent:

Description: Create a VPC with a SG which references itself
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  vpctester:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 172.16.0.0/23
      EnableDnsSupport: false
      EnableDnsHostnames: false
      InstanceTenancy: default
      Tags:
      - Key: Name
        Value: vpctester
  sgtester:
    Type: AWS::EC2::SecurityGroup
    DependsOn: vpctester
    Properties:
      GroupDescription: vpc tester sg
      VpcId: !Ref vpctester
  sgtesteringress:
    Type: AWS::EC2::SecurityGroupIngress
    DependsOn: sgtester
    Properties:
      GroupId: !Ref sgtester
      IpProtocol: tcp
      FromPort: 0
      ToPort: 65535
      SourceSecurityGroupId: !Ref sgtester

@saumilsdk
Copy link
Copy Markdown

How to give all protocols?

@john-aws
Copy link
Copy Markdown

john-aws commented Feb 2, 2021

@saumilsdk See the IpProtocol documentation:

Use -1 to specify all protocols.

@SwathiKanduri
Copy link
Copy Markdown

can you help me understand the difference between groupId and sourceSecurityGroupId?

Also, consider for eg, I have an ec2 bastion host, I have an RDS in the private subnet. I want to create a security group on ec2 that allows all inbound ssh traffic through the Internet gateway. I have another security group on RDS that allows inbound traffic from ec2 bastion. How can I do this? should I use sourceSecuritygroupId:<id of ec2's SG> in the ingress of RDS's security group?

@john-aws
Copy link
Copy Markdown

john-aws commented Apr 1, 2021

@SwathiKanduri the groupId relates to the security group for which this AWS::EC2::SecurityGroupIngress resource is actually an ingress rule. The sourceSecurityGroupId relates to the security group which we want to allow inbound traffic from. In this case they both refer to sgtester because this is a self-referencing security group, but in the general case sourceSecurityGroupId would refer to some other security group that we want to allow inbound traffic from.

@rverma-ccs
Copy link
Copy Markdown

Thanks, it was helpful

@jjeanjacques10
Copy link
Copy Markdown

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment