diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a3e2fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Ignore any .tfvars files that are generated automatically for each Terraform run. Most +# .tfvars files are managed as part of configuration and so should be included in +# version control. +# +# example.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..d704305 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.54.0" + constraints = "~> 4.0" + hashes = [ + "h1:j/L01+hlHVM2X2VrkQC2WtMZyu4ZLhDMw+HDJ7k0Y2Q=", + "zh:24358aefc06b3f38878680fe606dab2570cb58ab952750c47e90b81d3b05e606", + "zh:3fc0ef459d6bb4fbb0e4eb7b8adadddd636efa6d975be6e70de7327d83e15729", + "zh:67e765119726f47b1916316ac95c3cd32ac074b454f2a67b6127120b476bc483", + "zh:71aed1300debac24f11263a6f8a231c6432497b25e623e8f34e27121af65f523", + "zh:722043077e63713d4e458f3228be30c21fcff5b6660c6de8b96967337cdc604a", + "zh:76d67be4220b93cfaca0882f46db9a42b4ca48285a64fe304f108dde85f4d611", + "zh:81534c18d9f02648b1644a7937e7bea56e91caef13b41de121ee51168faad680", + "zh:89983ab2596846d5f3413ff1b5b9b21424c3c757a54dcc5a4604d3ac34fea1a6", + "zh:8a603ac6884de5dc51c372f641f9613aefd87059ff6e6a74b671f6864226e06f", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:b6fae6c1cda6d842406066dac7803d24a597b62da5fae33bcd50c5dae70140c2", + "zh:bc4c3b4bfb715beecf5186dfeb91173ef1a9c0b68e8c45cbeee180195bbfa37f", + "zh:c741a3fe7d085593a160e79596bd237afc9503c836abcc95fd627554cdf16ec0", + "zh:f6763e96485e1ea5b67a33bbd04042e412508b2b06946acf957fb68a314d893e", + "zh:fc7144577ea7d6e05c276b54a9f8f8609be7b4d0a128aa45f233a4b0e5cbf090", + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c22d1e --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# DBurse-Terraform \ No newline at end of file diff --git a/Variables.tf b/Variables.tf new file mode 100644 index 0000000..b3a50cc --- /dev/null +++ b/Variables.tf @@ -0,0 +1,19 @@ +#--- root/variables.tf + +variable "ami_id" { + description = "ec2 ami_id" + type = string + default = "ami-033cfb3e88287e319" +} + +variable "instance" { + description = "instance type" + type = string + default = "t2.micro" +} + +variable "region" { + description = "instance region" + type = string + default = "us-east-1" +} \ No newline at end of file diff --git a/ec2.tf b/ec2.tf index f55bdd8..e69de29 100644 --- a/ec2.tf +++ b/ec2.tf @@ -1,24 +0,0 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 3.27" - } - } - - required_version = ">= 0.14.9" -} - -provider "aws" { - profile = "default" - region = "us-west-2" -} - -resource "aws_instance" "app_server" { - ami = "ami-830c94e3" - instance_type = "t2.micro" - - tags = { - Name = "ExampleAppServerInstance" - } -} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..d420cb6 --- /dev/null +++ b/main.tf @@ -0,0 +1,10 @@ +#---root/main.tf + +resource "aws_instance" "db_server" { + ami = "ami-033cfb3e88287e319" + instance_type = "t2.micro" + + tags = { + Name = "db_tag" + } +} diff --git a/module-ec2/child-Variables.tf b/module-ec2/child-Variables.tf new file mode 100644 index 0000000..14a31f7 --- /dev/null +++ b/module-ec2/child-Variables.tf @@ -0,0 +1,5 @@ +#---ChildModule/Variables.tf + +variable "ami_id" {} +variable "instance" {} +variable "region" {} \ No newline at end of file diff --git a/module-ec2/child-main.tf b/module-ec2/child-main.tf new file mode 100644 index 0000000..d20bbb1 --- /dev/null +++ b/module-ec2/child-main.tf @@ -0,0 +1,5 @@ +#---ChildModule/main.tf + +module "terraformec2" { + source = "./module-ec2/terraformec2/" +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..efd3f0b --- /dev/null +++ b/outputs.tf @@ -0,0 +1,9 @@ +#---root/outputs.tf + +output "public_ip" { + value = aws_instance.db_server.public_ip +} + +output "ec2_tags" { + value = aws_instance.db_server.tags_all.Name +} \ No newline at end of file diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..c005087 --- /dev/null +++ b/provider.tf @@ -0,0 +1,17 @@ +#---root/provider.tf + +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 4.0" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = "us-east-1" +} diff --git a/resize.sh b/resize.sh new file mode 100755 index 0000000..fe1ecb6 --- /dev/null +++ b/resize.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB. +SIZE=${1:-20} + +# Get the ID of the environment host Amazon EC2 instance. +INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id) +REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/region) + +# Get the ID of the Amazon EBS volume associated with the instance. +VOLUMEID=$(aws ec2 describe-instances \ + --instance-id $INSTANCEID \ + --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \ + --output text \ + --region $REGION) + +# Resize the EBS volume. +aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE + +# Wait for the resize to finish. +while [ \ + "$(aws ec2 describe-volumes-modifications \ + --volume-id $VOLUMEID \ + --filters Name=modification-state,Values="optimizing","completed" \ + --query "length(VolumesModifications)"\ + --output text)" != "1" ]; do +sleep 1 +done + +#Check if we're on an NVMe filesystem +if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]] +then + # Rewrite the partition table so that the partition takes up all the space that it can. + sudo growpart /dev/xvda 1 + + # Expand the size of the file system. + # Check if we're on AL2 + STR=$(cat /etc/os-release) + SUB="VERSION_ID=\"2\"" + if [[ "$STR" == *"$SUB"* ]] + then + sudo xfs_growfs -d / + else + sudo resize2fs /dev/xvda1 + fi + +else + # Rewrite the partition table so that the partition takes up all the space that it can. + sudo growpart /dev/nvme0n1 1 + + # Expand the size of the file system. + # Check if we're on AL2 + STR=$(cat /etc/os-release) + SUB="VERSION_ID=\"2\"" + if [[ "$STR" == *"$SUB"* ]] + then + sudo xfs_growfs -d / + else + sudo resize2fs /dev/nvme0n1p1 + fi +fi \ No newline at end of file