Amazon EC2
aws ec2 run-instances
--image-id ami-12345678
--instance-type t3.micro
--user-data file://script.yamlSets up Node.js with PM2 process manager for production applications
#cloud-config
package_update: true
package_upgrade: true
packages:
- curl
- git
- build-essential
write_files:
- path: /etc/systemd/system/nodeapp.service
content: |
[Unit]
Description=Node.js App
After=network.target
[Service]
Type=simple
User=nodeapp
WorkingDirectory=/opt/nodeapp
ExecStart=/usr/bin/node app.js
Restart=on-failure
RestartSec=10
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
users:
- name: nodeapp
system: true
shell: /bin/false
home: /opt/nodeapp
runcmd:
# Install Node.js 18.x
- curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
- apt-get install -y nodejs
# Install PM2 globally
- npm install -g pm2
# Create app directory
- mkdir -p /opt/nodeapp
- chown nodeapp:nodeapp /opt/nodeapp
# Setup PM2 startup script
- pm2 startup systemd -u nodeapp --hp /opt/nodeappaws ec2 run-instances
--image-id ami-12345678
--instance-type t3.micro
--user-data file://script.yamldoctl compute droplet create
--image ubuntu-22-04-x64
--size s-1vcpu-1gb
--user-data-file script.yaml
my-dropletgcloud compute instances create
my-instance
--metadata-from-file
user-data=script.yaml