Created
September 16, 2022 20:06
-
-
Save ardeshir/45269b4605e2f771e772c470b3e88c1e to your computer and use it in GitHub Desktop.
AWS Infra with Terraform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger: | |
- main | |
pool: | |
vmImage: ubuntu-latest | |
variables: | |
- group: TerraformConfiguration | |
- group: TerraformVariables | |
parameters: | |
- name: ENVIRONMENT | |
displayName: Environments | |
type: string | |
default: dev | |
values: | |
- dev | |
- qa | |
- name: provisionType | |
type: string | |
default: Apply | |
values: | |
- Apply | |
- Destroy | |
- name: ec2_ami | |
type: string | |
default: 'ami-052efd3df9dad4825' | |
values: | |
- 'ami-052efd3df9dad4825' | |
stages: | |
- stage: Deploy_Infra_Terraform | |
displayName: 'Deploying Infra with Terraform' | |
dependsOn: [] | |
jobs: | |
- job: Deploy_Infra_Terraform | |
steps: | |
- task: Bash@3 | |
inputs: | |
targetType: 'inline' | |
script: | | |
wget -qO - terraform.gpg https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/terraform-archive-keyring.gpg | |
sudo echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/terraform-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/terraform.list | |
sudo apt update | |
sudo apt install terraform | |
terraform --version | |
displayName: 'Install Terraform' | |
- script: | | |
echo " Terraform init!" | |
terraform init -backend-config=bucket="cds-iot-terraform-${{ parameters.ENVIRONMENT }}" | |
displayName: 'Terraform Init' | |
workingDirectory: $(System.DefaultWorkingDirectory)/ | |
enabled: true | |
env: | |
AWS_REGION: $(AWS_REGION) | |
AWS_ACCESS_KEY_ID: $(AWS_ACCESS_KEY_ID_${{ parameters.ENVIRONMENT }}) | |
AWS_SECRET_ACCESS_KEY: $(AWS_SECRET_ACCESS_KEY_${{ parameters.ENVIRONMENT }}) | |
- script: | | |
terraform plan -out=plan.tfplan -var=keypair="tfvm-${{ parameters.ENVIRONMENT }}" -var=ec2_ami=${{ parameters.ec2_ami}} -var=env=${{ parameters.ENVIRONMENT}} | |
terraform apply -input=false -auto-approve plan.tfplan | |
displayName: 'Terraform Plan & Apply' | |
workingDirectory: $(System.DefaultWorkingDirectory)/ | |
condition: eq('${{ parameters.provisionType }}', 'Apply') | |
env: | |
AWS_REGION: $(AWS_REGION) | |
AWS_ACCESS_KEY_ID: $(AWS_ACCESS_KEY_ID_${{ parameters.ENVIRONMENT }}) | |
AWS_SECRET_ACCESS_KEY: $(AWS_SECRET_ACCESS_KEY_${{ parameters.ENVIRONMENT }}) | |
- script: | | |
terraform plan -destroy -out=plan.tfdestroy | |
terraform apply -auto-approve plan.tfdestroy | |
displayName: 'Terraform Destroy' | |
workingDirectory: $(System.DefaultWorkingDirectory)/ | |
condition: eq('${{ parameters.provisionType }}', 'Destroy') | |
env: | |
AWS_REGION: $(AWS_REGION) | |
AWS_ACCESS_KEY_ID: $(AWS_ACCESS_KEY_ID_${{ parameters.ENVIRONMENT }}) | |
AWS_SECRET_ACCESS_KEY: $(AWS_SECRET_ACCESS_KEY_${{ parameters.ENVIRONMENT }}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment