- Visit the Terraform website to download the appropriate binary for your operating system.
- Follow the installation instructions provided for your specific platform.
- Open a terminal or command prompt.
- Type
terraform version
and press Enter. - You should see the installed version of Terraform displayed. This confirms that Terraform has been successfully installed.
- If you don't have an AWS account, sign up for one here.
- Log in to your AWS Management Console.
- Navigate to the IAM (Identity and Access Management) service.
- Create a new IAM user with programmatic access.
- Attach the necessary permissions policies to the user for the AWS services you intend to manage with Terraform (e.g., AmazonEC2FullAccess, AmazonS3FullAccess).
- Once the user is created, note down the Access Key ID and Secret Access Key.
- In your terminal or command prompt, navigate to the directory where you plan to work with Terraform.
- Create a new file named
credentials.tf
or any other appropriate name. - Add the following content to the file, replacing
YOUR_ACCESS_KEY
andYOUR_SECRET_KEY
with the respective values obtained in Step 3:
provider "aws" {
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
region = "us-west-2" // Modify the region as per your requirement
}
- Run
terraform init
in your terminal. - This command initializes Terraform in the current directory, downloading any necessary plugins and modules.
- Create a new
.tf
file in your working directory (e.g.,main.tf
). - Write Terraform configuration code to define the AWS resources you want to create or manage. Refer to the Terraform documentation for guidance on resource configuration.
- Run
terraform validate
to check the syntax and validity of your Terraform configuration. - Fix any errors or warnings reported by the validation process.
- Run
terraform plan
to generate an execution plan. - Review the proposed changes to ensure they align with your expectations.
- Verify that no unintended changes will occur.
- Run
terraform apply
to apply the changes defined in your Terraform configuration. - Confirm the changes when prompted by Terraform.
- Wait for Terraform to provision the infrastructure.
- If you no longer need the provisioned resources, run
terraform destroy
to tear down the infrastructure. - Confirm the destruction when prompted by Terraform.
If you encounter issues when pushing Terraform code to GitHub, such as the repository being prevented from uploads, it may be due to sensitive information or cached files within the Terraform directory. Follow these steps to address the issue:
- Ignore Terraform Cached Files:
- Ensure that Terraform's directory, typically named
.terraform
, which holds cached files, is ignored during version control. - One possible solution involves using the following command:
git filter-branch -f --index-filter 'git rm --cached -r --ignore-unmatch .terraform/'
- Ensure that Terraform's directory, typically named
- Alternative Approach:
- Another approach involves removing every
.terraform
directory from subdirectories. - Execute the following command from the root directory of your repository:
find . -type d -name '.terraform' -exec rm -r {} +
- Another approach involves removing every
- Remove State Files:
- Additionally, to prevent state files from being uploaded, you can remove them using:
find . -name '*.backup' -delete
- Additionally, to prevent state files from being uploaded, you can remove them using: