Skip to content

Instantly share code, notes, and snippets.

@rameerez
Last active May 3, 2023 17:28
Show Gist options
  • Save rameerez/a9fa4e78bebe7caf91fced41d781d60f to your computer and use it in GitHub Desktop.
Save rameerez/a9fa4e78bebe7caf91fced41d781d60f to your computer and use it in GitHub Desktop.
User data to perform the initial set up of an AWS EC2 machine after running the AMI script
#!/bin/bash
# This scripts performs the initial setup for an EC2 AMI that has
# been previously prepared to accept Rails apps.
# --- USER DATA SCRIPT STARTS ---
# Configure SSH key for the rails user (same as ubuntu, AWS configured key)
sudo mkdir /home/rails/.ssh
sudo cp /home/ubuntu/.ssh/authorized_keys /home/rails/.ssh
sudo chown -R rails:rails /home/rails/.ssh
sudo chmod 700 /home/rails/.ssh
sudo chmod 600 /home/rails/.ssh/authorized_keys
# Copy Github SSH credentials so Capistrano is able to pull code from private Github repos
cat << EOF > /home/rails/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
<REDACTED>
-----END OPENSSH PRIVATE KEY-----
EOF
cat << EOF > /home/rails/.ssh/id_rsa.pub
ssh-rsa <REDACTED>
<REDACTED> <redacted-email>@users.noreply.github.com
EOF
# Set up the right permissions to the ssh folder
sudo chown -R rails:rails /home/rails/.ssh
sudo chmod 700 /home/rails/.ssh
sudo chmod 600 /home/rails/.ssh/authorized_keys
sudo chmod 600 /home/rails/.ssh/id_rsa
sudo chmod 600 /home/rails/.ssh/id_rsa.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment