A Python script to start your AWS EC2 instance and update your computer's hosts
file with the instance's public IP address for as many hosts as you want. All in one step!
- Starts the EC2 instance.
- Retrieves the instance's public IP.
- Update the system
hosts
file with the public IP for the urls specified in thehosts.txt
file (one per line).
- Python 3.6 or higher.
- An AWS account with access key credentials. Learn how to create access keys in AWS.
- Administrator or root permissions to write the
hosts
system file.
Install the required dependencies using pip
:
pip install boto3 python-dotenv pywin32
sudo pip install boto3 python-dotenv
-
Clone the repository:
git clone https://github.com/carloscolmenarez/start-ec2-instance-and-update-hosts-file.git cd start-ec2-instance-and-update-hosts-file
-
Create a
.env
file in the root directory of the project with the following content:INSTANCE_ID=your_instance_id AWS_ACCESS_KEY_ID=your_access_key_id AWS_SECRET_ACCESS_KEY=your_secret_access_key AWS_REGION=eu-west-1 HOSTS_PATH=C:\Windows\System32\drivers\etc\hosts
Replace
your_instance_id
,your_access_key_id
,your_secret_access_key
, andeu-west-1
with your instance ID, AWS credentials (Access Key), and region. -
Create a
hosts.txt
file in the root directory of the project with the hosts you want to add/update in yourhosts
system file, using the public IP of the EC2 instance (one host per line) Seehosts.txt.example
for reference.
- Open a terminal as administrator an run:
python start_ec2_instance.py
sudo python start_ec2_instance.py
If you prefer not to use the terminal, you can create a Windows desktop shortcut:
- Run the
create_windows_shortcut.py
script to create a desktop shortcut called "Start EC2 Instance and Update Hosts". Then, you can run the script just clicking on the shortcut.
python create_windows_shortcut.py
Now you will have a shortcut in your desktop to run the script just in one click (or two).
- Ensure you have administrator permissions to modify
hosts
file. - Do not share your AWS credentials. Use secure methods to manage them.
This project is licensed under the MIT License. See the LICENSE file for details.