Untitled

 avatar
unknown
plain_text
14 days ago
1.5 kB
4
Indexable
# Task: Deploy a simple VPC on AWS
# 1. Define the variables: vpc_name, vpc_cidr, private_subnet_cidrs, public_subnet_cidrs
# 2. Create a VPC
# 3. Create the corresponding public subnets and private subnets
# 4. Setup an internet gateway and associate to the public subnets
# 5. Output the internet_gateway ID

# 1. Define Variables
variable "vpc_name" {
  description = "Name of the VPC"
  type        = string
}

variable "vpc_cidr" {
  description = "CIDR for the VPC"
  type        = string
}

variable "private_subnet_cidrs" {
  description = "List of Private Subnets CIDRs"
  type        = list(string)
}

variable "public_subnet_cidrs" {
  description = "List of Public Subnets CIDRs"
  type        = list(string)
}

# The AWS module provider

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 2.7.0"
    }
  }
}

# Create VPC and related subnet using aws module
module "vpc" {
  source = "terraform-aws-modules/vpc/aws"

  name = var.vpc_name
  cidr = var.vpc_cidr

  azs             = ["ap-east-1a", "ap-east-1b"]
  private_subnets = var.private_subnet_cidrs
  public_subnets  = var.public_subnet_cidrs

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

# 4. Create IGW in vpc
resource "aws_internet_gateway" "gw" {
  vpc_id = module.vpc.vpc_id

  tags = {
    Name = "main"
  }
}

# 5. output the igw id
output "igw_id" {
  value = try(ws_internet_gateway.gw.id, null)
  description = "The ID of the IGW"
}
Editor is loading...
Leave a Comment