Products

Solutions

Resources

Engineering

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

Mar 17, 2023

2 min read

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.

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.

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.

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.

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.

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.

Share this post

Share this post

Share this post

Monthly product updates in your inbox. No spam.