AWS VM Manager is a personal project designed to automate the creation and management of EC2 instances for experimentation purposes. It uses AWS CDK (Cloud Development Kit) with TypeScript to define and deploy the infrastructure as code.
- Motivation
- Features
- Prerequisites
- Setup
- Usage
- Project Structure
- Troubleshooting
- Contributing
- License
I frequently spin up EC2 instances for personal testing, requiring security groups to allow inbound traffic from my network's public IP. This project automates this setup process while providing an opportunity to learn and implement CDK.
- Generates a new SSH key and creates a new key pair
- Creates EC2 instances using the new key pair
- Sets up a Security Group to allow inbound traffic from your router's public IP address
- Lists EC2 instances' SSH configurations
- Generates an SSH config file for easy access
- Displays instance status (running, stopped, etc.)
- Provides a formatted table with instance details
Before you begin, ensure you have the following installed:
- Node.js and npm: Download and install Node.js
- AWS CLI: Install the AWS CLI
- AWS CDK: Install the AWS CDK toolkit globally using npm:
npm install -g aws-cdk
git clone <repository-url>
cd aws-vm-manager
npm install
Update the config/instances.yaml
file with the following content:
aws:
account: "" # your aws account id here
region: "us-east-1"
keyPair:
name: "MyKeyPair"
path: "/path/to/my/keypair.pem"
instances:
- name: "one"
type: "t2.micro"
- name: "two"
type: "t2.micro"
Replace /path/to/my/keypair.pem
with the actual path where you want to store your key pair file.
cdk deploy
This script fetches your laptop's public IP dynamically and uses it to configure the security group rules.
Destroy the stack to remove all resources:
cdk destroy
sh scripts/list.sh
This will print the following information and generate a ssh config file to use with vscode or Ansible.
Instance Name Public IP Status SSH Command
----------------------------------------------------------------------------------------------------
master <ip_address> running ssh -i <path_to_pem_file> ubuntu@<ip_address>
worker1 <ip_address> running ssh -i <path_to_pem_file> ubuntu@<ip_address>
SSH config file generated at ./ssh_config
bin/aws-vm-manager.ts
: Entry point for the CDK application.lib/aws-vm-manager-stack.ts
: Main stack definition.lib/key-pair.ts
: Key pair creation and management.lib/vpc.ts
: VPC configuration.lib/security-group.ts
: Security group configuration.lib/ec2.ts
: EC2 instances configuration.config/instances.yaml
: Configuration file for EC2 instances and key pair.
Public IP not fetched: Ensure you have an active internet connection and that the service http://checkip.amazonaws.com/ is accessible. AWS CLI not configured: Ensure your AWS CLI is configured with the necessary credentials and region. Logs and Debugging: Check the terminal output for any errors during deployment. AWS CloudFormation console can also provide detailed logs for stack events and resource creation issues.
If you wish to contribute to this project, please create a pull request or open an issue on the repository.
This project is licensed under the MIT License.