Create AWS instance using Terraform code
Install Terraform on your local machine: You can download Terraform from the official website and follow the installation instructions for your operating system.
Please visit- https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli
Create a new directory for your Terraform project: Create a new directory on your local machine where you will store your Terraform configuration files.
As I created like Terraform-project/aws-instance-terraform/
Create a new file named main.tf in the directory and add the following code: In the main.tf file, you will define the AWS provider and the resources you want to create.
Here is an example code that creates an AWS instance and also a S3 bucket creation.
provider "aws" { region= "us-east-1"
} resource "aws_s3_bucket" "demo_s3" { bucket= "terraform-bucket-sujit" }
resource "aws_instance" "demo-ec2" { ami= var.ec2_ubuntu_ami count = 2 instance_type = "t2.micro" tags = { Name = "ubuntu_EC2" } }
It has used the variable file to mention the instance type to avoid hard coded. You could simply go to that variable file and change the instance type. It is easy and simple.
Here is the below the variable.tf file.
resource "ec2_ubuntu_ami"{ default= "ami-053b0d53c279acc90" }
resource "ec2_windows_ami" { default= "ami-0be0e902919675894" }
resource "ec2_Amazon_linux" { default= "ami-0be0e902919675894" }
Again I have divided the terraform provider for AWS. Below is the code:
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } ubuntu@ip-172-31-38-43:~/terraform-aws$ cat terraform.tf terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } }
We can simply create the instance, but I have attached VPC, public key, CIDR and security group and allowed access through SSH port.
provider "aws" { region= "us-east-1"
} resource "aws_s3_bucket" "demo_s3" { bucket= "terraform-bucket-sujit" }
resource "aws_instance" "demo-ec2" { ami= var.ec2_ubuntu_ami count = 2 instance_type = "t2.micro" tags = { Name = "ubuntu_EC2" } }
terraform-aws$ cat vpc.tf resource "aws_key_pair" "mykey" { key_name= "terra-key" public_key= file("/home/ubuntu/.ssh/terra-key.pub") }
resource "aws_instance" "my-vpc-instance" { key_name= aws_key_pair.mykey.key_name ami =var.ec2_ubuntu_ami instance_type = "t2.micro" security_groups = [aws_security_group.allow_ssh.name]
} resource "aws_default_vpc" "default_vpc" {
}
resource "aws_security_group" "allow_ssh" { name = "allow_ssh" description = "Allow ssh inbound traffic"
#using default VPC
vpc_id = aws_default_vpc.default_vpc.id ingress { description = "TLS from VPC"
# we should allow incoming and outgoing # TCP packets from_port = 22 to_port = 22 protocol = "tcp"
# allow all traffic cidr_blocks = ["0.0.0.0/0"] } }
To get the code directly please visit my GitHub Repo using the following link:-
Subscribe to my newsletter
Read articles from Sujit Sahoo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by