EC2 user data script example

Some time ago I wrote about Linux agent from CloudBerry Lab. This time I want to pack all together and show advanced capabilities of AWS.

When we launch AWS EC2 (compute instance in the cloud) we may want to have something pre-installed (or pre-done) before we get into this VM and start working. Let me give you an example. I do launch instances with Linux OS quite often for various reason (customer’s demo, testing applications, testing software etc) and I want to have number of steps without my intervention after launch. To be precise I need my software installed when I SSH into my either CentOS or Debian. Think wide now, don’t be locked in my software. Use anything you’d love to have. Well this is good article on how to have PHP installed. In my article I’d like to have CBL for Linux installed with storage configured and backup plan scheduled. Sound cool, right? Let’s do it.

Command Line steps

We just need the following things:

  • Update system and Linux distributive (well, this is optionally, but I still prefer to do this since I want to make sure I have all security patches and fixes applied to my system)l
  • Change directory my ~ (I will do Ubuntu 16.04 out of marketplace of AMI), so I will be working with ubuntu user-name;
  • curl binaries from vendor’s website;
  • Install using package manage (dpkg);
  • Setup storage account (using software’s CLI);
  • Add default backup plan (same as above, CLI) with schedule.

Sounds clear. Here is my script example for AWS EC2 user data:

#!/bin/bash
sudo su
passwd ubuntu << EOF
Pa$$w0rd
Pa$$w0rd
EOF
apt-get update -y
cd ~
curl -# -o cloudberry-backup.deb \
	-F "__EVENTTARGET=" \
	-F "prod=cbbub1214" \
	https://www.cloudberrylab.com/download-thanks.aspx
dpkg -i cloudberry-backup.deb

Let’s take a look at the above snippet closer. We elevate permissions to root and change password for our default user ubuntu to Pa$$w0rd. We then proceed with system updates and jump into our home directory for binaries download. My curl command gets binaries from CloudBerry Lab website. Finally I install my package using dpkg default package manager! This should be enough to have CBL Backup for Linux installed when EC2 launched. In order to manage it through CLI change your directory to cd /opt/local/CloudBerry Backup/bin/ and pick any of the below attributes:

./cbb
CloudBerry Backup Command Line Interface started
CloudBerry Backup Commands Help:

 addBackupPlan      - Create a new plan to backup files to cloud storage
 addRestorePlan     - Create a new plan to restore files from cloud storage
 editBackupPlan     - Edit an existing backup plan
 editRestorePlan    - Edit an existing resetore plan
 getPlanDetails     - Get plan details
 plan               - Run custom plan now | list plans
 changePlanState    - Change state
 deletePlan         - Delete plan

 addAccount         - Add backup destination storage
 editAccount        - Edit account
 deleteAccount      - Delete account
 account            - Account list
 option             - Set default option

 sendLog            - Send Error Report

 saveLog            - Save Error Report


 exportConfig       - Export backup configuration to the specific file

 importConfig       - Import backup configuration from the file


 version            - Version

 activateLicense    - activate license
 releaseLicense     - release current license

Start with ./cbb activateLicense -e email -t "ultimate/pro" for example. This is trial license request, check official command line interface user guide for further details.

CloudBerry Backup web interface

With release of CBL for Linux 2.1 it comes with web interface what significantly simplifies product management. Make sure you have 43210 port available (check your security group for your EC2). You can change this port later in the UI. If it is available, open up your favorite browser and type IP-of-your-EC2:43210. This should bring you to the login page, where you should use your system user name and password (ubuntu and Pa$$w0rd).

Login page to CloudBerry Backup for Linux (web)

Now, if you’d like to change default ports to something else, check this part of options:

CloudBerry Backup for Linux - ports configuration for web

That’s it! Hope it helps someone with EC2 protection!

Appendix

If all above did not make anything, you may want to troubleshoot your script by checking logs (of course you need SSH into this instance). Logs are in /var/log/. This is what I usually do (since system update may take some time I have enough time to SSH and do tail):

tail -f /var/log/cloud-init-output.log

If you need the same for Windows, please do check this article.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy