What you’ll create
- A
stack.yamlCloudFormation template. - Template
Parametersto capture user input (key pair and EC2 name). - Template
Resourcesfor an EC2 instance and a Security Group. - Template
Outputsto reveal useful runtime values such as the instance public IP.
Quick overview of template structure
CloudFormation templates declare resources under the top-levelResources key. Each resource has:
- A logical name (your chosen identifier).
- A
Type(the AWS resource type, e.g.,AWS::EC2::Instance). Propertiesthat configure the resource.
Type and supported Properties: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html
Example snippet showing logical name and type:
Pick the AMI and key pair
Choose an AMI (ImageId) in the EC2 Launch Instance console or from the AWS Marketplace. Also supply or pick an existing key pair name from your account — this is used for SSH access to the instance.

Security groups and VPC considerations
- Create a security group resource in the same template and add ingress rules (for example, allow SSH on port 22).
- For EC2 instances launched inside a VPC, reference the security group using
SecurityGroupIdson the instance. - If your account requires specifying a VPC when creating security groups, include the
VpcIdproperty on the security group resource.
Minimal, complete CloudFormation template
This example:- Accepts a key-pair name and an EC2 name as parameters.
- Creates a security group that allows SSH ingress.
- Creates an EC2 instance that uses the security group and key pair.
- Exposes the instance public IP in
Outputs.
ImageId with a valid AMI for your region.
Specifying
Type: AWS::EC2::KeyPair::KeyName for the KeyName parameter causes the CloudFormation console to show a drop-down listing existing key pairs in the selected region — making it easier and less error-prone than entering an arbitrary string.Deploying the template using the AWS Console
- Open the CloudFormation service in the AWS Management Console.
- Create stack → Upload a template file → select
stack.yaml. - Enter a Stack name, provide the
Ec2Name, and select theKeyNamefrom the drop-down. Proceed through the wizard. - Review and create the stack.

Monitor stack events and resources
Watch stack events during creation to see lifecycle events for each resource. After completion, use the Resources tab to open or examine the individual AWS resources created by the stack (EC2 instance, security group, etc.).
Cleaning up: deleting the stack
To remove all created resources, delete the CloudFormation stack. CloudFormation attempts to delete resources it created. If deletion fails (for example, because a resource was modified outside CloudFormation or has deletion protection), resolve those issues and retry.
Quick reference table
Additional tips and next steps
- Use
SecurityGroupIdswhen referencing security groups by logical ID from the same template. - If you need to place the instance in a specific VPC/subnet, add
SubnetIdand useVpcIdon the security group resource. - Extend the template by adding UserData, IAM instance profiles, block device mappings, or autoscaling resources as your architecture needs grow.
Links and references
- CloudFormation resource and property reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html
- EC2 documentation and AMI lookup: https://docs.aws.amazon.com/ec2/
- AWS Console: https://console.aws.amazon.com/
Summary
- Use
Parametersto capture user input (key pairs, instance names). - Declare
Resourceswith logical names and correctTypevalues (consult the CloudFormation docs). - Use
SecurityGroupIdswhen referencing security groups defined in the same template. - Use
Outputsto return useful runtime values such as the instance public IP.