Last active
November 3, 2021 21:21
-
-
Save ankyit/d180cdc2843a21204f27473a6c7eeb2c to your computer and use it in GitHub Desktop.
Terraform-AWS-EC2-Creation-V0.12 : This gist can help you create an EC2 instance on AWS with Terraform version 0.12.16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variable Definition | |
variable "aws_region" {} | |
variable "aws_vpc_cidr_block" {} | |
variable "aws_subnet_cidr_block" {} | |
variable "aws_private_ip_fe" {} | |
variable "aws_Name" {} | |
variable "aws_Application" {} | |
variable "aws_ami" {} | |
variable "aws_instance_type" {} | |
// Provider Definition | |
provider "aws" { | |
version = "~> 2.40" | |
region = var.aws_region | |
} | |
// Adds a VPC | |
resource "aws_vpc" "aws_ec2_deployment_test-vpc" { | |
cidr_block = var.aws_vpc_cidr_block | |
tags = { | |
Name = join("-", [var.aws_Name, "vpc"]) | |
Application = var.aws_Application | |
} | |
} | |
//Adds a subnet | |
resource "aws_subnet" "aws_ec2_deployment_test-subnet" { | |
vpc_id = aws_vpc.aws_ec2_deployment_test-vpc.id | |
cidr_block = var.aws_subnet_cidr_block | |
availability_zone = join("", [var.aws_region, "a"]) | |
tags = { | |
Name = join("-", [var.aws_Name, "subnet"]) | |
Application = var.aws_Application | |
} | |
} | |
//Adds a Network Interface | |
resource "aws_network_interface" "aws_ec2_deployment_test-fe" { | |
subnet_id = aws_subnet.aws_ec2_deployment_test-subnet.id | |
private_ips = [ var.aws_private_ip_fe ] | |
tags = { | |
Name = join("-", [var.aws_Name, "network-interface-fe"]) | |
Application = var.aws_Application | |
} | |
} | |
//Adds an EC2 Instance | |
resource "aws_instance" "aws_ec2_deployment_test-fe"{ | |
ami = var.aws_ami | |
instance_type = var.aws_instance_type | |
network_interface { | |
network_interface_id = aws_network_interface.aws_ec2_deployment_test-fe.id | |
device_index = 0 | |
} | |
tags = { | |
Name = join("-", [var.aws_Name, "fe-ec2"]) | |
Application = var.aws_Application | |
} | |
} | |
// Print Output Values | |
output "aws_ec2_deployment_test-vpc" { | |
description = "CIDR Block for the VPC: " | |
value = aws_vpc.aws_ec2_deployment_test-vpc.cidr_block | |
} | |
output "aws_ec2_deployment_test-subnet" { | |
description = "Subnet Block: " | |
value = aws_subnet.aws_ec2_deployment_test-subnet.cidr_block | |
} | |
output "aws_ec2_deployment_test-private-ip" { | |
description = "System Private IP: " | |
value = aws_network_interface.aws_ec2_deployment_test-fe.private_ip | |
} | |
output "aws_ec2_deployment_test-EC2-Details" { | |
description = "EC2 Details: " | |
value = aws_instance.aws_ec2_deployment_test-fe.public_ip | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Replace with preferred AWS Region | |
aws_region = "ap-southeast-1" | |
// Replace with preferred CIDR/ Subnet and IP | |
aws_vpc_cidr_block = "172.16.0.0/16" | |
aws_subnet_cidr_block = "172.16.10.0/24" | |
aws_private_ip_fe = "172.16.10.100" | |
// Replace with preferred name | |
aws_Name = "aws-ec2-test-ground-application" | |
aws_Application = "aws-ec2-test-ground" | |
//Replace with AMI id from your region | |
aws_ami = "ami-07539a31f72d244e7" | |
//Replace with instance type | |
aws_instance_type = "t2.micro" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment