Execute the below command after login to EC2 where you installed Terraform:
navigate to project-terraform folder where you have created already tf files.
navigate to project-terraform folder where you have created already tf files.
cd ~/project-terraform
if you are using Apple laptop or EC2 instance, execute below command:
sudo vi sonar.tf
for Windows laptop use below command:
notepad sonar.tf
Copy the below content with green background:
Change the key name marked red below per your key name:
resource "aws_vpc" "sonar" {
cidr_block = "172.16.0.0/16"
instance_tenancy = "default"
tags = {
Name = "sonar_vpc"
}
}
resource "aws_security_group" "security_sonar_group_2023" {
name = "security_sonar_group_2023"
description = "security group for Sonar"
ingress {
from_port = 9000
to_port = 9000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound from Sonar server
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags= {
Name = "security_sonar"
}
}
from_port = 9000
to_port = 9000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound from Sonar server
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags= {
Name = "security_sonar"
}
}
resource "aws_instance" "mySonarInstance" {
ami = "ami-0b9064170e32bde34"
key_name = "your_aws_ssh_key"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.security_sonar_group_2023.id]
ami = "ami-0b9064170e32bde34"
key_name = "your_aws_ssh_key"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.security_sonar_group_2023.id]
tags= {
Name = "sonar_instance"
}
}
# Create Elastic IP address for Sonar instance
resource "aws_eip" "mySonarInstance" {
vpc = true
instance = aws_instance.mySonarInstance.id
tags= {
Name = "sonar_elastic_ip"
}
}
Execute below command:
terraform plan
and then
terraform apply
Now you will see a new EC2 instance being created in AWS console.