Terraform-GIT-Bitbucket-AWS


git-terraform commands
git clone https://thakishore-admin@bitbucket.org/thakishore/terraform-pipeline.git
git clone https://x-token-auth:ATCTT3xFfGN0Grd7ydtJ7KWY12D_FlMcBlgwxTCXJeyezzpIMFj_2YqQCuMSawrOLViuTlImoC9r5mQqEOgCtqYmiGI25ynqGchpNx5UEylgXgCBWayZ3_zy8lEPLMDnlPmeDg3nqKjPs8w6lCbv2VsUN
FTs2Ple0UsFumKQ4-6_ttbzmXpF2MA=F8B06163@bitbucket.org/thakishore/terraform-pipeline.git
terraform init
terraform plan
terraform apply -auto-approve
terraform apply -lock=false
git pull origin master
git status
git add .							-> stage all files
git add bitbucket-pipelines.yml		-> stage mentioned file only
git commit -m"my commit"
git push origin master

main.tf
data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

  tags = {
    Name = "Ubuntu"
  }
}

bitbucket-pipelines.yml
image: hashicorp/terraform

pipelines:
  branches:
    master:
      - step:
          name: Deploy to staging
          deployment: staging
          script:
            - terraform init
            - terraform plan
            - terraform apply -auto-approve

backend.tf
terraform {
  backend "s3" {
    bucket = "thakishore-bitbucket-terraform"
	dynamodb_table = "terraform-state-lock-dynamo"
    key    = "bitbucket-terraform.tfstate"
    region = "ap-south-1"
  }
}

provider.tf

provider "aws" {
  region = "ap-south-1"
}

.gitignore
.terraform*
terraform.tfstate*

Terraform Commands
terraform init
terraform plan
terraform apply
terraform apply -auto-approve
terraform destroy -auto-approve
terraform destroy									-> Delete All Resources in One Folder
terraform destroy -target aws_instance.AmazonAMI	-> resource type.local resource name
terraform apply -auto-approve -var-file="dev.tfvars"
terraform validate									-> Validating terrafom configuration files
terraform apply -replace="aws_instance.myec2		-> Recreating the resource

GIT Terraform Bitbucket Deployment Steps

Step 1:


Step 2:


Step 3:


Step 4:


Step 5:


Step 6:


Step 7:


Step 8:


Step 9:


Step 10:


Step 11:


Step 12:


Step 13:

Step 14:

Step 15:

Step 16:

Step 17:

Step 18:

Step 19:

Step 20:

Step 21:

Step 22:

Step 23:

Step 24:

Step 25:

Step 26:

Step 27:

Step 28:

Step 29:

Step 30:

Step 31:

Step 32:

Step 33:

Step 34:

Step 35:

Step 36:

Step 37:

Step 38:

Step 39:

Step 40:

https://stackoverflow.com/questions/65582660/using-different-aws-credentials-in-bitbucket-pipeline-->