Terraform Example for creating simple AWS Resource
- Service: What cloud provider is offering you
- Resource: Anything created using Service
Setup Terraform DEV Environment
- Install Terraform
- Install Visual Studio Code
- Install Terraform Extension into Visual Studio Code
Manual: Create a VPC in oregon Region
Terraform: Create a VPC in oregon Region
- Create a new directory hello-tf as folder is input to terraform
- Create a new file called as main.tf
- To create resources in aws, use aws provider. Provider syntax is
provider 'provider type' {
arg1 'value1'
...
...
argn 'valuen'
}
- Using The above syntax and the documentation over here, lets fill the aws provider
provider "aws" {
access_key = "<ACCESS-KEY>"
secret_key = "<SECRET-KEY>"
region = "us-west-2"
}
-
Note: Inputs in Terraform are called as arguments and outputs are called as attributes.
-
Now we need to create a VPC Resource. Search the Resources in Documentation page or just google ‘terraform resource <cloud> <resource>’
-
Resource Syntax is
resource "type" "name" {
arg1 'value1'
...
...
argn 'valuen'
}
- Lets add the VPC resource details to existing main.tf file
provider "aws" {
access_key = "<ACCESS-KEY>"
secret_key = "<SECRET-KEY>"
region = "us-west-2"
}
resource "aws_vpc" "myvpc" {
cidr_block = "192.168.0.0/16"
tags = {
"Name" = "from-tf"
}
}
- Now to execute this terraform tempalte, Launch Powershell in the directory where terraform template is written
terraform init .
terraform validate .
terraform apply .
- Once you are done resource can be removed using
terraform destroy .