How to fire EC2 instances and upload/run a startup script on each of them?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Amazon Elastic Compute Cloud (EC2) is a web service that provides resizable compute capacity in the cloud. It enables users to quickly and effortlessly scale capacity as their computing requirements evolve. Today, we will focus on how to launch EC2 instances and run startup scripts on each instance to automate tasks.
Preparing to Launch EC2 Instances
Prerequisites
- AWS Account: You need an active AWS account to create and manage EC2 instances.
- AWS CLI: The AWS Command Line Interface (CLI) tool must be installed and configured on your machine.
- IAM Role: Ensure that the IAM user you’re using has necessary permissions, such as EC2FullAccess.
- Key Pair: A key pair will be required to SSH into your instance. You can create one from the EC2 Dashboard.
Installing AWS CLI
To manage EC2 instances using the CLI, install AWS CLI and configure it with your credentials:
- Go to your AWS Management Console and select EC2.
- Click on "Launch Instance" and follow the wizard to configure your instance. Select details like Amazon Machine Image (AMI), instance type, VPC settings, storage, etc.
- Make sure to select an existing key pair or create a new one to access the instance securely.
- Configure a security group to allow SSH access (port 22) from your IP address.
- In the Advanced Details section, you can specify a startup script using the User Data field.
- Make sure,
ami-12345678corresponds to a valid AMI ID. - Replace
MyKeyPair,sg-123456, andsubnet-123456with your actual Key Pair name, security group ID, and subnet ID, respectively.
Related reading
- How to fix a drifted AWS CloudFormation stack?
- How to fix 'Access Denied' while deleting empty S3 Elastic Beanstalk?
- How to fix apt-get command not found on AWS EC2?
- How to force https on elastic beanstalk?
- How to fix NoHttpResponseException when running Wiremock on jenkins?
- How to force a redeploy with HELM
- How to force SSL for Kubernetes Ingress on GKE
- How to format a URL to get a file from Amazon S3?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.