Skip to content

Instantly share code, notes, and snippets.

@pindlebot
Created October 10, 2018 21:53
Show Gist options
  • Select an option

  • Save pindlebot/e54651bc2c1c98749935cd6bcd0ccd3c to your computer and use it in GitHub Desktop.

Select an option

Save pindlebot/e54651bc2c1c98749935cd6bcd0ccd3c to your computer and use it in GitHub Desktop.
#!/usr/bin/bash env
export AWS_DEFAULT_REGION=us-east-1
SG="ec2-webserver-$(openssl rand 3 -hex)"
DEFAULT_SG=$(aws ec2 describe-security-groups --group-name default | jq -r '.SecurityGroups[0].GroupId')
SUBNET_ID=$(aws ec2 describe-subnets | jq -r '.Subnets[0].SubnetId')
KEY_NAME=$(aws ec2 describe-key-pairs | jq -r '.KeyPairs[0].KeyName')
INSTANCE_TYPE=t2.micro
IMAGE_ID=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameters[0].[Value]' --output text)
SG_ID=$(aws ec2 create-security-group --group-name $SG --description $SG | jq -r '.GroupId')
aws ec2 authorize-security-group-ingress --group-name $SG --protocol tcp --port 22 --cidr '0.0.0.0/0'
aws ec2 authorize-security-group-ingress --group-name $SG --protocol tcp --port 80 --cidr '0.0.0.0/0'
aws ec2 authorize-security-group-ingress --group-name $SG --protocol tcp --port 443 --cidr '0.0.0.0/0'
aws ec2 run-instances --image-id $IMAGE_ID \
--instance-type $INSTANCE_TYPE \
--key-name $KEY_NAME \
--subnet-id $SUBNET_ID \
--user-data file://$PWD/user-data.sh \
--security-group-ids "$SG_ID" "$DEFAULT_SG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment