Created
November 16, 2023 20:41
-
-
Save chadbrewbaker/99ed0c2c978c77175da7835301c7ca22 to your computer and use it in GitHub Desktop.
ChadCloudForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: create-keypair launch-instance connect-instance shutdown-instance clean log-details | |
# Variables | |
KEY_NAME = MyKeyPair | |
INSTANCE_TYPE = t2.micro | |
AMI_ID = ami-12345678 | |
SECURITY_GROUP_ID = sg-12345678 | |
SUBNET_ID = subnet-12345678 | |
KEY_FILE = $(KEY_NAME).pem | |
INSTANCE_IP = # Put your instance's public IP or DNS here | |
INSTANCE_ID = # Put your instance's ID here | |
LOG_FILE = ec2_instances.csv | |
# Create a new key pair | |
create-keypair: | |
@aws ec2 create-key-pair --key-name $(KEY_NAME) --query 'KeyMaterial' --output text > $(KEY_FILE) | |
@chmod 400 $(KEY_FILE) | |
@echo "Key pair created and saved to $(KEY_FILE)" | |
# Launch an EC2 instance and log details | |
launch-instance: create-keypair | |
@aws ec2 run-instances --image-id $(AMI_ID) --count 1 --instance-type $(INSTANCE_TYPE) --key-name $(KEY_NAME) --security-group-ids $(SECURITY_GROUP_ID) --subnet-id $(SUBNET_ID) | |
@echo "EC2 instance launched" | |
@$(MAKE) log-details | |
# Log details to a CSV file | |
log-details: | |
@echo "$(INSTANCE_ID),$(KEY_NAME),$(KEY_FILE)" >> $(LOG_FILE) | |
@echo "Logged instance details to $(LOG_FILE)" | |
# SSH into the EC2 instance | |
connect-instance: | |
ssh -i $(KEY_FILE) ec2-user@$(INSTANCE_IP) | |
@echo "Connected to EC2 instance" | |
# Terminate the EC2 instance | |
shutdown-instance: | |
@aws ec2 terminate-instances --instance-ids $(INSTANCE_ID) | |
@echo "EC2 instance terminated" | |
# Clean up resources | |
clean: shutdown-instance | |
@echo "Deleting the key pair..." | |
@aws ec2 delete-key-pair --key-name $(KEY_NAME) | |
@rm -f $(KEY_FILE) | |
@echo "Key pair deleted" | |
# Default target | |
all: launch-instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment