Introduction#
this is day 3 and will show
- modules
- variables
- setup
create a vpc, igw and route table in the setup, then create a webserver, subnet using module and variables from modules.
Project Structure#
--day3|--setup|--main.tf|--modules|--webserver|--main.tf|--variables.tf
Modules#
variables in variables.tf
variable "vpc_id" {type = stringdescription = "vpc id"}variable "subnet_cidr_block" {type = stringdescription = "vpc cidr block"}variable "ami" {type = stringdescription = "ami of ec2"}variable "instance_type" {type = stringdescription = "ec2 instance type"}variable "webserver_name" {type = stringdescription = "webserver name"}variable "route_table_id" {type = stringdescription = "route table id"}
then main.tf which use variables from the variable.tf
terraform {required_providers {aws = {source = "hashicorp/aws"version = "~>4.0.0"}}}resource "aws_subnet" "webserver" {vpc_id = var.vpc_idcidr_block = var.subnet_cidr_block}resource "aws_route_table_association" "routeassociation" {subnet_id = aws_subnet.webserver.idroute_table_id = var.route_table_id}resource "aws_instance" "webserver" {ami = var.amiinstance_type = var.instance_typesubnet_id = aws_subnet.webserver.idtags = {"Name" : "${var.webserver_name}"}}
Setup#
terraform versions and providers
terraform {required_providers {aws = {source = "hashicorp/aws"version = "~>4.0.0"}}}provider "aws" {region = "us-east-1"}
create a vpc, internet gateway and route table
resource "aws_vpc" "vpc" {cidr_block = "10.0.0.0/16"}resource "aws_internet_gateway" "igw" {vpc_id = aws_vpc.vpc.id}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}}
then create a webserver as following
module "webserver" {source = "../modules/webserver"vpc_id = aws_vpc.vpc.idsubnet_cidr_block = "10.0.1.0/24"webserver_name = "haimtran"ami = "ami-0b0dcb5067f052a63"instance_type = "t2.medium"route_table_id = aws_route_table.route-table-igw.id}