Creating an EC2 instance using CloudFormation
- You will need the AWSCLI configured with the reqion set to eu-west-2
- jq installed (sudo apt install -y jq)
-
Clone the repo and cd into it
git clone https://github.com/mrbilalshafiq/CloudFormation-EC2-Instance && cd CloudFormation-EC2-Instance
-
From here you could enter the command below to execute the script and automate the process or continue entering the rest of the commands manually
bash script.sh
-
Make sure SSH folder exists with the correct permissions
mkdir -p ~/.ssh && chmod 700 $_ -
Set the key pair name
key_name="CloudFormationKeyPair" -
Create a key pair
aws ec2 create-key-pair --key-name ${key_name} --query 'KeyMaterial' --output text > ~/.ssh/${key_name}.pem -
Make sure the private key has the correct permissions
chmod 600 ~/.ssh/${key_name}.pem -
Create the stack using the template provided
aws cloudformation create-stack --stack-name test-stack --template-body file://test-stack.yaml -
Wait until it's ready, you can either use this command to programatically wait or just look on the AWS Management Console to check
aws cloudformation wait stack-create-complete --stack-name test-stack -
Get the ID of the newly created instance
instance_id=$(aws cloudformation describe-stack-resources --stack-name test-stack | jq -r '.StackResources[] | select(.StackName == "test-stack" and .ResourceType == "AWS::EC2::Instance") | .PhysicalResourceId') -
Get the DNS name of the instance using the instance ID
dns_name=$(aws ec2 describe-instances | jq -r --arg instance_id ${instance_id} '.Reservations[] | .Instances[] | select(.InstanceId == $instance_id) | .PublicDnsName') -
Add instances to known hosts to avoid the yes/no prompt when connecting for the first time
ssh-keyscan -H ${dns_name} >> ~/.ssh/known_hosts -
Finally, connect with SSH using the private key
ssh -i ~/.ssh/CloudFormationKeyPair.pem ubuntu@${dns_name}
-
Delete the stack
aws cloudformation delete-stack --stack-name test-stack -
Delete the key pair
aws ec2 delete-key-pair --key-name ${key_name} -
Remove the private key
rm ~/.ssh/${key_name}.pem