Setup an AWS EC2 instance
Local machine environment
Local environment might matter for some shell commands
- Windows 10 Pro
- OS Build 19042.928
- Software installed: WSL 2
- Software installed: Ubuntu 20.x
Setup steps
All commands mentioned in the guide are tried on ubuntu shell
1. Setup AWS Account
- Create an AWS account
- AWS Console > Create an AWS Account
- User needs an existing email ID
- Debit or credit card details will be needed to setup an account
2. Create an IAM user
- Add an user
- It is used by AWS CLI
- It is created because root user should not be used for security purposes.
- AWS Console > IAM > Users > Add user
- Provide a
name
- Check
Programmatic access
- Keep
AWS Management Console access
unchecked - Attach existing policy with
AdministratorAccess
(for starters; in longer run, provide only required access) - On user creation,
Access key id
andsecret access key
are created. Save these locally for reference
- Provide a
3. Setup AWS CLI
- Install AWS CLI on local
- execute following command
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install
- execute following command
- Configure AWS CLI
- execute following command
aws configure
- It is an interactive command. When prompted provide ID and secret from previous step. Optionally, provide default region. I used
ap-south-1
AWS Access Key ID [None]: <access key id> AWS Secret Access Key [None]: <secret access key> Default region name [None]: <region> Default output format [None]:
- execute following command
4. Setup EC2 Instance
- Create key pair
- I created a key pair locally so that AWS does not know my private key
- This key pair is used to login to EC2 instances on AWS.
- Using this approach user name and password are not asked.
- execute following command
ssh-keygen -t rsa -b 4096 -f ~/.ssh/local-ec2-key
- additional inputs (I did not provide any paraphrase.)
Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again:
- Output
Your identification has been saved in /home/sunilg/.ssh/local-ec2-key Your public key has been saved in /home/sunilg/.ssh/local-ec2-key.pub The key fingerprint is: SHA256:dummysshkeymentionedhereformdeo usernamedummy@pcnamedummy
- Import public key in AWS
- execute following command. Change region based on where you need to setup EC2.
aws ec2 import-key-pair --key-name "ec2-key" --public-key-material fileb://~/.ssh/local-ec2-key.pub --region ap-south-1
~/.ssh/local-ec2-key.pub
is location of my public key that I created in last step~
points to/home/username
- with
WSL
it was accessible on\\wsl$\Ubuntu\home\username\.ssh\local-ec2-key.pub
from windows explorer
- execute following command. Change region based on where you need to setup EC2.
- Create EC2 instance
- In this example, I have used t3a.large (2 vCPU, 8 GB RAM) and 32 GB volume storage. This is because I procured this EC2 instance for running minikube that needs this much resources. One can choose different configuration based on the requirement**
- Validate AMI IDs before using; these change based on OS and regions etc.
- I also created a tag for assigning VM a name (
vm-mk
) for easy reference. - Note: tags / VM names can have duplicate values
- execute following command
aws ec2 run-instances \ --image-id ami-0d758c1134823146a \ --instance-type t3a.large \ --key-name ec2-key --region ap-south-1 \ --block-device-mappings Ebs={VolumeSize=32},DeviceName=/dev/sda1 \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=vm-mk}]'
- It creates a machine with an instance Id
- Update EC2 instance security group settings [pending - AWS CLI approach]
- AWS Console > EC2 > Instance id > Security Group > Inbound rules
- Select
all traffic
undertype
- Add
my ip
underSource
- Save rules
- Get Public IP Address by instance ID (or by name tag as mentioned in next step)
- replace
instance_id
text with currently created instance idaws ec2 describe-instances --instance-ids instance_id \ --query "Reservations[*].Instances[*].PublicIpAddress" \ --output=text
- replace
- Get Public IP Address by Name tag (not required if IP address was checked using previous step)
- replace
vm-mk
text with the tag assigned to currently created EC2 instanceaws ec2 describe-instances \ --filters Name=tag:Name,Values=vm-mk \ --query "Reservations[*].Instances[*].PublicIpAddress" \ --output=text
- replace
5. Login to EC2 instance
- Connect to AWS EC2 instance
- execute following command
ssh -i <private key file path> ubuntu@<public ip address>
- execute following command
6. Maintain EC2 Instance
- Check status of an EC2 instance
aws ec2 describe-instance-status --instance-ids my-instance-id
- Start an EC2 instance
aws ec2 start-instances --instance-ids my-instance-id
- Stop an EC2 instance
aws ec2 stop-instances --instance-ids my-instance-id
7. Assign Static IP to EC2 instance (Optional)
There is an additonal cost associated with elastic IP EC2 On-Demand Pricing
- Allocate elastic IP
- A new public IP address is assigned to EC2 instance on restart
- This can help if one needs to access the machine with same IP
- execute following command (it provides a static IP)
aws ec2 allocate-address --domain vpc --network-border-group ap-south-1
- This IP address will add to bill even when EC2 instance is down
- Associate IP address with EC2 instance
- execute following command
aws ec2 associate-address --instance-id <ec2-istanceid> --public-ip <elastic ip address>
- Post this association, one can access the EC2 instance using this IP address
- execute following command
Notes
- For each account created on AWS, a
VPC
is created - Each VPC has
three subnets
forthree availability zones
in aregion
- Each VPC subnet has a
IPv4 CIDR block
that tells how many IP addresses are available. In total, around65,536
IP addresses are available Security Groups
are likefirewall
Yet to try
- Configure
AWS profile
(on local machine) to access different accounts? - Check approach to
set security settings of EC2 instance through AWS CLI
References
Inspired by a k8s introductory session by Vijay Dharap. Some steps and commands are added based on my personal experiment.