Connectins AWS via SSH

Hello everybody.
I’m trying to connect to AWS EC2 instance via SSH, but I don’t know what
I should write in circle.yml and in .sh file. You have example, how to
connect to EC2 instance via CodeDeploy, but there are no example how to
do this via SSH.Could you help me with this?

Update
After few hours of thinking and asking stackoverflow and Node.JS devs I found the solution.
It’s not very difficult. First, you should create SSH keypair on your computer and put private key to CircleCI Project Settings. Then you should add your public key to authorized_keys file on your EC2 instance(the file is in .ssh folder on EC2 with Ubuntu). Third, you should understand the main concept. You should archive your code, then put it on EC2 instance, then extract it and run.
You should create a folder with your .sh file (let it be “build” folder) in your github project. Then you should add a few lines to circle.yml file.
Example lines:
deployment:

    master:
        branch: master
        commands:
          - ../build/deploy.sh`

In deploy.sh file you should write something like this:

``
#You are now in build folder and you should go to folder, which contains your code. In my example the code is in folder "src" outside of the "build" directory.
cd ..

#Here you make an archive
tar -zcvf your_project_name.tar.gz src/

#Here you put your archive to EC2 via SSH
Remember that I have Ubuntu preinstalled on EC2 instance. You should write "ec2-user@" instead of "ubuntu@" if you have Amazon Linux instance
scp your_project_name.tar.gz ubuntu@your_instance_IP:/home/ubuntu

#Here you extract your archive on EC2 using SSH
ssh ubuntu@your_instance_IP tar -xvzf your_project_name.tar.gz

After all this actions you will have your project on your EC2 instance. But you should run it, so this is not the end. And here I have a question. How we can manage our instance? Should we write our commands like ssh ubuntu@your_instance_IP your_command or we can connect the instance once using this command ssh ubuntu@your_instance_IP and then write commands like this your_command?

1 Like

Either should work. I’ve never used AWS, but those are valid SSH methods (Although I’ve not used the the with with the command on the same line before)

Thanks, you are right. You could write your commands like ssh ubuntu@your_instance_IP command. I haven’t tried second variant. I think this could be good help for everyone, who is going to connect instance via SSH.

2 Likes