├── config.tf ├── userdata.sh ├── oregon-region-key-pair.pub ├── variables.tf ├── vpc.tf ├── README.md ├── asg.tf └── network.tf /config.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "${var.AWS_REGION}" 3 | } -------------------------------------------------------------------------------- /userdata.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sudo yum install httpd -y 3 | sudo chkconfig httpd on 4 | sudo systemctl start httpd -------------------------------------------------------------------------------- /oregon-region-key-pair.pub: -------------------------------------------------------------------------------- 1 | place your public key here gerenerated from command 2 | 3 | ssh-keygen -f oregon-region-key-pair 4 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "AWS_REGION" { 2 | default = "us-west-2" 3 | } 4 | 5 | variable "AMI" { 6 | type = map(string) 7 | 8 | default = { 9 | us-west-2 = "ami-0d593311db5abb72b" 10 | us-east-1 = "ami-0c2a1acae6667e438" 11 | } 12 | } 13 | 14 | variable "PUBLIC_KEY_PATH" { 15 | default = "/Users/praveensingampalli/Documents/BOOTCAMP2_FINAL/Terraform_learning_demo/Terraform_learning_demo/oregon-region-key-pair.pub" 16 | } -------------------------------------------------------------------------------- /vpc.tf: -------------------------------------------------------------------------------- 1 | resource "aws_vpc" "dev-vpc" { 2 | cidr_block = "10.0.0.0/16" 3 | enable_dns_support = "true" #gives you an internal domain name 4 | enable_dns_hostnames = "true" #gives you an internal host name 5 | instance_tenancy = "default" 6 | 7 | tags = { 8 | Name = "dev-vpc" 9 | } 10 | } 11 | 12 | resource "aws_subnet" "dev-subnet-public-1" { 13 | vpc_id = "${aws_vpc.dev-vpc.id}" //Attaching the VPC to subnet 14 | cidr_block = "10.0.1.0/24" 15 | map_public_ip_on_launch = "true" //it makes this a public subnet 16 | availability_zone = "us-west-2a" 17 | tags = { 18 | Name = "dev-subnet-public-1" 19 | } 20 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Search the AMI ID in the AWS and add it in the variables.tf 2 | PUBLIC_KEY_PATH ADD FULL PATH IN VARIABLES.TF-> 3 | -> command to generate the pub file -> ssh-keygen -f oregon-region-key-pair 4 | 5 | 6 | 1. Install terraform - https://www.terraform.io/downloads 7 | 2. Install Python if not already done https://www.python.org/downloads/ 8 | 3. Install aws cli - https://docs.aws.amazon.com/cli/v1/userguide/install-macos.html 9 | 4. Create access key for terraform https://aws.amazon.com/console/ 10 | 5. Use aws configure to configure the access keys [ aws configure command ] 11 | 6. Create a S3 bucket and add the name in config.tf 12 | 7. Define variables.tf and config.tf files. 13 | 8. Create Key pair -> ssh-keygen -f oregon-region-key-pair 14 | 9. Run - terraform init 15 | 10. terraform plan -out "file.plan" 16 | 11. terraform apply 17 | 12. terraform destroy 18 | 13. Define other necessary files. 19 | 20 | 21 | -------------------------------------------------------------------------------- /asg.tf: -------------------------------------------------------------------------------- 1 | resource "aws_launch_template" "dev-launch-config" { 2 | name = "PROD-launch-config" 3 | vpc_security_group_ids = ["${aws_security_group.ssh-allowed.id}"] 4 | user_data = filebase64("${"userdata.sh"}") // Userdata is added in the lauch config and launch added to ASG 5 | # Keep below arguments 6 | instance_type = "t2.micro" 7 | image_id = "${lookup(var.AMI, var.AWS_REGION)}" 8 | key_name = "${aws_key_pair.oregon-region-key-pair.id}" 9 | #associate_public_ip_address = true 10 | 11 | } 12 | 13 | // Sends your public key to the instance 14 | resource "aws_key_pair" "oregon-region-key-pair" { 15 | key_name = "oregon-region-key-pair" 16 | public_key = "${file(var.PUBLIC_KEY_PATH)}" 17 | } 18 | 19 | resource "aws_autoscaling_group" "dev-autoscaling-group-3" { 20 | name = "dev-asg-3" 21 | min_size = "1" 22 | max_size = "1" 23 | #launch_configuration = "${aws_launch_template.dev-launch-config.name}" 24 | launch_template { 25 | id = aws_launch_template.dev-launch-config.id 26 | version = "$Latest" 27 | } 28 | vpc_zone_identifier = ["${aws_subnet.dev-subnet-public-1.id}"] 29 | depends_on = [aws_subnet.dev-subnet-public-1] 30 | tag { 31 | key = "Name" 32 | value = "dev-test" 33 | propagate_at_launch = true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /network.tf: -------------------------------------------------------------------------------- 1 | resource "aws_internet_gateway" "dev-igw" { 2 | vpc_id = "${aws_vpc.dev-vpc.id}" 3 | tags = { 4 | Name = "dev-igw" 5 | } 6 | } 7 | 8 | resource "aws_route_table" "dev-public-crt" { 9 | vpc_id = "${aws_vpc.dev-vpc.id}" 10 | 11 | route { 12 | //associated subnet can reach everywhere 13 | cidr_block = "0.0.0.0/0" 14 | //CRT uses this IGW to reach internet 15 | gateway_id = "${aws_internet_gateway.dev-igw.id}" 16 | } 17 | 18 | tags = { 19 | Name = "dev-public-crt" 20 | } 21 | } 22 | 23 | resource "aws_route_table_association" "dev-crta-public-subnet-1"{ 24 | subnet_id = "${aws_subnet.dev-subnet-public-1.id}" 25 | route_table_id = "${aws_route_table.dev-public-crt.id}" 26 | } 27 | 28 | resource "aws_security_group" "ssh-allowed" { 29 | vpc_id = "${aws_vpc.dev-vpc.id}" 30 | 31 | egress { 32 | from_port = 0 33 | to_port = 0 34 | protocol = -1 35 | cidr_blocks = ["0.0.0.0/0"] 36 | } 37 | ingress { 38 | from_port = 22 39 | to_port = 22 40 | protocol = "tcp" 41 | // This means, all ip address are allowed to ssh ! 42 | // Do not do it in the production. 43 | // Put your office or home address in it! 44 | cidr_blocks = ["0.0.0.0/0"] 45 | } 46 | //If you do not add this rule, you can not reach the NGIX 47 | ingress { 48 | from_port = 80 49 | to_port = 80 50 | protocol = "tcp" 51 | cidr_blocks = ["0.0.0.0/0"] 52 | } 53 | tags = { 54 | Name = "ssh-allowed" 55 | } 56 | } --------------------------------------------------------------------------------