AuthorizeSecurityGroupIngressCommand

Adds the specified inbound (ingress) rules to a security group.

An inbound rule permits instances to receive traffic from the specified IPv4 or IPv6 address range, the IP address ranges that are specified by a prefix list, or the instances that are associated with a destination security group. For more information, see Security group rules .

You must specify exactly one of the following sources: an IPv4 or IPv6 address range, a prefix list, or a security group. You must specify a protocol for each rule (for example, TCP). If the protocol is TCP or UDP, you must also specify a port or port range. If the protocol is ICMP or ICMPv6, you must also specify the ICMP/ICMPv6 type and code.

Rule changes are propagated to instances associated with the security group as quickly as possible. However, a small delay might occur.

For examples of rules that you can add to security groups for specific access scenarios, see Security group rules for different use cases  in the Amazon EC2 User Guide.

For more information about security group quotas, see Amazon VPC quotas  in the Amazon VPC User Guide.

Example Syntax

Use a bare-bones client and the command you need to make an API call.

import { EC2Client, AuthorizeSecurityGroupIngressCommand } from "@aws-sdk/client-ec2"; // ES Modules import
// const { EC2Client, AuthorizeSecurityGroupIngressCommand } = require("@aws-sdk/client-ec2"); // CommonJS import
const client = new EC2Client(config);
const input = { // AuthorizeSecurityGroupIngressRequest
  CidrIp: "STRING_VALUE",
  FromPort: Number("int"),
  GroupId: "STRING_VALUE",
  GroupName: "STRING_VALUE",
  IpPermissions: [ // IpPermissionList
    { // IpPermission
      IpProtocol: "STRING_VALUE",
      FromPort: Number("int"),
      ToPort: Number("int"),
      UserIdGroupPairs: [ // UserIdGroupPairList
        { // UserIdGroupPair
          Description: "STRING_VALUE",
          UserId: "STRING_VALUE",
          GroupName: "STRING_VALUE",
          GroupId: "STRING_VALUE",
          VpcId: "STRING_VALUE",
          VpcPeeringConnectionId: "STRING_VALUE",
          PeeringStatus: "STRING_VALUE",
        },
      ],
      IpRanges: [ // IpRangeList
        { // IpRange
          Description: "STRING_VALUE",
          CidrIp: "STRING_VALUE",
        },
      ],
      Ipv6Ranges: [ // Ipv6RangeList
        { // Ipv6Range
          Description: "STRING_VALUE",
          CidrIpv6: "STRING_VALUE",
        },
      ],
      PrefixListIds: [ // PrefixListIdList
        { // PrefixListId
          Description: "STRING_VALUE",
          PrefixListId: "STRING_VALUE",
        },
      ],
    },
  ],
  IpProtocol: "STRING_VALUE",
  SourceSecurityGroupName: "STRING_VALUE",
  SourceSecurityGroupOwnerId: "STRING_VALUE",
  ToPort: Number("int"),
  TagSpecifications: [ // TagSpecificationList
    { // TagSpecification
      ResourceType: "capacity-reservation" || "client-vpn-endpoint" || "customer-gateway" || "carrier-gateway" || "coip-pool" || "declarative-policies-report" || "dedicated-host" || "dhcp-options" || "egress-only-internet-gateway" || "elastic-ip" || "elastic-gpu" || "export-image-task" || "export-instance-task" || "fleet" || "fpga-image" || "host-reservation" || "image" || "import-image-task" || "import-snapshot-task" || "instance" || "instance-event-window" || "internet-gateway" || "ipam" || "ipam-pool" || "ipam-scope" || "ipv4pool-ec2" || "ipv6pool-ec2" || "key-pair" || "launch-template" || "local-gateway" || "local-gateway-route-table" || "local-gateway-virtual-interface" || "local-gateway-virtual-interface-group" || "local-gateway-route-table-vpc-association" || "local-gateway-route-table-virtual-interface-group-association" || "natgateway" || "network-acl" || "network-interface" || "network-insights-analysis" || "network-insights-path" || "network-insights-access-scope" || "network-insights-access-scope-analysis" || "outpost-lag" || "placement-group" || "prefix-list" || "replace-root-volume-task" || "reserved-instances" || "route-table" || "security-group" || "security-group-rule" || "service-link-virtual-interface" || "snapshot" || "spot-fleet-request" || "spot-instances-request" || "subnet" || "subnet-cidr-reservation" || "traffic-mirror-filter" || "traffic-mirror-session" || "traffic-mirror-target" || "transit-gateway" || "transit-gateway-attachment" || "transit-gateway-connect-peer" || "transit-gateway-multicast-domain" || "transit-gateway-policy-table" || "transit-gateway-route-table" || "transit-gateway-route-table-announcement" || "volume" || "vpc" || "vpc-endpoint" || "vpc-endpoint-connection" || "vpc-endpoint-service" || "vpc-endpoint-service-permission" || "vpc-peering-connection" || "vpn-connection" || "vpn-gateway" || "vpc-flow-log" || "capacity-reservation-fleet" || "traffic-mirror-filter-rule" || "vpc-endpoint-connection-device-type" || "verified-access-instance" || "verified-access-group" || "verified-access-endpoint" || "verified-access-policy" || "verified-access-trust-provider" || "vpn-connection-device-type" || "vpc-block-public-access-exclusion" || "route-server" || "route-server-endpoint" || "route-server-peer" || "ipam-resource-discovery" || "ipam-resource-discovery-association" || "instance-connect-endpoint" || "verified-access-endpoint-target" || "ipam-external-resource-verification-token" || "mac-modification-task",
      Tags: [ // TagList
        { // Tag
          Key: "STRING_VALUE",
          Value: "STRING_VALUE",
        },
      ],
    },
  ],
  DryRun: true || false,
};
const command = new AuthorizeSecurityGroupIngressCommand(input);
const response = await client.send(command);
// { // AuthorizeSecurityGroupIngressResult
//   Return: true || false,
//   SecurityGroupRules: [ // SecurityGroupRuleList
//     { // SecurityGroupRule
//       SecurityGroupRuleId: "STRING_VALUE",
//       GroupId: "STRING_VALUE",
//       GroupOwnerId: "STRING_VALUE",
//       IsEgress: true || false,
//       IpProtocol: "STRING_VALUE",
//       FromPort: Number("int"),
//       ToPort: Number("int"),
//       CidrIpv4: "STRING_VALUE",
//       CidrIpv6: "STRING_VALUE",
//       PrefixListId: "STRING_VALUE",
//       ReferencedGroupInfo: { // ReferencedSecurityGroup
//         GroupId: "STRING_VALUE",
//         PeeringStatus: "STRING_VALUE",
//         UserId: "STRING_VALUE",
//         VpcId: "STRING_VALUE",
//         VpcPeeringConnectionId: "STRING_VALUE",
//       },
//       Description: "STRING_VALUE",
//       Tags: [ // TagList
//         { // Tag
//           Key: "STRING_VALUE",
//           Value: "STRING_VALUE",
//         },
//       ],
//       SecurityGroupRuleArn: "STRING_VALUE",
//     },
//   ],
// };

