- Let's use the following HCL script for provisioning multiple EC2 instances, creating a VPC with a VPN and also launching an S3 bucket on AWS Cloud.
Although the following script is meant specifically for AWS cloud, the terraform commands have the same usage for all cloud platforms.
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "nish" {
ami = "ami-02b5fbc2cb28b77b8"
count = 2
instance_type = "t2.micro"
tags = {
Name = "noicecurse"
}
}
resource "aws_s3_bucket" "nish" {
bucket = "shiftnoicecurse" # name must be universally unique for all AWS users
acl = "private"
}
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_vpn_gateway" "vpn_gateway" {
vpc_id = "vpc-0a5a4d4f01bc6769e" #hardcoding default vpc id
}
resource "aws_customer_gateway" "customer_gateway" {
bgp_asn = 65000
ip_address = "172.0.0.1"
type = "ipsec.1"
}
resource "aws_vpn_connection" "main" {
vpn_gateway_id = aws_vpn_gateway.vpn_gateway.id
customer_gateway_id = aws_customer_gateway.customer_gateway.id
type = "ipsec.1"
static_routes_only = true
}
It is assumed that the AWS credentials are stored locally by using AWS-CLI.
Commands
1. Fmt
$ terraform fmt
Format the HCL script into canonical form for increased readability.
- Before format
- After format
2. Init
$ terraform init
Install the plugins and dependencies mentioned for the provider.
3. Validate
$ terraform validate
Validate syntax and find errors in the HCL script.
4. Plan
$ terraform plan
See the changes that would occur on provisioning the infrastructure
5. Graph
$ terraform graph -h # help
$ terraform graph -draw-cycles # visual help for debugging
$ terraform graph -type=plan # type of graph ranging from plan, destroy, apply etc.
6. Apply
$ terraform apply
$ terraform apply -auto-approve # to skip user interaction
7. Destroy
$ terraform destroy
$ terraform destroy -auto-approve # to skip user interaction
Note: If you are getting started with your journey in the world of Terraform, I highly recommend you to focus on destroying resources. In my experience, I have left resources after creating them, and ended up spending a lot of capital on cloud resources.
For an in-depth coverage around Terraform destroy, I highly recommend reading this blog by Spacelift.
Top comments (0)