<p>I would look at <a href="https://docs.aws.amazon.com/systems-manager/" rel="nofollow noreferrer">AWS System Manager </a> to do this instead of direct ssh. Create a SSM document to run the commands you want to run, then this can be invoked by an AWS cli command from your CI/CD pipeline.</p>
<p>We do something the same to update configuration files from an s3 bucket, run as part of a Gitlab pipeline.</p>

CC BY-SA 4.0
2021-06-20T08:35:43.880Z
Hi @Alastair Montgomery , thanks for suggestion to use SSM instead of SSH. I tried to invoke the deploy.sh placed under my ec2 instance by using the `runshellscript` command, but its not able to run npm commands, tried using `runasuser`. Any pointer to fix this, thanks!
Mukul Kumar Jha
CC BY-SA 4.0
2021-06-20T21:23:07.763Z
Might be something to do with the environment it's getting with SSH or SSM is running the script. For example might not have npm on the path. Try referencing it using the commands full path in your script.
Alastair Montgomery
CC BY-SA 4.0
2021-06-19T10:05:26.437Z
2021-06-19T10:05:26.437Z
Alastair Montgomery
<p>I have a simple node application running on the EC2 instance, everything is working just fine and I want to integrate this application with CircleCI for the continuous deployment. I have been following this <a href="https://medium.sayadi.io/how-to-ssh-to-an-aws-ec2-instance-running-linux-from-a-circleci-build-fffa45003715" rel="nofollow noreferrer">article</a> where it says to SSH to the EC2 instances and then run your deployment commands.</p>
<p>I've created a basic <code>deploy.sh</code> file in the EC2 instance, where I simply change the directory to project directory and then run <code>npm i</code> and <code>pm2 delete all</code> with <code>pm2 start server.js</code>. Now before testing it out my circleCI I thought of checking if it's working in general or not, so</p>
<ol>
<li>When I run this deploy.sh file from within EC2 instance, it works fine and follows all the steps to install <code>node_modules</code> and then serving the application with <code>pm2</code> correctly. So, no issues here.</li>
<li>From my local machine when I tried to run the same deploy.sh with this command <code>ssh -i &lt;my-pem-file&gt; &lt;ec2-user&gt;@&lt;ec2-ip&gt; \ ./deploy.sh</code>. it <code>echoes</code> the basic stuff but says <em><code>npm</code> and <code>pm2</code> : not found</em> . The reason this is happening is because that deploy.sh file is being run in context of my local machine (or not?).</li>
</ol>
<p>So how can I simply run this <code>deploy.sh</code> file right after I SSH to my EC2 instance from circleCI, or If there is some better approach to do this all together.</p>

<amazon-ec2><continuous-deployment><bash><ssh><circleci-2.0>
Setting up CircleCI to AWS EC2 for a simple node application deployment
Mukul Kumar Jha
# Setting up CircleCI to AWS EC2 for a simple node application deployment

(Asked by: username_0 on 2021-06-17)

I have a simple node application running on the EC2 instance, everything is working just fine and I want to integrate this application with CircleCI for the continuous deployment. I have been following this [article](https://medium.sayadi.io/how-to-ssh-to-an-aws-ec2-instance-running-linux-from-a-circleci-build-fffa45003715) where it says to SSH to the EC2 instances and then run your deployment commands.

I've created a basic `deploy.sh` file in the EC2 instance, where I simply change the directory to project directory and then run `npm i` and `pm2 delete all` with `pm2 start server.js`. Now before testing it out my circleCI I thought of checking if it's working in general or not, so

1. When I run this deploy.sh file from within EC2 instance, it works fine and follows all the steps to install `node_modules` and then serving the application with `pm2` correctly. So, no issues here.
2. From my local machine when I tried to run the same deploy.sh with this command `ssh -i  @ \ ./deploy.sh`. it `echoes` the basic stuff but says *`npm` and `pm2` : not found* . The reason this is happening is because that deploy.sh file is being run in context of my local machine (or not?).

So how can I simply run this `deploy.sh` file right after I SSH to my EC2 instance from circleCI, or If there is some better approach to do this all together.

**Tags:** amazon-ec2, continuous-deployment, bash, ssh, circleci-2.0


---

## 1 answers

### Answer by username_1 on 2021-06-19. Score: 1

I would look at [AWS System Manager](https://docs.aws.amazon.com/systems-manager/)  to do this instead of direct ssh. Create a SSM document to run the commands you want to run, then this can be invoked by an AWS cli command from your CI/CD pipeline.

We do something the same to update configuration files from an s3 bucket, run as part of a Gitlab pipeline.

> **Comments:**
> **username_0**: Hi @username_1 , thanks for suggestion to use SSM instead of SSH. I tried to invoke the deploy.sh placed under my ec2 instance by using the `runshellscript` command, but its not able to run npm commands, tried using `runasuser`. Any pointer to fix this, thanks!  
> **username_1**: Might be something to do with the environment it's getting with SSH or SSM is running the script. For example might not have npm on the path. Try referencing it using the commands full path in your script.  

---

