How to run bash commands on AWS EC2 instance restart ?
In this blog, you will learn how to run bash commands on AWS EC2 instance restart.
![](https://framerusercontent.com/images/kO9LVXO7Ia8dHpJu7PjfXa1h80.png)
Shivansh Agrawal
2 min read
Bash Commands for EC2: Ensuring Persistent Settings with User Data Scripts
Amazon EC2 instances are great for many kinds of workloads. Amazon provides the option to run bash scripts at instance start using user data shell scripts. User data shell scripts can be used for a myriad of things like, downloading GitHub repos, setting up docker containers etc. By default, user data scripts are run only once, when creating the EC2 instance.
Generally, we don’t restart EC2 instances but in the event we do, it becomes important to configure the EC2 instance to its previous state, which is often set using user data shell scripts in the first place.
Interestingly, AWS provides the option to modify cloud-init directives, which can be configured to run the user data script every time the EC2 instance is restarted. To configure them, ssh into the EC2 instance. Then go to /etc/cloud/cloud.cfg.d directory and create a cloud-config.cfg file.
![](https://framerusercontent.com/images/bBANkm8u9UlYs57Huw3pP8mdShQ.png)
Then copy the following configuration in the the file created above.
![](https://framerusercontent.com/images/QUjzQRtWnNMjhLI9ld76FDjs5ZE.png)
And that’s it. You’re good to go. This will make sure that the user data scripts are run every time the EC2 instance is restarted.
Alternatively, you can add the following script to the user data itself, at the time of creating the EC2 instance, to achieve the same result.
![](https://framerusercontent.com/images/FhxqBsPOfEtBfZ674ylxEKMiqo.png)
Scroll down to the bottom after clicking on launch instances in AWS EC2 dashboard to find user data option.
![](https://framerusercontent.com/images/DMaUgs6TppM4IfL0oPlFYjJRI.png)
AWS continues to expand and innovate, offering new services and features regularly to meet various cloud computing and infrastructure needs. If your application services are hosted in AWS, follow this link to install Akto in your AWS cloud or this link to setup Akto in AWS multi-VPC environment. You can also create a custom subdomain on Akto on AWS by following the steps here.
You can also check out how SSRF attack could potentially be used to steal AWS EC2 tokens from the server, a learning from the Lego Marketplace Hack.
Thanks folks for reading this. This is our first Akto engineering blog. Show us some love by staring our GitHub repo here if you like our blogs. It motivates us to write more for the community.
Keep reading
API Security
3 minutes
Twilio Data Breach: 33 Million Authy User Phone Numbers Exposed by Hacker
Twilio's data breach exposed 33 million Authy user phone numbers because of an unauthenticated endpoint.
API Security
10 minutes
Dynamic White Box Testing Guide - Key Features, Levels and Examples
Dynamic White Box Testing is a strategy in which the tester is aware of the internal structure of the application under test.
API Security
8 minutes
DAST Black Box Testing: Types of Black Box Testing and How it works
Black Box Testing is a methodology where the internal workings of the system under test are unknown to the tester.