Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
- Download the latest version of Terraform from here.
- Unzip the downloaded file.
- Move the executable file to
/usr/local/bin/
. - Run
terraform --version
to verify the installation. - Done!
Create a directory for your Terraform configuration files.
Create a file named main.tf
in the directory you created in step 1.
Run terraform init
to initialize Terraform.
Run terraform plan
to create a Terraform execution plan.
Run terraform apply
to apply the Terraform execution plan.
Run terraform destroy
to destroy Terraform resources.
- Go to AWS IAM.
- Click on
Users
in the left sidebar. - Click on
Add user
. - Enter a name for the user.
- Select
Programmatic access
. - Click on
Next: Permissions
. - Select
Attach existing policies directly
. - Select
AdministratorAccess
. (This is not recommended for production environments.) - Click on
Next: Tags
. - Click on
Next: Review
. - Click on
Create user
. - Copy the
Access key ID
andSecret access key
to a safe place. - set aws cli credentials using
aws configure
command.
- Create s3 bucket for remote state. (e.g.
terraform-remote-state
) usingaws s3api create-bucket --bucket terraform-remote-state-0001 --region us-east-1 --create-bucket-configuration LocationConstraint=ap-south-1
- Create a DynamoDB table for state locking. (e.g.
terraform-state-lock
) usingaws dynamodb create-table --table-name terraform-state-lock --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
- in
main.tf
file add following code
terraform {
backend "s3" {
bucket = "terraform-remote-state"
key = "terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-state-lock"
}
}
- other terraform commands will work as usual.