Skip to content

Instantly share code, notes, and snippets.

@tkamag
Created March 9, 2024 06:16
Show Gist options
  • Select an option

  • Save tkamag/a950cb47fdb9a7ce42bc832eb341b69c to your computer and use it in GitHub Desktop.

Select an option

Save tkamag/a950cb47fdb9a7ce42bc832eb341b69c to your computer and use it in GitHub Desktop.
Test

Resources Block

Resource-1: Create VPC

resource "aws_vpc" "vpc-dev" {
  cidr_block = "10.0.0.0/16"
  tags = {
    "Name" = "vpc-dev"
  }
}

Resource-2: Create Subnets

resource "aws_subnet" "vpc-dev-public-subnet-1" {
  vpc_id                  = aws_vpc.vpc-dev.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true
}

Resource-3: Internet Gateway

resource "aws_internet_gateway" "vpc-dev-igw" {
  vpc_id = aws_vpc.vpc-dev.id
}

Resource-4: Create Route Table

resource "aws_route_table" "vpc-dev-public-route-table" { vpc_id = aws_vpc.vpc-dev.id }

Resource-5: Create Route in Route Table for Internet Access

resource "aws_route" "vpc-dev-public-route" { route_table_id = aws_route_table.vpc-dev-public-route-table.id destination_cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.vpc-dev-igw.id }

Resource-6: Associate the Route Table with the Subnet

resource "aws_route_table_association" "vpc-dev-public-route-table-associate" { route_table_id = aws_route_table.vpc-dev-public-route-table.id subnet_id = aws_subnet.vpc-dev-public-subnet-1.id }

Resource-7: Create Security Group

resource "aws_security_group" "dev-vpc-sg" { name = "dev-vpc-default-sg" description = "Dev VPC Default Security Group" vpc_id = aws_vpc.vpc-dev.id

ingress { description = "Allow Port 22" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }

ingress { description = "Allow Port 80" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }

egress { description = "Allow all IP and Ports Outbound" from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }

Resource-8: Create EC2 Instance

resource "aws_instance" "my-ec2-vm" { ami = "ami-047a51fa27710816e" ## Amazon Linux instance_type = "t2.micro" key_name = "terraform-key" subnet_id = aws_subnet.vpc-dev-public-subnet-1.id vpc_security_group_ids = [aws_security_group.dev-vpc-sg.id] ##user_data = file("apache-install.sh") user_data = <<-EOF ##!/bin/bash sudo yum update -y sudo yum install httpd -y sudo systemctl enable httpd sudo systemctl start httpd echo "

Welcome to StackSimplify ! AWS Infra created using Terraform in us-east-1 Region

" > /var/www/html/index.html EOF tags = { "Name" = "myec2vm" }
}

Resource-9: Create Elastic IP

resource "aws_eip" "my-eip" { instance = aws_instance.my-ec2-vm.id vpc = true

Meta-Argument

depends_on = [aws_internet_gateway.vpc-dev-igw] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment