Search code examples
amazon-web-servicesamazon-ec2sshgithub-actionsterraform-provider-aws

Unable to figure out the reason for connection refused while trying to ssh into an ec2 instance using git action


I have created an EC2 instance using Terraform script and then I am using a git action script to deploy my application by ssh into the instance. Below is the git action script.

- name: Deploy Streamlit to EC2
        run: |
          echo "Deploying Streamlit application..."
          mkdir -p ~/.ssh
          echo "$EC2_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
          chmod 400 ~/.ssh/id_rsa
          ssh -o "StrictHostKeyChecking=no" -i ~/.ssh/id_rsa ec2-user@$instance_public_ip << 'EOF'
          sudo apt-get update
          sudo apt-get install -y python3-pip
          ** Code to deploy my app **
          EOF
        env:
          EC2_SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
          instance_public_ip: ${{ env.instance_public_ip }}

The error generated is : Pseudo-terminal will not be allocated because stdin is not a terminal. ssh: connect to host ... port 22: Connection refused

The EC2 instance is being created using an existing key pair from AWS. I have stored the private key in GitHub secret. I have updated the security groups to allow SSH on port 22. VPC and routing tables have been set correctly.

I have double checked the instance IP it is correct. I am able to connect to the instance using the same key value pair locally. Its just the issue that is showing up in git action.

I am unable to figure out what is going wrong ?


Solution

  • The issue has been resolved. I understood that as soon as I create an EC2 instance it takes some time for SSH to be initialized. We can't directly start SSH-ing it. So I added a sleep process for one minute and it worked.