Answers: <p>Setup a Jenkins server. Please do code checkout using Jenkins. Jenkins supports parameterized build. you can pass selected parameter from Jenkins UI to linux bash.</p>
  <p>Then use linux's cli command to fire terraform.Terraform supports several workspace. You can use same code for dev, test and prod.</p>
  <p><code>terraform workspace new dev</code>
  Use this command to create new workspace. After that , we can use this workspace name to your code. Below is the content of <code>local.tf</code></p>
  <pre><code>locals {
    env = terraform.workspace
  }
  </code></pre>
  <p>Like below use the same code for multiple env.</p>
  <pre><code>resource &quot;aws_autoscaling_group&quot; &quot;asg&quot; {
    name = &quot;${local.env}-${local.project}-demo-asg&quot;
  }
  </code></pre>
  <p>Moreover to save your state file securely , you can use s3 as backend.</p>
  <pre><code>terraform {
    backend &quot;s3&quot; {
      bucket = &quot;tfstate-demo&quot;
      key = &quot;keyfiles&quot;
      region = &quot;ap-southeast-1&quot;
      dynamodb_table = &quot;tfstate-demo&quot;
    }
  }
  </code></pre>
  <p>To separate backend and frontend service, use best practice of AWS. Create a public ALB for frontend and then move traffic to backend services via private ALB.</p>
  <p>You can use Ansible as a configuration manager to build your server then create a AMI of that using packer. Later on, use the same AMI in ASG.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-19T06:15:30.903Z
Answers: 2021-03-19T06:15:30.903Z
Answers: again
Answers: <p>The way we do this is to use <a href="https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/" rel="nofollow noreferrer">EC2 User data</a> to run a cloud-init script after the EC2 instance has been launched.</p>
  <p>With Terraform this user data can be templated and parameterized for different environments using Terraform's <a href="https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file" rel="nofollow noreferrer">templatefile</a> and passing in <a href="https://www.terraform.io/docs/language/values/variables.html" rel="nofollow noreferrer">variables via parameter files</a>.</p>
  <p>I would suggest you also read up on AWS Auto scaling groups and launch templates as well.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-05-13T09:37:45.400Z
Answers: 2021-05-13T09:37:45.400Z
Answers: Alastair Montgomery
Body: <p>I've just started learning Terraform (utilizing the AWS provider) and I have managed to replicate our infrastructure which includes quite a few amazon web services.</p>
  <p>I can successfully deploy it and get a basic Apache website up and running on an EC2 instance with the following bash script (run from Terraform):</p>
  <pre><code>#!/bin/bash
  sudo apt update -y
  sudo apt install apache2 -y
  sudo systemctl start apache2
  sudo bash -c 'echo frontend-website &gt; /var/www/html/index.html'
  </code></pre>
  <p>Very basic, but I'm having issues in finding some documentation on the best practices for getting our project code onto the AWS EC2 instance once provisioned, and how everything should be setup for Gitlab CI/CD to keep it all automated.</p>
  <p>Especially for a bigger project (e.g., separate frontend and backend instances) where you may have a dev, test, and prod environment for the frontend and backend.</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-03-20T08:27:32.860Z
Comments: Don't do this. Use Packer instead. Provisioners are a tool of last resort.
Comments: Software Engineer
Tags: <amazon-web-services><terraform><gitlab><amazon-ec2>
Title: How to get code onto aws ec2 instance using terraform and gitlabs
UserDisplayName: Second2None
ThreadText: # How to get code onto aws ec2 instance using terraform and gitlabs
  
  (Asked by: username_0 on 2021-03-18)
  
  I've just started learning Terraform (utilizing the AWS provider) and I have managed to replicate our infrastructure which includes quite a few amazon web services.
  
  I can successfully deploy it and get a basic Apache website up and running on an EC2 instance with the following bash script (run from Terraform):
  
  ```
  #!/bin/bash
  sudo apt update -y
  sudo apt install apache2 -y
  sudo systemctl start apache2
  sudo bash -c 'echo frontend-website > /var/www/html/index.html'
  
  ```
  
  Very basic, but I'm having issues in finding some documentation on the best practices for getting our project code onto the AWS EC2 instance once provisioned, and how everything should be setup for Gitlab CI/CD to keep it all automated.
  
  Especially for a bigger project (e.g., separate frontend and backend instances) where you may have a dev, test, and prod environment for the frontend and backend.
  
  **Tags:** amazon-web-services, terraform, gitlab, amazon-ec2
  
  > **Comments:**
  > **username_1**: Don't do this. Use Packer instead. Provisioners are a tool of last resort.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2021-03-19. Score: 1
  
  Setup a Jenkins server. Please do code checkout using Jenkins. Jenkins supports parameterized build. you can pass selected parameter from Jenkins UI to linux bash.
  
  Then use linux's cli command to fire terraform.Terraform supports several workspace. You can use same code for dev, test and prod.
  
  `terraform workspace new dev`
  Use this command to create new workspace. After that , we can use this workspace name to your code. Below is the content of `local.tf`
  
  ```
  locals {
    env = terraform.workspace
  }
  
  ```
  
  Like below use the same code for multiple env.
  
  ```
  resource "aws_autoscaling_group" "asg" {
    name = "${local.env}-${local.project}-demo-asg"
  }
  
  ```
  
  Moreover to save your state file securely , you can use s3 as backend.
  
  ```
  terraform {
    backend "s3" {
      bucket = "tfstate-demo"
      key = "keyfiles"
      region = "ap-southeast-1"
      dynamodb_table = "tfstate-demo"
    }
  }
  
  ```
  
  To separate backend and frontend service, use best practice of AWS. Create a public ALB for frontend and then move traffic to backend services via private ALB.
  
  You can use Ansible as a configuration manager to build your server then create a AMI of that using packer. Later on, use the same AMI in ASG.
  
  
  ---
  
  ### Answer by username_3 on 2021-05-13. Score: 0
  
  The way we do this is to use [EC2 User data](https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/) to run a cloud-init script after the EC2 instance has been launched.
  
  With Terraform this user data can be templated and parameterized for different environments using Terraform's [templatefile](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) and passing in [variables via parameter files](https://www.terraform.io/docs/language/values/variables.html).
  
  I would suggest you also read up on AWS Auto scaling groups and launch templates as well.
  
  
  ---
  
  