Unable to ssh inside ec2 instance (AWS-Linux) from codebuild (using buildspec)

23 views Asked by At

I want to ssh inside my ec2 instance from my buildspec to run 2 bash scripts inside my server but am getting:

Running command ssh -i ~/.ssh/xx_key ec2-user@$EC2_HOST "cd /var/www/xx-api && ./xx-api-deploy.sh"
Warning: Permanently added '13.aa.xx.yy' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[Container] Command did not exit successfully ssh -i ~/.ssh/xx_key ec2-user@$EC2_HOST "cd /var/www/xx-api && ./xx-api-deploy.sh" exit status 255

This is my current buildspec:

phases:

    commands:
      - export EC2_ACCESS_KEY=$EC2_ACCESS_KEY_PARAM
      - echo "$EC2_ACCESS_KEY" > ~/.ssh/xx_key
      - chmod 700 ~/.ssh
      - chmod 400 ~/.ssh/xx_key
      - echo -e "Host $EC2_HOST\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config

  build:

    commands:
      - ssh -i ~/.ssh/xx_key ec2-user@$EC2_HOST "cd /var/www/xx-api && ./xx-api-deploy.sh"
      - ssh -i ~/.ssh/xx_key ec2-user@$EC2_HOST "cd /var/www/xx-api && ./yy-api-deploy.sh"

I can access my server locally and have made plenty of changes inside it so it is not a problem of access via port 22. I have also changed permissions in my authorized_keys inside /home/[user]/.ssh/authorized_keys in my server such as: PermitRootLogin no, PubkeyAuthentication yes, GSSAPIAuthentication yes, GSSAPICleanupCredentials no, UsePAM yes, PasswordAuthentication yes and finally chmod 0600 /home/[username]/.ssh/authorized_keys and chmod 0700 /home/your_home/.ssh

0

There are 0 answers