title | tags |
---|---|
AWS VPC |
aws vpc |
This code is written for a demo Instruqt course hwoever it can be utilized for creating an AWS VPC.
The aws-vpc module can be used to quickly setup an AWS VPC. With a few variables passed in, a new VPC with associated priv/public subnets availability-zones can be created.
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "zt-vpc"
cidr = "10.0.0.0/16"
azs = ["${var.region}a", "${var.region}b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
tags = {
Terraform = "true"
Environment = "development"
}
}
The backend file is to specify the location and name of the state file.
Below we are storing state in the local current directory
The statefile will not be created until a terraform init
is run.
terraform {
required_version = "~> 1.0.11"
backend "local" {
path = "./terraform.tfstate"
}
}
The provider file is what Terraform Core interacts with in order to bring in different providers like AWS, Azure etc. In the following provider we are using the AWS provider. To interact with the provider for AWS, we will need credentials and region.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}
variable "region" {
type = string
description = "AWS Region"
default = "us-east-2"
}
variable "access_key" {
type = string
description = "This is the value of AWS_ACCESS_KEY_ID"
default = "x---"
}
variable "secret_key" {
type = string
description = "This is the value of AWS_SECRET_ACCESS_KEY"
default = "x---"
sensitive = true
}
The following creates an output for the VPN Gateway
output "aws_vpn_gateway_id" {
description = "The ID of the VPN Gateway"
value = module.vpc.vgw_id
}