Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

CloudFormation-EC2-Instance

Creating an EC2 instance using CloudFormation

Prerequisites

  • You will need the AWSCLI configured with the reqion set to eu-west-2
  • jq installed (sudo apt install -y jq)

Step-by-Step Guide

  1. 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
    
  1. Make sure SSH folder exists with the correct permissions

     mkdir -p ~/.ssh && chmod 700 $_
    
  2. Set the key pair name

     key_name="CloudFormationKeyPair"
    
  3. Create a key pair

     aws ec2 create-key-pair --key-name ${key_name} --query 'KeyMaterial' --output text > ~/.ssh/${key_name}.pem
    
  4. Make sure the private key has the correct permissions

     chmod 600 ~/.ssh/${key_name}.pem
    
  5. Create the stack using the template provided

     aws cloudformation create-stack --stack-name test-stack --template-body file://test-stack.yaml
    
  6. 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
    
  7. 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') 
    
  8. 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')
    
  9. 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
    
  10. Finally, connect with SSH using the private key

    ssh -i ~/.ssh/CloudFormationKeyPair.pem ubuntu@${dns_name}
    

Clean up

  1. Delete the stack

     aws cloudformation delete-stack --stack-name test-stack
    
  2. Delete the key pair

     aws ec2 delete-key-pair --key-name ${key_name}
    
  3. Remove the private key

     rm ~/.ssh/${key_name}.pem
    

About

Creating an EC2 instance using CloudFormation

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages