creating an EC2 instance using terraform code example

Example 1: aws instance creation using terraform

resource "aws_instance" "myec2" {
  ami                    = "<AMI-ID>"
  instance_type          = "t2.micro"
  vpc_security_group_ids = ["SECURITY-GROUP-ID"]
  key_name               = "<KEY-NAME>"
  subnet_id              = "<SUBNET ID>"
  iam_instance_profile = "<ROLE NAME>"

  tags = {
    Name = "<PUT THE NAME>"
  }
}

Example 2: create aws ec2 instance using terraform

#create aws ec2 instance using terraform
provider "aws" {
  region = "ap-south-1"

}
resource "aws_instance" "ec2" {
  ami = "ami-019f0583716f0aec4" //ap-south-1
  instance_type = "t2.micro"
  tags = {
    Name = "visualscrapy"
  }
}
#you can get the ami-id from amzon marketplace