So this marks the first post on my Terraform series, and I really enjoyed the process , and I hope it makes it easier for you to follow along too.
What is Terraform?
It is written and developed by HashiCorp.
It is a tool for building ,changing and versioning infrastructure safely and efficiently.
It can manage existing and popular service providers as well as custom in-house solutions through its extensive offering of providers.
Prerequisites
- 1. VS Code
- Terraform
- Terraform extension (download it from the extension tab on VS Code)
- AWS CLI extension (download it from the extension tab on VS Code)
- AWS account
Let's get into it
- Open VS code and ensure that Terraform has been succeffully installed by running the command below in the terminal:
>terraform version
- Confirm that the AWS CLI has been installed by running the command below in the VS code terminal:
>aws --version
- Get AWS credentials for the CLI(remember that we are accessing this through the VS code terminal) by either creating a new user or using an existing user and using their credentials to login into AWS programmatically and create resources ;
Log into AWS(I logged in as root user) and switched to IAM(Identity and Acccess Management)
Create a new user and give them programmatic access , then download their access keys and secret access keys as a csv. file and take note of your AWS region
- Configure the AWS profile in the VS code terminal by running and filling in the prompts from the downloaded csv. file and input the region as chosen above:
>aws configure
- Create a terraform file in the directory:
>main.tf
Then create a code block for the terraform provider (You can also grab it from the Terraform/AWS Provider version on Terraform)
terraform {
required_providers{
aws = {
source = "harshicorp/aws"
version = "~3.74"
}
}
}
Grab the region code block that the AWS Provider should use(Get it from the Terraform/AWS Provider version on Terraform)
provider "aws" {
region = "us-east-1"
}
Tell it to provide the budget (Grab the budget code by going to AWS resource in the Terraform registry and search for "aws-budgets-budget" and edit as required)
resource "aws_budgets_budget" "i_love_terraform" {
name = "monthly_budget"
budget_type = "COST"
limit_amount = "10"
limit_unit = "USD"
"time_period_start" = "2022-10-01_00:00"
time_unit = "MONTHLY" }
- Initialize the script:
>terraform init
- Clean the code:
>terraform fmt
- Ensure that the code is valid:
>terraform validate
- This step is especially important if you are running terraform code in a production environment to prevent you from breaking anything:
>terraform plan
- Launch the code:
>terraform apply
Log back into the AWS console and switch over to budgets to confirm that the new budget has been created
To confirm that your code can be modified, change a value i.e. limit_amount , then run terraform plan then apply then confirm that the changes have been made.
FIN
Top comments (0)