Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tfvars file to assign values main.tf outside Day-3 folder #47

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Day-3/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
provider "aws" {
region = "us-east-1"
}

module "ec2_instance" {
source = "./modules/ec2_instance"
ami_value = "ami-053b0d53c279acc90" # replace this
instance_type_value = "t2.micro"
subnet_id_value = "subnet-019ea91ed9b5252e7". # replace this
}
source = "./modules/ec2-instance"
//referencing the externalised variables in Day-3/variables.tf
ami_var=var.oami_var
var_key_name=var.ovar_key_name
varble_subnet_id=var.ovarble_subnet_id
variable_instance_type=var.ovariable_instance_type

//Directly assigining the values to the remote module variables.tf in ec2-instance/variables.tf
/*ami_var="ami-0f58b397bc5c1f2e8"
var_key_name="aws_login"
varble_subnet_id="subnet-0a94c001e716ebd8b"
variable_instance_type="t2.micro"*/
}
13 changes: 5 additions & 8 deletions Day-3/modules/ec2_instance/main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
provider "aws" {
region = "us-east-1"
resource "aws_instance" "ec2_instance" {
ami = var.ami_var
instance_type = var.variable_instance_type
subnet_id = var.varble_subnet_id
key_name = var.var_key_name
}

resource "aws_instance" "example" {
ami = var.ami_value
instance_type = var.instance_type_value
subnet_id = var.subnet_id_value
}
6 changes: 3 additions & 3 deletions Day-3/modules/ec2_instance/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
output "public-ip-address" {
value = aws_instance.example.public_ip
}
output "ec2_public_key" {
value = aws_instance.ec2_instance.public_ip
}
3 changes: 3 additions & 0 deletions Day-3/modules/ec2_instance/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "ap-south-1"
}
20 changes: 11 additions & 9 deletions Day-3/modules/ec2_instance/variables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
variable "ami_value" {
description = "value for the ami"
variable "ami_var" {
description = "ami(Amazon Machine Image) contains confiuration to launch a machine"

}

variable "instance_type_value" {
description = "value for instance_type"
variable "var_key_name" {
description = "key value pair to ssh into the aws instance"
}
variable "varble_subnet_id" {
description = "instance will be placed in subnet of the vpc"
}
variable "variable_instance_type" {
description = "this define the size specifications of the ec2 instance"
}

variable "subnet_id_value" {
description = "value for the subnet_id"
}
4 changes: 4 additions & 0 deletions Day-3/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
oami_var="ami-0f58b397bc5c1f2e8"
ovar_key_name="aws_login"
ovarble_subnet_id="subnet-0a94c001e716ebd8b"
ovariable_instance_type="t2.micro"
13 changes: 13 additions & 0 deletions Day-3/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "oami_var" {
description = "ami(Amazon Machine Image) contains confiuration to launch a machine"

}
variable "ovar_key_name" {
description = "key value pair to ssh into the aws instance"
}
variable "ovarble_subnet_id" {
description = "instance will be placed in subnet of the vpc"
}
variable "ovariable_instance_type" {
description = "this define the size specifications of the ec2 instance"
}
86 changes: 40 additions & 46 deletions Day-5/main.tf
Original file line number Diff line number Diff line change
@@ -1,98 +1,93 @@
# Define the AWS provider configuration.
provider "aws" {
region = "us-east-1" # Replace with your desired AWS region.
region = "ap-south-1"
}

variable "cidr" {
variable "cidr"{
default = "10.0.0.0/16"
}

resource "aws_key_pair" "example" {
key_name = "terraform-demo-abhi" # Replace with your desired key name
public_key = file("~/.ssh/id_rsa.pub") # Replace with the path to your public key file
resource "tls_private_key" "example" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = "aws_login"
public_key = tls_private_key.example.public_key_openssh
}

resource "aws_vpc" "myvpc" {
cidr_block = var.cidr
}

resource "aws_subnet" "sub1" {
vpc_id = aws_vpc.myvpc.id
cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
}

resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.myvpc.id
tags = {
"key" = "myigw"
}

}

resource "aws_route_table" "RT" {
resource "aws_subnet" "mysubnet1" {
vpc_id = aws_vpc.myvpc.id

route {
cidr_block = "10.0.0.0/24"
availability_zone = "ap-south-1"
map_public_ip_on_launch = true
}
resource "aws_route_table" "routetb"{
vpc_id = aws_vpc.myvpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}

resource "aws_route_table_association" "rta1" {
subnet_id = aws_subnet.sub1.id
route_table_id = aws_route_table.RT.id
resource "aws_route_table_association" "rta1"{
route_table_id = aws_route_table.routetb.id
subnet_id = aws_subnet.mysubnet1.id
}

resource "aws_security_group" "webSg" {
name = "web"
resource "aws_security_group" "asg" {
name = "web"
vpc_id = aws_vpc.myvpc.id

ingress {
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "Web-sg"
}
}

resource "aws_instance" "server" {
ami = "ami-0261755bbcb8c4a84"
instance_type = "t2.micro"
key_name = aws_key_pair.example.key_name
vpc_security_group_ids = [aws_security_group.webSg.id]
subnet_id = aws_subnet.sub1.id
resource "aws_instance" "my_ec2" {
ami = ""
instance_type = ""
subnet_id = aws_subnet.mysubnet1.id
key_name = aws_key_pair.generated_key.key_name
vpc_security_group_ids = [aws_security_group.asg.id]

connection {
type = "ssh"
user = "ubuntu" # Replace with the appropriate username for your EC2 instance
private_key = file("~/.ssh/id_rsa") # Replace with the path to your private key
private_key = tls_private_key.example.private_key_pem # Replace with the path to your private key
host = self.public_ip
}

# File provisioner to copy a file from local to the remote EC2 instance
provisioner "file" {
# File provisioner to copy a file from local to the remote EC2 instance
provisioner "file"{
source = "app.py" # Replace with the path to your local file
destination = "/home/ubuntu/app.py" # Replace with the path on the remote instance
}

provisioner "remote-exec" {
}
provisioner "remote-exec" {
inline = [
"echo 'Hello from the remote instance'",
"sudo apt update -y", # Update package lists (for ubuntu)
Expand All @@ -103,4 +98,3 @@ resource "aws_instance" "server" {
]
}
}

51 changes: 37 additions & 14 deletions Day-6/main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
provider "aws" {
region = "us-east-1"
module "ec2_instance" {
source = "./modules/ec2_instance"
var_ami = lookup(var.ext_ami, terraform.workspace , "some default if no environment specified" )
var_instance_type = lookup(var.ext_instance_type , terraform.workspace , "default val if no env specifed")
var_key_name = lookup(var.ext_keypem , terraform.workspace , "default value")
var_subnet = lookup(var.ext_subnet , terraform.workspace , "default val")
}

variable "ami" {
description = "value"
variable "ext_ami" {
type = map(string)

default = {
"dev" = "ami-0f58b397bc5c1f2e8"
"stage" = "ami-0f58b397bc5c1f2e8"
"prod" = "ami-0f58b397bc5c1f2e8"

}
}

variable "instance_type" {
description = "value"
variable "ext_instance_type" {
type = map(string)

default = {
"dev" = "t2.micro"
"stage" = "t2.medium"
"prod" = "t2.xlarge"
"stage" = "t2.medium"
"prod" = "t2.large"
}
}

module "ec2_instance" {
source = "./modules/ec2_instance"
ami = var.ami
instance_type = lookup(var.instance_type, terraform.workspace, "t2.micro")
}
variable "ext_subnet" {
type = map(string)
default = {
"dev" = "subnet-0a94c001e716ebd8b"
"stage" = "subnet-0a94c001e716ebd8b"
"prod" = "subnet-0a94c001e716ebd8b"
}
}
variable "ext_keypem" {
type = map(string)
default = {
"dev" ="aws_login"
"stage" = "aws_login"
"prod" = "aws_login"
}
}

//we can create multiple tfvar files for each envronment for setting up values by switching between enviroenmts and while doing
// apply we can supply -tfvar file ="envfilename.tfvars"
18 changes: 6 additions & 12 deletions Day-6/modules/ec2_instance/main.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
provider "aws" {
region = "us-east-1"
region = "ap-south-1"
}

variable "ami" {
description = "This is AMI for the instance"
resource "aws_instance" "ec2_instance" {
ami = var.var_ami
instance_type = var.var_instance_type
key_name = var.var_key_name
subnet_id = var.var_subnet
}

variable "instance_type" {
description = "This is the instance type, for example: t2.micro"
}

resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
}
12 changes: 12 additions & 0 deletions Day-6/modules/ec2_instance/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
variable "var_ami" {
description = "ami of ec2"
}
variable "var_instance_type" {
description = "instance type"
}
variable "var_key_name" {
description="SSH KEY PUBLIC KEY"
}
variable "var_subnet" {
description = "subnet name"
}
22 changes: 22 additions & 0 deletions Day-6/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
to create an environment
cmd > terraform workspace new <workspace name>
eg : terraform workspace new prod

to see on which environment we are in
cmd > terraform workspace show

to list all the available environemnts
cmd > terraform workspace list

to switch from one to another environment
cmd > terraform workspace select <workspace name>

to delete a workspace
cmd > terraform workspace delete <workspace name>


//we can create multiple tfvar files for each envronment for setting up values by switching between enviroenmts and while doing
// apply we can supply -tfvar file ="envfilename.tfvars"

cheat sheet
https://spacelift.io/blog/terraform-commands-cheat-sheet