Products

Solutions

Resources

Search for API Security Tests

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.

In this blog, you will learn how to run bash commands on AWS EC2 instance restart.

bash-command-on-AWS
bash-command-on-AWS
bash-command-on-AWS

Shivansh Agrawal

2 min read

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.

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.

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.

Monthly product updates in your inbox. No spam.

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.

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.

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.

Share this post

Share this post

Share this post

Monthly product updates in your inbox. No spam.