Starting Terraform with VS-Code : EC2 Creation
To start terraform with vs-code first install the extension it will help you.
Install hashicorp terraform plugin
Why we use AWS Provider?
We use the Amazon Web Services (AWS) provider to interact with the many resources supported by AWS. You must configure the provider with the proper credentials before you can use it.
Steps to follow for VS Code with terraform
- Create a file with
terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "eu-west-01"
}
Create an ec2 file :
We are creating an ec2 isntance
For quick reference of key-pair write go to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/key_pair
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
Code for ec2
resource "aws_key_pair" "terra-key-vscode" { key_name = "junoon-key-vscode" public_key = file("junoon-key-vscode.pub") } resource "aws_default_vpc" "default" { } resource "aws_security_group" "my_sg" { name = "my zplus security" description = "only this description" vpc_id = aws_default_vpc.default.id ingress { description = "allow access to SSH port 22" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { description = "allow access to http port 80" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { description = "allow all outgoing traffic" to_port = 0 from_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "my z plus sec" } } resource "aws_instance" "my_instance" { ami = "ami-0e9085e60087ce171" instance_type = "t2.micro " security_groups = [aws_security_group.my_sg.name] #interpolation key_name = aws_key_pair.terra-key-vscode.key_name #interpolation root_block_device { volume_size = 10 volume_type = "gp3" } }
Terraform in local - vscode : Check AWS is configured?
Go to terminal and write
aws configure
First check aws is installed or not in your windows machine
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
Download AWS cli for local (windows)
Configure AWS in local: Fetch credential with AWS EC2
Go to your EC2 AWS where you have configured AWS cred before, copy
access key and secret access key.
Go to your VS-code local terraform and configure the credential there:
This how you can configure AWS CLI in VS-Code AWS
To check AWS CLI in VS-code is configured or not
aws s3 ls
Yes, done I’m getting the output.
Now initiate the terraform file with apply
terraform init terraform plan # to check everything is correct terraform apply
These all files will be create with the following commands
You will get instance running in “Ireland region”
successfully running
To access ec2 created with terraform : with ssh
Revoke the permission with
icacls junoon-key-vscode /inheritance:r /grant:r "DELL:R"
Copy your ssh client form aws ec2
ssh -i "junoon-key-vscode.pem" ubuntu@ec2-34-255-7-235.eu-west-1.compute.amazonaws.com
do not forget to remove .pem then run
Successfully connected AWS EC2 with VS-code