Adding a security group to a CloudFormation template and attaching it to an EC2 instance to allow public HTTP access while troubleshooting VPC and update issues
In this lesson you’ll add a Security Group to a CloudFormation template and attach it to an EC2 instance so the instance accepts HTTP (port 80) traffic from the public internet. This walk-through covers the minimal resource definitions, how to reference the security group from the instance, a common VPC-related error and how to fix it, and the CloudFormation update steps to apply the change.
When the instance launches without an explicit security group, it may use the account’s default security group. To allow HTTP access from the internet, add a security group resource to the same template and then attach it to the instance.This follows the same pattern used when adding other resources (for example, an S3 bucket and a corresponding bucket policy): define the new resource, then reference it where needed.
2. Define a Security Group with an ingress rule for HTTP
Add a Security Group resource that allows inbound HTTP traffic. SecurityGroup ingress rules are defined in the SecurityGroupIngress list; each rule is an object with fields describing protocol, ports, and source.
A security group is a virtual firewall for your instance. Use SecurityGroupIngress to open inbound ports (like HTTP) and SecurityGroupEgress to restrict or allow outbound traffic.
4. Common error: security group and instance must be in the same VPC
If CloudFormation reports an error about the security group reference format or the instance update fails, the most common cause is that the Security Group is being created in a different VPC than the instance. To ensure the Security Group is created in the correct VPC, add the VpcId property to the security group (copy the VPC ID from your EC2 instance details):
Validate and lint your CloudFormation template (for example with cfn-lint or the CloudFormation console validator), then update the stack.
Upload the updated template file, step through the update wizard, and submit the change.
CloudFormation may need to replace the instance to attach the new security group. Monitor the stack events until the update completes.
When the update finishes, CloudFormation may have terminated the previous instance and launched a fresh instance that has the new security group attached. Wait for the instance to reach the running state and finish initialization.
Open the EC2 console → Network & Security → Security Groups, locate the security group created by the stack, and inspect the Inbound rules. You should see an IPv4 HTTP rule (TCP port 80) with source 0.0.0.0/0.
Once verified, your EC2 instance is reachable over HTTP. To harden or extend the configuration you can:
Add additional ingress rules (HTTPS on 443, SSH on 22 with a restricted CIDR).
Define SecurityGroupEgress rules to restrict outbound traffic.
Use CloudFormation parameters or mappings to make the VpcId and CIDR ranges configurable.