DEV Community

Msaghu
Msaghu

Posted on

CREATING AN AWS BUDGET USING TERRAFORM

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. 1. VS Code
  2. Terraform
  3. Terraform extension (download it from the extension tab on VS Code)
  4. AWS CLI extension (download it from the extension tab on VS Code)
  5. 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
Enter fullscreen mode Exit fullscreen mode
  • Confirm that the AWS CLI has been installed by running the command below in the VS code terminal:
>aws --version
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Create a terraform file in the directory:
>main.tf
Enter fullscreen mode Exit fullscreen mode

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"
            }
       }
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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"        }

Enter fullscreen mode Exit fullscreen mode
  • Initialize the script:
>terraform init
Enter fullscreen mode Exit fullscreen mode
  • Clean the code:
>terraform fmt
Enter fullscreen mode Exit fullscreen mode
  • Ensure that the code is valid:
>terraform validate
Enter fullscreen mode Exit fullscreen mode
  • This step is especially important if you are running terraform code in a production environment to prevent you from breaking anything:
>terraform plan
Enter fullscreen mode Exit fullscreen mode
  • Launch the code:
>terraform apply
Enter fullscreen mode Exit fullscreen mode
  • 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)