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.
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.
Then copy the following configuration in the the file created above.
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.
Scroll down to the bottom after clicking on launch instances in AWS EC2 dashboard to find user data option.
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.
Important Links
Keep reading
API Security
3 minutes
What is API Discovery?
API Discovery helps identify, map, and manage APIs within an organization, ensuring security, performance, and seamless integration across systems.
API Security
5 minutes
Top 10 DAST Tools in 2024
DAST tools secure web apps by identifying vulnerabilities through automated security testing.
API Security
8 minutes
Security Information and Event Management (SIEM)
SIEM aggregates and analyzes security data across an organization to detect, monitor, and respond to potential threats in real time.
Experience enterprise-grade API Security solution