From 567af8248b037f237f5bfefeaf4dd9819b8ea5d6 Mon Sep 17 00:00:00 2001 From: poonsalai <105905102+poonsalai@users.noreply.github.com> Date: Fri, 20 May 2022 12:44:39 +0530 Subject: [PATCH] Add files via upload --- ex1/main.tf | 141 +++++++++++++++++++++++++++++++++++++++++++++++ ex1/output.tf | 11 ++++ ex1/terraform.tf | 26 +++++++++ ex2/main.tf | 16 ++++++ ex2/output.tf | 20 +++++++ ex2/terraform.tf | 21 +++++++ ex2/variables.tf | 0 ex3/ex3.py | 22 ++++++++ 8 files changed, 257 insertions(+) create mode 100644 ex1/main.tf create mode 100644 ex1/output.tf create mode 100644 ex1/terraform.tf create mode 100644 ex2/main.tf create mode 100644 ex2/output.tf create mode 100644 ex2/terraform.tf create mode 100644 ex2/variables.tf create mode 100644 ex3/ex3.py diff --git a/ex1/main.tf b/ex1/main.tf new file mode 100644 index 0000000..4691286 --- /dev/null +++ b/ex1/main.tf @@ -0,0 +1,141 @@ +data "aws_vpc" "selected" { + default = true +} + +data "aws_subnet_ids" "ex1_subnet_ids" { + vpc_id = data.aws_vpc.selected.id +} + +# creating a target group for LB +resource "aws_lb_target_group" "ex1" { + name = "ex1-target-group" + port = 80 + protocol = "HTTP" + vpc_id = data.aws_vpc.selected.id +} + + +# checking ami +data "aws_ami" "ex1" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] + + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + owners = ["099720109477"] # Canonical +} + +# AWS launch template with above AMI + +resource "aws_launch_template" "ex1" { + name_prefix = "ex1" + image_id = data.aws_ami.ex1.id + instance_type = "t2.micro" + #user_data = filebase64("${path.module}/example.sh") --> This can be used to make a connection to DB and run an operation + # This is why we have kept a dependency of DB on auto_scaling_group +} + + +# creating auto scaling group + +resource "aws_autoscaling_group" "ex1" { + availability_zones = ["ap-south-1a", "ap-south-1b", "ap-south-1c"] + desired_capacity = 2 + max_size = 4 + min_size = 1 + + launch_template { + id = aws_launch_template.ex1.id + version = "$Latest" + } + target_group_arns = [aws_lb_target_group.ex1.arn] + # below dependency is so that we can run anything on DB and it is already UP + depends_on = [aws_db_instance.ex1] +} + +# create sec group with igress port 80 open and it gets traffic from within vpc, from ALB - to be used by EC2 +resource "aws_security_group" "ex1" { + name = "ex1-ec2" + vpc_id = data.aws_vpc.selected.id + + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = [data.aws_vpc.selected.cidr_block] + } + + egress { + from_port = 0 + to_port = 65322 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "allow_tls" + } +} + +# create sec group with igress port 3306 open and it gets traffic from ec2 security group only, from ALB - to be used by DB +resource "aws_security_group" "ex2" { + name = "ex2-db" + vpc_id = data.aws_vpc.selected.id + + ingress { + from_port = 3306 + to_port = 3306 + protocol = "tcp" + security_groups = [aws_security_group.ex1.id] + } + + tags = { + Name = "allow_tls" + } +} + + +# Creating an LB +resource "aws_lb" "ex1" { + name = "lb-ex1" + internal = false + load_balancer_type = "application" + #security_groups = [aws_security_group.lb_sg.id] + #subnets = [for subnet in aws_subnet.public : subnet.id] + subnets = data.aws_subnet_ids.ex1_subnet_ids.ids + enable_deletion_protection = false +} + +# creating LB listner which listenes to request and send it to target group +resource "aws_lb_listener" "ex1" { + load_balancer_arn = aws_lb.ex1.arn + port = "80" + protocol = "HTTP" + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.ex1.arn + } +} + + +# create a db instance + +resource "aws_db_instance" "ex1" { + allocated_storage = 10 + engine = "mysql" + engine_version = "5.7" + instance_class = "db.t3.micro" + name = "mydb" + username = "foo" + password = "foobarbaz" + parameter_group_name = "default.mysql5.7" + skip_final_snapshot = true +} + diff --git a/ex1/output.tf b/ex1/output.tf new file mode 100644 index 0000000..d887ca2 --- /dev/null +++ b/ex1/output.tf @@ -0,0 +1,11 @@ +output "vpc_name" { + value = data.aws_vpc.selected.id +} + +output "subnet_ids" { + value = data.aws_subnet_ids.ex1_subnet_ids.ids +} + +output "target_group" { + value = aws_lb_target_group.ex1.id +} \ No newline at end of file diff --git a/ex1/terraform.tf b/ex1/terraform.tf new file mode 100644 index 0000000..e06b3d9 --- /dev/null +++ b/ex1/terraform.tf @@ -0,0 +1,26 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} + +provider "aws" { + region = "ap-south-1" + shared_config_files = ["./.aws/conf"] + shared_credentials_files = ["./.aws/credentials"] + + assume_role { + # The role ARN within Account B to AssumeRole into. Created in step 1. + role_arn = "arn:aws:iam::441029311384:role/AdminRoleEx1" + + } +} +/* +provider "google" { +project = "omproject-350619" +region = "us-central1" +zone = "us-central1-c" +} +*/ \ No newline at end of file diff --git a/ex2/main.tf b/ex2/main.tf new file mode 100644 index 0000000..3263266 --- /dev/null +++ b/ex2/main.tf @@ -0,0 +1,16 @@ +# AWS +# Here we have an instance in stopped state on aws and getting the instance_type and state of it in output.tf +data "aws_instance" "ex2" { + filter { + name = "tag:Name" + values = ["ex2aws"] + } +} + +/* +# GCP +# Here we have an instance in stopped state on gcp and getting the machine_type and description of it in output.tf +data "google_compute_instance" "ex2" { + name = "ex2gcp" +} +*/ \ No newline at end of file diff --git a/ex2/output.tf b/ex2/output.tf new file mode 100644 index 0000000..fbdb9ca --- /dev/null +++ b/ex2/output.tf @@ -0,0 +1,20 @@ + +# outputs for AWS instance +output "aws-ec2-type" { + value = "${data.aws_instance.ex2.instance_type}" +} +output "aws-ec2-state" { + value = "${data.aws_instance.ex2.instance_state}" +} + +/* below outputs can be used while fetching details for gcp instance + +# outputs for GCP instance +output "gcp-vm-type" { + value = "${data.google_compute_instance.ex2.machine_type}" +} +output "gcp-vm-desc" { + value = "${data.google_compute_instance.ex2.description}" +} + +*/ \ No newline at end of file diff --git a/ex2/terraform.tf b/ex2/terraform.tf new file mode 100644 index 0000000..bbe1b08 --- /dev/null +++ b/ex2/terraform.tf @@ -0,0 +1,21 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} + +provider "aws" { + region = "ap-south-1" + shared_config_files = ["./.aws/conf"] + shared_credentials_files = ["./.aws/credentials"] + +} +/* +provider "google" { +project = "omproject-350619" +region = "us-central1" +zone = "us-central1-c" +} +*/ \ No newline at end of file diff --git a/ex2/variables.tf b/ex2/variables.tf new file mode 100644 index 0000000..e69de29 diff --git a/ex3/ex3.py b/ex3/ex3.py new file mode 100644 index 0000000..54bd8ba --- /dev/null +++ b/ex3/ex3.py @@ -0,0 +1,22 @@ +# This function returns the value to the supplied key as mentioned in below test cases +def read_obj(objectn,keyn): + all_keys=keyn.split("/") + print(all_keys) + for key in all_keys: + res=objectn[key] + objectn=res + return res + #print("res is --> {}".format(res)) + #print("key is --> {}".format(key)) + + + +##### Test 1 --> first object #### +object1 = {"a":{"b":{"c":"d"}}} +key1="a/b/c" +print("Value would be --> {}".format(read_obj(object1,key1))) + +### Test 2 --> second object ### +object2 = {"x":{"y":{"z":"a"}}} +key2="x/y/z" +print("Value would be --> {}".format(read_obj(object2,key2))) \ No newline at end of file