Deploy a Node.js application on AWS EC2 using Terraform for infrastructure and Ansible for application deployment.
Note: The Node.js application code is in a separate repository and will be automatically cloned during deployment.
- Terraform installed
- Ansible installed
- AWS account with configured credentials
- AWS key pair (.pem file)
- Git installed
git clone https://github.com/<username>/nodejs-app-deployment.git
cd nodejs-app-deployment
Edit ansible/inventory.ini
:
[ec2]
ubuntu@<PUBLIC_IP> ansible_ssh_private_key_file=/path/to/your/key.pem ansible_ssh_common_args='-o StrictHostKeyChecking=no'
cd terraform
terraform init
terraform plan
terraform apply
Type yes
when prompted.
cd ../ansible
ansible-playbook -i inventory.ini playbook.yml
Navigate to: http://<PUBLIC_IP>:3007
nodejs-app-deployment/
├── terraform/ # Infrastructure code
├── ansible/ # Configuration & deployment
│ ├── inventory.ini # Server details
│ └── playbook.yml # Deployment tasks
└── README.md
Update variables directly in your Terraform files as needed for your specific AWS setup.
SSH Connection Issues:
- Check .pem file path in inventory.ini
- Set correct permissions:
chmod 400 your-key.pem
- Verify EC2 instance is running
Terraform Errors:
- Confirm AWS credentials are configured
- Ensure key pair exists in specified region
- Check AWS permissions
Application Not Loading:
- Verify security group allows port 3007
- Check public IP address
- SSH to instance and verify app status
cd terraform
terraform destroy
Action | Command |
---|---|
Deploy infrastructure | terraform apply |
Deploy application | ansible-playbook -i inventory.ini playbook.yml |
Destroy infrastructure | terraform destroy |
Debug Ansible | ansible-playbook -i inventory.ini playbook.yml -vvv |