Change an instance's security groups

Replace the set of security groups attached to a running or stopped instance.

Last updated 18 November 2026View as Markdown

At launch, list the groups in securityGroupIds (see Launch an instance). Afterwards, replace the whole set. Replace the ids with your own.

Prerequisites

  • In the console, also ec2:DescribeInstances to open the instance's page (without it the page is not found).
  • Permission for ec2:ModifyInstanceAttribute.
  • The instance is not terminated (else 400 "Instance is terminated"). A node of a Kubernetes cluster is refused with 409: add rules to the cluster's node security group instead.
  • At most 16 groups.

Replace the groups

  1. Open the instance, then choose Networking.
  2. Under Security groups, choose Edit.
  3. Select the groups the instance should have and clear the others.
  4. Choose Save.

Result

The firewall change is applied before the call answers; the answer lists the groups attached afterwards. If the change cannot be applied, the answer is 502 and nothing is recorded. An unknown group is refused with 400 "Security group … not found". With no group, the instance is not filtered: every port is open to the internet.

Did this page help you?