I want you to act as a Terraform Engineer. You will be skilled in AWS. I will type in…
Private research annotation
Sign in to add a private research label or note.
- User
I want you to act as a Terraform Engineer. You will be skilled in AWS. I will type in commands asking you to generate terraform code in specific files. You will only reply with the added code having no code comments, you won't add any explanations to your replies. Any variables you refer you will add automatically to variables.tf, any extra resources referenced by you while generating the code will automatically be added to main.tf Any code written by you would be written keeping security in mind. You'll name the resources generated using the project name "alpha". Your first task is to create a VPC in main.tf
- Assistant
resource "aws_vpc" "alpha" { cidr_block = var.vpc_cidr enable_dns_support = true enable_dns_hostnames = true tags = { Name = "alpha" } }
variable "vpc_cidr" {}
- User
Add a public subnet in main.tf
- Assistant
resource "aws_subnet" "alpha_public" { vpc_id = aws_vpc.alpha.id cidr_block = var.public_subnet_cidr availability_zone = var.public_subnet_az map_public_ip_on_launch = true tags = { Name = "alpha-public" } }
variable "public_subnet_cidr" {} variable "public_subnet_az" {}
- User
Add an internet gateway to main.tf
- Assistant
resource "aws_internet_gateway" "alpha" { vpc_id = aws_vpc.alpha.id tags = { Name = "alpha" } }
- User
Add a route table with a route to the internet gateway
- Assistant
resource "aws_route_table" "alpha" { vpc_id = aws_vpc.alpha.id tags = { Name = "alpha" }
route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.alpha.id } }
resource "aws_route_table_association" "alpha_public" { subnet_id = aws_subnet.alpha_public.id route_table_id = aws_route_table.alpha.id }
- User
Add a NAT gateway in our public subnet
- Assistant
resource "aws_nat_gateway" "alpha" { allocation_id = aws_eip.alpha.id subnet_id = aws_subnet.alpha_public.id tags = { Name = "alpha" } }
resource "aws_eip" "alpha" { vpc = true }
resource "aws_route_table" "alpha_private" { vpc_id = aws_vpc.alpha.id tags = { Name = "alpha-private" }
route { cidr_block = "0.0.0.0/0" nat_gateway_id = aws_nat_gateway.alpha.id } }
resource "aws_subnet" "alpha_private" { vpc_id = aws_vpc.alpha.id cidr_block = var.private_subnet_cidr availability_zone = var.private_subnet_az tags = { Name = "alpha-private" } }
variable "private_subnet_cidr" {} variable "private_subnet_az" {}