Variables#
create a variables.tf as
variable "instance_name" {type = stringdefault = "webserver"description = "instance name"}variable "instance_type" {type = stringdefault = "t2.medium"description = "instance type"}
Network#
configure terraform and provider
terraform {required_providers {aws = {source = "hashicorp/aws"version = "~>4.0.0"}}}provider "aws" {region = "us-east-1"}
create a vpc
resource "aws_vpc" "vpc" {cidr_block = "10.0.0.0/16"}
create an internet gateway
resource "aws_internet_gateway" "igw" {vpc_id = aws_vpc.vpc.id}
create a route table
resource "aws_route_table" "route-table-igw" {vpc_id = aws_vpc.vpc.idroute {cidr_block = "0.0.0.0/0"gateway_id = aws_internet_gateway.igw.id}}
create a public subnet
resource "aws_route_table_association" "routeassociation" {subnet_id = aws_subnet.publicsubnet.idroute_table_id = aws_route_table.route-table-igw.id}
create a route association
resource "aws_subnet" "publicsubnet" {vpc_id = aws_vpc.vpc.idcidr_block = "10.0.1.0/24"map_public_ip_on_launch = true}
Create a WebServer#
security group open port 22 for ssh
resource "aws_security_group" "web-sg" {vpc_id = aws_vpc.vpc.iddescription = "allow port 22 and 80"ingress {from_port = 22to_port = 22protocol = "tcp"cidr_blocks = ["0.0.0.0/0"]description = "allow port 22"}egress {from_port = 0to_port = 0protocol = "-1"cidr_blocks = ["0.0.0.0/0"]}tags = {"Name" = "allow port 22 and 80"}}
webserver with variables from variables.tf
resource "aws_instance" "webserver" {ami = "ami-0b0dcb5067f052a63"instance_type = var.instance_typesubnet_id = aws_subnet.publicsubnet.idsecurity_groups = [aws_security_group.web-sg.id]tags = {"Name" = var.instance_name}}