1

I want to remove all the files and folders inside the particular directory at /var/www/example directory using user data script at AWS instance launch. Can you please help me.

slava
  • 4,085

2 Answers2

0

From Amazon's Cloud Service website:


The following example shows how to specify a script using a text file. Be sure to use the file:// prefix to specify the file.

aws ec2 run-instances --image-id ami-abcd1234 --count 1 --instance-type m3.medium \
--key-name my-key-pair --subnet-id subnet-abcd1234 --security-group-ids sg-abcd1234 \
--user-data file://my_script.txt

The following is an example text file with a shell script.

#!/bin/bash
rm -rf /var/www/example
0

To delete files inside a folder during an AWS EC2 instance launch, you can use user data scripts. These scripts are executed automatically when the instance launches for the first time.

  1. Add User Data in EC2 Launch Configuration

Under Configure Instance Details, Scroll down to Advanced Details and paste the following script into the User data field.

#!/bin/bash
# Delete all files inside /var/www/example

rm -rf /var/www/example/*

This: . Keeps the folder . Deletes all files and subdirectories inside it

Optional: Confirm via Logs . You can check /var/log/cloud-init.log and /var/log/cloud-init-output.log to verify the script run as expected:

cat /var/log/cloud-init-output.log