Hello Readers! We are back again. And In this blog we will learn how we can launch an EC2 instance in AWS using Docker-Machine in a few easiest steps. We have some earlier blogs on EC2 instance which we can refer to are: https://blog.knoldus.com/how-to-launch-ec2-instance-from-aws-cli/ & https://blog.knoldus.com/how-to-create-ec2-instance-using-python3-with-boto3/
So, before starting we again have some prerequisites that we need to follow. These are the following:
- You must have configured AWS CLI. Check it by this command.
$ aws configure
- You must have docker-machine installed on your system. If you hadn’t and want to install it then follow these steps for installing:
$ curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` -o ~/docker-machine
$ chmod +x ~/docker-machine
$ sudo cp ~/docker-machine /usr/local/bin/
It’s done and now you can check the docker-machine version.
$ docker-machine –version
$ which docker-machine
Currently we don’t have any machines. You can check it by using this command.
$ docker-machine ls
Creating EC2 Instance:
We have successfully installed docker-machine on our system. We will now launch an EC2 instance using docker-machine by a single command. In this command I will be using driver amazonec2 for creating it.
If you want to look at the options which you can pass by using this driver then use this command:
$ docker-machine create --driver amazonec2 --help | less
I have logged into my AWS Console and you can see here I dont have any EC2 right now.
Lets launch the instance. Use the following command for launching.
$ docker-machine create --driver amazonec2 --amazonec2-instance-type "<TYPE>" --amazonec2-region "<REGION>" <INSTANCE_NAME>
Here I am using instance type as t2.micro which is free tier eligible. And I am creating the instance in the ap-south-1 region which is mumbai.This command will take some time to create the instance. It will launch a security group, a new key pair and an EC2 instance.
It’s done now. Lets move to the AWS console and check if an instance got created.
Yaa its creating and one instance is up and running. You can see it here also.
You can see it here also by listing docker-machine.
$ docker-machine ls
It has created a key pair on the name of ec2 instance.
It has created a security group you can see here.
Connect to the EC2 Instance:
We can easily ssh into the instance by only a simple command. Use the following command for this.
$ docker-machine ssh aws1
Now, we are inside our EC2 instance running in AWS.
Terminate the EC2 Instance:
If you want to terminate your running instance in AWS then you use the following command:
$ docker-machine rm aws1
You can see the result here. The instance aws1 got terminated.
It will delete all the resources that it has created.
Congrats! 👏 We are successfully done now!
Conclusion
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.
HAPPY LEARNING!