commit 1e9b274965e6bc40c44bd5249727aca7fff6562e Author: KF Date: Thu May 30 12:01:24 2024 +0800 NEW: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f82f9b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +### Terraform ### +.terraform diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..032097c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..237a950 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Compute/Demo1/.terraform.lock.hcl b/Compute/Demo1/.terraform.lock.hcl new file mode 100644 index 0000000..e3a658f --- /dev/null +++ b/Compute/Demo1/.terraform.lock.hcl @@ -0,0 +1,10 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.51.1" + constraints = ">= 5.0.0" + hashes = [ + "h1:ESfxP2tCO6IZldSQnepXmIm+x+VtaQt/bKgGjYE+0BY=", + ] +} diff --git a/Compute/Demo1/main.tf b/Compute/Demo1/main.tf new file mode 100644 index 0000000..71df742 --- /dev/null +++ b/Compute/Demo1/main.tf @@ -0,0 +1,5 @@ +resource "aws_instance" "example" { + ami = "ami-0157c3cc39a1c5cc0" + instance_type = "t4g.large" + subnet_id = "subnet-0927ba1b06ccfe6c5" +} \ No newline at end of file diff --git a/Compute/Demo1/provider.tf b/Compute/Demo1/provider.tf new file mode 100644 index 0000000..fa025da --- /dev/null +++ b/Compute/Demo1/provider.tf @@ -0,0 +1,28 @@ +provider "aws" { + region = "ap-east-1" + + default_tags { + tags = { + ServiceProvider = "RackspaceTechnology" + Environment = "Training" + Project = "Iac" + TerraformMode = "managed" + Owner = "ken2026" + TerraformDir = "${reverse(split("/", path.cwd))[1]}/${reverse(split("/", path.cwd))[0]}" + } + } +} + +output "last-updated" { + value = timestamp() +} + +terraform { + required_version = ">= 1.3.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.0" + } + } +} \ No newline at end of file diff --git a/Compute/Demo1/terraform.tfstate b/Compute/Demo1/terraform.tfstate new file mode 100644 index 0000000..94aff38 --- /dev/null +++ b/Compute/Demo1/terraform.tfstate @@ -0,0 +1,9 @@ +{ + "version": 4, + "terraform_version": "1.3.9", + "serial": 9, + "lineage": "05a7ba76-3512-c858-95f8-aa6ea555f1ee", + "outputs": {}, + "resources": [], + "check_results": null +} diff --git a/Compute/Demo1/terraform.tfstate.backup b/Compute/Demo1/terraform.tfstate.backup new file mode 100644 index 0000000..9ed8405 --- /dev/null +++ b/Compute/Demo1/terraform.tfstate.backup @@ -0,0 +1,156 @@ +{ + "version": 4, + "terraform_version": "1.3.9", + "serial": 7, + "lineage": "05a7ba76-3512-c858-95f8-aa6ea555f1ee", + "outputs": { + "last-updated": { + "value": "2024-05-30T02:41:22Z", + "type": "string" + } + }, + "resources": [ + { + "mode": "managed", + "type": "aws_instance", + "name": "example", + "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]", + "instances": [ + { + "schema_version": 1, + "attributes": { + "ami": "ami-0157c3cc39a1c5cc0", + "arn": "arn:aws:ec2:ap-east-1:040216112220:instance/i-037e063ccb317f0db", + "associate_public_ip_address": true, + "availability_zone": "ap-east-1a", + "capacity_reservation_specification": [ + { + "capacity_reservation_preference": "open", + "capacity_reservation_target": [] + } + ], + "cpu_core_count": 2, + "cpu_options": [ + { + "amd_sev_snp": "", + "core_count": 2, + "threads_per_core": 1 + } + ], + "cpu_threads_per_core": 1, + "credit_specification": [ + { + "cpu_credits": "unlimited" + } + ], + "disable_api_stop": false, + "disable_api_termination": false, + "ebs_block_device": [], + "ebs_optimized": false, + "enclave_options": [ + { + "enabled": false + } + ], + "ephemeral_block_device": [], + "get_password_data": false, + "hibernation": false, + "host_id": "", + "host_resource_group_arn": null, + "iam_instance_profile": "", + "id": "i-037e063ccb317f0db", + "instance_initiated_shutdown_behavior": "stop", + "instance_lifecycle": "", + "instance_market_options": [], + "instance_state": "running", + "instance_type": "t4g.large", + "ipv6_address_count": 0, + "ipv6_addresses": [], + "key_name": "", + "launch_template": [], + "maintenance_options": [ + { + "auto_recovery": "default" + } + ], + "metadata_options": [ + { + "http_endpoint": "enabled", + "http_protocol_ipv6": "disabled", + "http_put_response_hop_limit": 2, + "http_tokens": "required", + "instance_metadata_tags": "disabled" + } + ], + "monitoring": false, + "network_interface": [], + "outpost_arn": "", + "password_data": "", + "placement_group": "", + "placement_partition_number": 0, + "primary_network_interface_id": "eni-0375c305514c6813c", + "private_dns": "ip-172-20-16-111.ap-east-1.compute.internal", + "private_dns_name_options": [ + { + "enable_resource_name_dns_a_record": false, + "enable_resource_name_dns_aaaa_record": false, + "hostname_type": "ip-name" + } + ], + "private_ip": "172.20.16.111", + "public_dns": "ec2-18-163-2-19.ap-east-1.compute.amazonaws.com", + "public_ip": "18.163.2.19", + "root_block_device": [ + { + "delete_on_termination": true, + "device_name": "/dev/xvda", + "encrypted": false, + "iops": 3000, + "kms_key_id": "", + "tags": {}, + "tags_all": { + "Environment": "Training", + "Owner": "ken2026", + "Project": "Iac", + "ServiceProvider": "RackspaceTechnology", + "TerraformDir": "Compute/Demo1", + "TerraformMode": "managed" + }, + "throughput": 125, + "volume_id": "vol-061192850e49ddff0", + "volume_size": 8, + "volume_type": "gp3" + } + ], + "secondary_private_ips": [], + "security_groups": [], + "source_dest_check": true, + "spot_instance_request_id": "", + "subnet_id": "subnet-0927ba1b06ccfe6c5", + "tags": {}, + "tags_all": { + "Environment": "Training", + "Owner": "ken2026", + "Project": "Iac", + "ServiceProvider": "RackspaceTechnology", + "TerraformDir": "Compute/Demo1", + "TerraformMode": "managed" + }, + "tenancy": "default", + "timeouts": null, + "user_data": null, + "user_data_base64": null, + "user_data_replace_on_change": false, + "volume_tags": null, + "vpc_security_group_ids": [ + "sg-04ec154cb0f516e76" + ] + }, + "sensitive_attributes": [], + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo2MDAwMDAwMDAwMDAsImRlbGV0ZSI6MTIwMDAwMDAwMDAwMCwicmVhZCI6OTAwMDAwMDAwMDAwLCJ1cGRhdGUiOjYwMDAwMDAwMDAwMH0sInNjaGVtYV92ZXJzaW9uIjoiMSJ9" + } + ] + } + ], + "check_results": null +} diff --git a/Compute/Demo2/.terraform.lock.hcl b/Compute/Demo2/.terraform.lock.hcl new file mode 100644 index 0000000..e3a658f --- /dev/null +++ b/Compute/Demo2/.terraform.lock.hcl @@ -0,0 +1,10 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.51.1" + constraints = ">= 5.0.0" + hashes = [ + "h1:ESfxP2tCO6IZldSQnepXmIm+x+VtaQt/bKgGjYE+0BY=", + ] +} diff --git a/Compute/Demo2/main.tf b/Compute/Demo2/main.tf new file mode 100644 index 0000000..e16f1fc --- /dev/null +++ b/Compute/Demo2/main.tf @@ -0,0 +1,14 @@ +resource "aws_instance" "example1" { + count = 2 + ami = "ami-0157c3cc39a1c5cc0" + instance_type = "t4g.large" + subnet_id = "subnet-0927ba1b06ccfe6c5" +} + +resource "aws_instance" "example2" { + for_each = toset(["instance1", "instance2"]) + ami = "ami-0157c3cc39a1c5cc0" + instance_type = "t4g.large" + subnet_id = "subnet-0927ba1b06ccfe6c5" + tags = { Name : each.value } +} \ No newline at end of file diff --git a/Compute/Demo2/provider.tf b/Compute/Demo2/provider.tf new file mode 100644 index 0000000..fa025da --- /dev/null +++ b/Compute/Demo2/provider.tf @@ -0,0 +1,28 @@ +provider "aws" { + region = "ap-east-1" + + default_tags { + tags = { + ServiceProvider = "RackspaceTechnology" + Environment = "Training" + Project = "Iac" + TerraformMode = "managed" + Owner = "ken2026" + TerraformDir = "${reverse(split("/", path.cwd))[1]}/${reverse(split("/", path.cwd))[0]}" + } + } +} + +output "last-updated" { + value = timestamp() +} + +terraform { + required_version = ">= 1.3.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.0" + } + } +} \ No newline at end of file diff --git a/Compute/Demo3/.terraform.lock.hcl b/Compute/Demo3/.terraform.lock.hcl new file mode 100644 index 0000000..ff8e7fc --- /dev/null +++ b/Compute/Demo3/.terraform.lock.hcl @@ -0,0 +1,17 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.51.1" + constraints = ">= 5.0.0" + hashes = [ + "h1:ESfxP2tCO6IZldSQnepXmIm+x+VtaQt/bKgGjYE+0BY=", + ] +} + +provider "registry.terraform.io/hashicorp/tls" { + version = "4.0.5" + hashes = [ + "h1:e4LBdJoZJNOQXPWgOAG0UuPBVhCStu98PieNlqJTmeU=", + ] +} diff --git a/Compute/Demo3/main.tf b/Compute/Demo3/main.tf new file mode 100644 index 0000000..e493aa7 --- /dev/null +++ b/Compute/Demo3/main.tf @@ -0,0 +1,4 @@ +module "ec2-instance" { + source = "../../Modules/Compute/ec2" + name = "demo3" +} \ No newline at end of file diff --git a/Compute/Demo3/provider.tf b/Compute/Demo3/provider.tf new file mode 100644 index 0000000..fa025da --- /dev/null +++ b/Compute/Demo3/provider.tf @@ -0,0 +1,28 @@ +provider "aws" { + region = "ap-east-1" + + default_tags { + tags = { + ServiceProvider = "RackspaceTechnology" + Environment = "Training" + Project = "Iac" + TerraformMode = "managed" + Owner = "ken2026" + TerraformDir = "${reverse(split("/", path.cwd))[1]}/${reverse(split("/", path.cwd))[0]}" + } + } +} + +output "last-updated" { + value = timestamp() +} + +terraform { + required_version = ">= 1.3.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.0" + } + } +} \ No newline at end of file diff --git a/Modules/Compute/ec2/main.tf b/Modules/Compute/ec2/main.tf new file mode 100644 index 0000000..5300b32 --- /dev/null +++ b/Modules/Compute/ec2/main.tf @@ -0,0 +1,26 @@ +resource "aws_instance" "example" { + ami = "ami-0157c3cc39a1c5cc0" + instance_type = "t4g.large" + subnet_id = "subnet-0927ba1b06ccfe6c5" + key_name = aws_key_pair.this.key_name + + # IMDSv2 requirement + dynamic "metadata_options" { + for_each = var.disable_secure_idmsv2 == false ? { set_idmsv2 : true } : {} + content { + http_endpoint = "enabled" + http_tokens = "required" + http_put_response_hop_limit = 2 + } + } + tags = { Name : var.name } +} + +resource "tls_private_key" "this" { + algorithm = "ED25519" +} + +resource "aws_key_pair" "this" { + key_name = "${var.name}-sshkey" + public_key = tls_private_key.this.public_key_openssh +} \ No newline at end of file diff --git a/Modules/Compute/ec2/provider.tf b/Modules/Compute/ec2/provider.tf new file mode 100644 index 0000000..fa025da --- /dev/null +++ b/Modules/Compute/ec2/provider.tf @@ -0,0 +1,28 @@ +provider "aws" { + region = "ap-east-1" + + default_tags { + tags = { + ServiceProvider = "RackspaceTechnology" + Environment = "Training" + Project = "Iac" + TerraformMode = "managed" + Owner = "ken2026" + TerraformDir = "${reverse(split("/", path.cwd))[1]}/${reverse(split("/", path.cwd))[0]}" + } + } +} + +output "last-updated" { + value = timestamp() +} + +terraform { + required_version = ">= 1.3.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.0" + } + } +} \ No newline at end of file diff --git a/Modules/Compute/ec2/variables.tf b/Modules/Compute/ec2/variables.tf new file mode 100644 index 0000000..899f93c --- /dev/null +++ b/Modules/Compute/ec2/variables.tf @@ -0,0 +1,10 @@ +variable "name" { + type = string + description = "Name of Ec2 instance" +} + +variable "disable_secure_idmsv2" { + type = bool + default = false + description = "Allow use of insecure idmsv1. Default is false." +} \ No newline at end of file diff --git a/terraform-training.iml b/terraform-training.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/terraform-training.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file