VS Studio setup for Terraform IAC
Prerequisites
To follow this tutorial you will need:
- The Terraform CLI (1.2.0+) installed.
- The AWS CLI installed.
- AWS account and associated credentials that allow you to create resources.Prerequisites
To follow this tutorial you will need:
- The Terraform CLI (1.2.0+) installed.
- The AWS CLI installed.
- AWS account and associated credentials that allow you to create resources.
Install the Visual Studio code.
Download and setup terraform software. ADD the environment variable to be able to run the command for CLI.
Create IAM user and get access Key. This will be required to setup AWS CLI.
Configure AWS CLI.
Create a file to define your infrastructure in AWS CLOUD.
In this exmaple i am creating main.tf that has below code.
[https://developer.hashicorp.com/terraform/tutorials/aws-get-started/aws-build]
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-southeast-1"
}
resource "aws_instance" "Docker-Server" {
ami = "ami-052f483c20fa1351a"
instance_type = "t2.micro"
tags = {
Name = "DockerInstance"
}
}
Initialize the directory
When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with Initialize the directory
When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with terraform init
.
Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the aws
provider..
Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the aws
provider.
The terraform fmt
command automatically updates configurations in the current directory for readability and consistency.
Format your configuration. Terraform will print out the names of the files it modified, if any. In this case, your configuration file was already formatted correctly, so Terraform won’t return any file names.
terraform fmt
You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate
command.
Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.
$ terraform validate
Success! The configuration is valid.