From 7d39d761761d44dffcadff7b4c17d70caaa0d45f Mon Sep 17 00:00:00 2001 From: Burse97 Date: Sun, 12 Feb 2023 14:35:57 +0000 Subject: [PATCH 1/5] create a custom module for ec2 instance with a amazon linux 2 ami ID --- .gitignore | 29 +++++++++++++++++++++ .terraform.lock.hcl | 25 +++++++++++++++++++ README.md | 1 + Variables.tf | 19 ++++++++++++++ child-main.tf | 5 ++++ ec2.tf | 24 ------------------ main.tf | 10 ++++++++ module-ec2/main.tf | 55 ++++++++++++++++++++++++++++++++++++++++ outputs.tf | 9 +++++++ provider.tf | 15 +++++++++++ resize.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 229 insertions(+), 24 deletions(-) create mode 100644 .gitignore create mode 100644 .terraform.lock.hcl create mode 100644 README.md create mode 100644 Variables.tf create mode 100644 child-main.tf create mode 100644 main.tf create mode 100644 module-ec2/main.tf create mode 100644 outputs.tf create mode 100644 provider.tf create mode 100755 resize.sh 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..fdc3dfe --- /dev/null +++ b/Variables.tf @@ -0,0 +1,19 @@ +#---ChildModule/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/child-main.tf b/child-main.tf new file mode 100644 index 0000000..0ee1a33 --- /dev/null +++ b/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/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..6c14ba1 --- /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" + } +} \ No newline at end of file diff --git a/module-ec2/main.tf b/module-ec2/main.tf new file mode 100644 index 0000000..25f0fe7 --- /dev/null +++ b/module-ec2/main.tf @@ -0,0 +1,55 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.27" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = "us-east-1" +} + +resource "aws_instance" "db_server" { + ami = var.ami_id + instance_type = var.instance + + tags = { + name = db.tag + } +} + +variable "ami_id" { + description = "ec2 ami_id" + type = sting + default = "ami-033cfb3e88287e319" +} + +variable "instance" { + description = "instance type" + type = sting + default = "t2.micro" +} + +variable "region" { + description = "instance region" + type = sting + default = "us-east-1" +} + +module "terraformec2" { + source = "./terraform-module/terraformec2/" +} + +output " " { + description = "ec2 public ip" + value =aws_instance.db_server.public_ip +} + +output "ec2_tags" { + value = aws_instance.db_server.tags_all.Name +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..052f3b1 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,9 @@ +#---ChildModule/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..f31f877 --- /dev/null +++ b/provider.tf @@ -0,0 +1,15 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 4.0" + } + } + + required_version = ">= 0.14.9" +} + +provider "aws" { + profile = "default" + region = "us-east-1" +} \ No newline at end of file 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 From fb1a2b1e8e5440fe0916784a555db8a7758f340a Mon Sep 17 00:00:00 2001 From: Burse97 Date: Sun, 12 Feb 2023 15:17:54 +0000 Subject: [PATCH 2/5] create a custom module for ec2 instance with a amazon linux 2 ami ID --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 6c14ba1..d420cb6 100644 --- a/main.tf +++ b/main.tf @@ -7,4 +7,4 @@ resource "aws_instance" "db_server" { tags = { Name = "db_tag" } -} \ No newline at end of file +} From 9347ef02afc1d7a1646689a513c20ea96d65cdc5 Mon Sep 17 00:00:00 2001 From: Burse97 Date: Tue, 14 Feb 2023 04:27:39 +0000 Subject: [PATCH 3/5] example --- child-main.tf | 5 ----- main.tf | 2 +- provider.tf | 2 +- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/child-main.tf b/child-main.tf index 0ee1a33..e69de29 100644 --- a/child-main.tf +++ b/child-main.tf @@ -1,5 +0,0 @@ -#---ChildModule/main.tf - -module "terraformec2" { - source = "./module-ec2/terraformec2/" -} \ No newline at end of file diff --git a/main.tf b/main.tf index d420cb6..6c14ba1 100644 --- a/main.tf +++ b/main.tf @@ -7,4 +7,4 @@ resource "aws_instance" "db_server" { tags = { Name = "db_tag" } -} +} \ No newline at end of file diff --git a/provider.tf b/provider.tf index f31f877..4a84cdb 100644 --- a/provider.tf +++ b/provider.tf @@ -12,4 +12,4 @@ terraform { provider "aws" { profile = "default" region = "us-east-1" -} \ No newline at end of file +} From c5ad282d9f37fde159c7c84df40e47d9aab7f7b8 Mon Sep 17 00:00:00 2001 From: Burse97 Date: Wed, 15 Feb 2023 03:51:44 +0000 Subject: [PATCH 4/5] Updated Terraform --- Variables.tf | 2 +- child-main.tf | 0 module-ec2/child-Variables.tf | 5 ++++ module-ec2/child-main.tf | 5 ++++ module-ec2/main.tf | 55 ----------------------------------- outputs.tf | 2 +- provider.tf | 2 ++ 7 files changed, 14 insertions(+), 57 deletions(-) delete mode 100644 child-main.tf create mode 100644 module-ec2/child-Variables.tf create mode 100644 module-ec2/child-main.tf delete mode 100644 module-ec2/main.tf diff --git a/Variables.tf b/Variables.tf index fdc3dfe..b3a50cc 100644 --- a/Variables.tf +++ b/Variables.tf @@ -1,4 +1,4 @@ -#---ChildModule/Variables.tf +#--- root/variables.tf variable "ami_id" { description = "ec2 ami_id" diff --git a/child-main.tf b/child-main.tf deleted file mode 100644 index e69de29..0000000 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/module-ec2/main.tf b/module-ec2/main.tf deleted file mode 100644 index 25f0fe7..0000000 --- a/module-ec2/main.tf +++ /dev/null @@ -1,55 +0,0 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 3.27" - } - } - - required_version = ">= 0.14.9" -} - -provider "aws" { - profile = "default" - region = "us-east-1" -} - -resource "aws_instance" "db_server" { - ami = var.ami_id - instance_type = var.instance - - tags = { - name = db.tag - } -} - -variable "ami_id" { - description = "ec2 ami_id" - type = sting - default = "ami-033cfb3e88287e319" -} - -variable "instance" { - description = "instance type" - type = sting - default = "t2.micro" -} - -variable "region" { - description = "instance region" - type = sting - default = "us-east-1" -} - -module "terraformec2" { - source = "./terraform-module/terraformec2/" -} - -output " " { - description = "ec2 public ip" - value =aws_instance.db_server.public_ip -} - -output "ec2_tags" { - value = aws_instance.db_server.tags_all.Name -} diff --git a/outputs.tf b/outputs.tf index 052f3b1..efd3f0b 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,4 +1,4 @@ -#---ChildModule/outputs.tf +#---root/outputs.tf output "public_ip" { value = aws_instance.db_server.public_ip diff --git a/provider.tf b/provider.tf index 4a84cdb..c005087 100644 --- a/provider.tf +++ b/provider.tf @@ -1,3 +1,5 @@ +#---root/provider.tf + terraform { required_providers { aws = { From dbbce941ea86e5d4b38d10fd17b41a5141fc13d1 Mon Sep 17 00:00:00 2001 From: Burse97 Date: Wed, 15 Feb 2023 03:54:43 +0000 Subject: [PATCH 5/5] Updated Terraform --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 6c14ba1..d420cb6 100644 --- a/main.tf +++ b/main.tf @@ -7,4 +7,4 @@ resource "aws_instance" "db_server" { tags = { Name = "db_tag" } -} \ No newline at end of file +}