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
https://stackoverflow.com/questions/65582660/using-different-aws-credentials-in-bitbucket-pipeline -->
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