Often it is useful to be able to upload custom scripts to the server and run them. One way to do this is first to upload the scripts to s3 and then download them down to the server as part of the user-data script. Lono supports this deployment flow with the
app/scripts folder. Any scripts added to the
app/scripts folder get tarballed up and uploaded to the lono s3 bucket.
Lono provides a extract_scripts helper that you can include your
user_data scripts to extract the
app/scripts files in your lono project to
/opt/scripts on the server. Here’s an example:
#!/bin/bash -exu <%= extract_scripts(to: "/opt") %> SCRIPTS=/opt/scripts $SCRIPTS/install_stuff.sh
In order to use extract_scripts, you’ll need scripts in the
app/scripts folder. We’ll add a test script:
cat >app/scripts/install_stuff.sh <<EOL yum install -y jq EOL
lono user_data command
app/user_data scripts are embedded in your CloudFormation templates with the
user_data helper method. You can see the generated script with lono generate and looking at the template in the
lono user_data command is also provided so you can see the code that
extract_script helper produces.
Here’s an example:
$ lono user_data bootstrap Detected app/scripts Tarballing app/scripts folder to scripts.tgz => cd app && dot_clean . => cd app && tar -c scripts | gzip -n > scripts.tgz Tarball created at output/scripts/scripts-93b8b29b.tgz Generating user_data for 'bootstrap' at ./app/user_data/bootstrap.sh #!/bin/bash -exu # Generated from the lono extract_scripts helper. # Downloads scripts from s3, extract them, and setup. mkdir -p /opt aws s3 cp s3://mybucket/path/to/folder/development/scripts/scripts-93b8b29b.tgz /opt/ cd /opt tar zxf /opt/scripts-93b8b29b.tgz chown -R ec2-user:ec2-user /opt/scripts SCRIPTS=/opt/scripts $SCRIPTS/install_stuff.sh $
Notice, that the name of the scripts tarball includes an md5 checksum. Lono first generates a
scripts.tgz, computes the file’s md5sum and then renames it to include the md5sum. There’s a very good reason for this.
Whenever you make changes in your
app/scripts folder and update your CloudFormation templates, CloudFormation does not see the changes. If the same
scripts.tgz s3 url were used then CloudFormation would not know that it needed to update the EC2 instance that uses the user-data script. By including the md5 checksum in the file name, this changes the user-data script, and this lets CloudFormation know that the scripts have changed.
Pro tip: Use the <- and -> arrow keys to move back and forward.