Example Usage

 There was an error loading the code editor. Retry

AuthorizeSecurityGroupIngressCommand Input

Parameter
Type
Description
CidrIp
string | undefined

The IPv4 address range, in CIDR format.

Amazon Web Services canonicalizes  IPv4 and IPv6 CIDRs. For example, if you specify 100.68.0.18/18 for the CIDR block, Amazon Web Services canonicalizes the CIDR block to 100.68.0.0/18. Any subsequent DescribeSecurityGroups and DescribeSecurityGroupRules calls will return the canonicalized form of the CIDR block. Additionally, if you attempt to add another rule with the non-canonical form of the CIDR (such as 100.68.0.18/18) and there is already a rule for the canonicalized form of the CIDR block (such as 100.68.0.0/18), the API throws an duplicate rule error.

To specify an IPv6 address range, use IP permissions instead.

To specify multiple rules and descriptions for the rules, use IP permissions instead.

DryRun
boolean | undefined

Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

FromPort
number | undefined

If the protocol is TCP or UDP, this is the start of the port range. If the protocol is ICMP, this is the ICMP type or -1 (all ICMP types).

To specify multiple rules and descriptions for the rules, use IP permissions instead.

GroupId
string | undefined

The ID of the security group.

GroupName
string | undefined

[Default VPC] The name of the security group. For security groups for a default VPC you can specify either the ID or the name of the security group. For security groups for a nondefault VPC, you must specify the ID of the security group.

IpPermissions
IpPermission[] | undefined

The permissions for the security group rules.

IpProtocol
string | undefined

The IP protocol name (tcp, udp, icmp) or number (see Protocol Numbers ). To specify all protocols, use -1.

To specify icmpv6, use IP permissions instead.

If you specify a protocol other than one of the supported values, traffic is allowed on all ports, regardless of any ports that you specify.

To specify multiple rules and descriptions for the rules, use IP permissions instead.

SourceSecurityGroupName
string | undefined

[Default VPC] The name of the source security group.

The rule grants full ICMP, UDP, and TCP access. To create a rule with a specific protocol and port range, specify a set of IP permissions instead.

SourceSecurityGroupOwnerId
string | undefined

The Amazon Web Services account ID for the source security group, if the source security group is in a different account.

The rule grants full ICMP, UDP, and TCP access. To create a rule with a specific protocol and port range, use IP permissions instead.

TagSpecifications
TagSpecification[] | undefined

The tags applied to the security group rule.

ToPort
number | undefined

If the protocol is TCP or UDP, this is the end of the port range. If the protocol is ICMP, this is the ICMP code or -1 (all ICMP codes). If the start port is -1 (all ICMP types), then the end port must be -1 (all ICMP codes).

To specify multiple rules and descriptions for the rules, use IP permissions instead.

AuthorizeSecurityGroupIngressCommand Output

Parameter
Type
Description
$metadata
Required
ResponseMetadata
Metadata pertaining to this request.
Return
boolean | undefined

Returns true if the request succeeds; otherwise, returns an error.

SecurityGroupRules
SecurityGroupRule[] | undefined

Information about the inbound (ingress) security group rules that were added.

Throws

Name
Fault
Details
EC2ServiceException
Base exception class for all service exceptions from EC2 service.