Answers: <p>You want to make use of <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html" rel="nofollow noreferrer">user_data</a> scripts.</p>
  <p>It's basically a bootstrap shell script that gets executed on an instance during launch.</p>
  <p>You can embed your app configuration script in there (either directly via bash, or have it call your configuration management software like Ansible or Chef).</p>
  <p>Then, if you attach this script to a Launch Template or a Launch Configuration (which is an instance template required as part of an AWS ASG), it will be applied to the whole autoscaling group.</p>
  <p>Then, when you have a scaling event, any new instances that come up in the ASG will run the script and deploy your code.</p>
  <p>However, I would recommend you look into immutable infrastructure instead.  I.e. set up automation (i.e. via Packer) that builds your application code into your AMI, with user_data only used for actually starting the app.</p>
  <p>Then, when you have a new build, have the same automation build a new AMI and replace your ASG (note you'll need to have your ASG created/deployed in a programmatic fashion.. I personally recommend Terraform).</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-01-25T18:18:14.207Z
Answers: 2021-01-25T18:18:14.207Z
Answers: maplebird
Body: <p>I have just started reading about AWS services. I have a project where the backend is created with <code>Spring-Boot</code>, front-end is created with <code>React</code> and <code>PostgreSQL</code> is used as database. Both backend and frontend is different <code>bitbucket</code> repository. I want to use two different <code>EC2</code> instance for front &amp; back-end and <code>RDS</code> for <code>PostgreSQL</code>. Now I also want to use the autoscaling feature. The problem is how can I set the autoscaling feature in such a fashion where it automatically clone the app from bitbucket and then run a shell script file to compile and run the application.</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-01-25T10:51:44.127Z
Comments: This could be done with a [cloud init user data](https://cloudinit.readthedocs.io/en/latest/topics/format.html) section or with a systemd unit... as well as a cron job and many other ways. We would need more context on how you are deploying these instances. What process are you using to make the AMIs, what does the pipeline look like, what tools are you using?
Comments: Bruce Becker
Tags: <amazon-ec2><deployment><autoscaling>
Title: how to set automatic git pull from bitbucket and complie project on ec2 instance create event with auto scaling
UserDisplayName: Ahashan Alam Sojib
ThreadText: # how to set automatic git pull from bitbucket and complie project on ec2 instance create event with auto scaling
  
  (Asked by: username_0 on 2021-01-25)
  
  I have just started reading about AWS services. I have a project where the backend is created with `Spring-Boot`, front-end is created with `React` and `PostgreSQL` is used as database. Both backend and frontend is different `bitbucket` repository. I want to use two different `EC2` instance for front & back-end and `RDS` for `PostgreSQL`. Now I also want to use the autoscaling feature. The problem is how can I set the autoscaling feature in such a fashion where it automatically clone the app from bitbucket and then run a shell script file to compile and run the application.
  
  **Tags:** amazon-ec2, deployment, autoscaling
  
  > **Comments:**
  > **username_1**: This could be done with a [cloud init user data](https://cloudinit.readthedocs.io/en/latest/topics/format.html) section or with a systemd unit... as well as a cron job and many other ways. We would need more context on how you are deploying these instances. What process are you using to make the AMIs, what does the pipeline look like, what tools are you using?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-01-25. Score: 1
  
  You want to make use of [user\_data](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html) scripts.
  
  It's basically a bootstrap shell script that gets executed on an instance during launch.
  
  You can embed your app configuration script in there (either directly via bash, or have it call your configuration management software like Ansible or Chef).
  
  Then, if you attach this script to a Launch Template or a Launch Configuration (which is an instance template required as part of an AWS ASG), it will be applied to the whole autoscaling group.
  
  Then, when you have a scaling event, any new instances that come up in the ASG will run the script and deploy your code.
  
  However, I would recommend you look into immutable infrastructure instead. I.e. set up automation (i.e. via Packer) that builds your application code into your AMI, with user\_data only used for actually starting the app.
  
  Then, when you have a new build, have the same automation build a new AMI and replace your ASG (note you'll need to have your ASG created/deployed in a programmatic fashion.. I personally recommend Terraform).
  
  
  ---
  
  