diff --git a/NOTICE b/NOTICE index 547595f909..6e2b44a461 100644 --- a/NOTICE +++ b/NOTICE @@ -1,12 +1,9 @@ -Copyright 2022 LinkedIn Corporation +Copyright 2023 LinkedIn Corporation All Rights Reserved. Licensed under the LinkedIn Learning Exercise File License (the "License"). See LICENSE in the project root for license information. -ATTRIBUTIONS: -[PLEASE PROVIDE ATTRIBUTIONS OR DELETE THIS AND THE ABOVE LINE “ATTRIBUTIONS”] - Please note, this project may automatically load third party code from external repositories (for example, NPM modules, Composer packages, or other dependencies). If so, such third party code may be subject to other license terms than as set diff --git a/README.md b/README.md index a277b16cf0..a13d5279d9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # Learning Terraform -This is the repository for the LinkedIn Learning course `Learning Terraform`. The full course is available from [LinkedIn Learning][lil-course-url]. +This is the repository for the LinkedIn Learning course Learning Terraform. The full course is available from [LinkedIn Learning][lil-course-url]. + +![Learning Terraform][lil-thumbnail-url] + +Terraform is a DevOps tool for declarative infrastructure—infrastructure as code. It simplifies and accelerates the configuration of cloud-based environments. In this course, instructor Josh Samuelson shows how to use Terraform to configure infrastructure and manage resources with Amazon Web Services (AWS). After demonstrating how to set up AWS for Terraform, Josh covers how Terraform manages your infrastructure, as well as how to use core Terraform commands. He also delves into more advanced topics, including how to leverage code modules from the Terraform registry and how to create your own modules. Upon wrapping up this course, you'll have the knowledge you need to efficiently define and manage infrastructure with this powerful tool. _See the readme file in the main branch for updated instructions and information._ ## Instructions @@ -10,8 +14,15 @@ The branches are structured to correspond to the videos in the course. The namin The `main` branch contains the starting code for the course and the `final` branch contains the completed code. -[0]: # (Replace these placeholder URLs with actual course URLs) +### Instructor + +Josh Samuelson + +DevOps Engineer + + -[lil-course-url]: https://www.linkedin.com/learning/ -[lil-thumbnail-url]: http:// +Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/josh-samuelson). +[lil-course-url]: https://www.linkedin.com/learning/learning-terraform-15575129?dApp=59033956 +[lil-thumbnail-url]: https://cdn.lynda.com/course/3087701/3087701-1666200696363-16x9.jpg diff --git a/dev/main.tf b/dev/main.tf new file mode 100644 index 0000000000..deb7e7d8be --- /dev/null +++ b/dev/main.tf @@ -0,0 +1,3 @@ +module "dev" { + source = "../modules/blog" +} \ No newline at end of file diff --git a/dev/outputs.tf b/dev/outputs.tf new file mode 100644 index 0000000000..73e1256158 --- /dev/null +++ b/dev/outputs.tf @@ -0,0 +1,7 @@ +output "env_url" { + value = module.dev.environment_url +} + +output "target_grp_name" { + value = module.dev.target_group_name +} \ No newline at end of file diff --git a/providers.tf b/dev/providers.tf similarity index 84% rename from providers.tf rename to dev/providers.tf index c41e3650b5..fd07223def 100644 --- a/providers.tf +++ b/dev/providers.tf @@ -2,6 +2,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" + version = "~> 6.0" } } } diff --git a/main.tf b/main.tf deleted file mode 100644 index 9b32ce06bb..0000000000 --- a/main.tf +++ /dev/null @@ -1,24 +0,0 @@ -data "aws_ami" "app_ami" { - most_recent = true - - filter { - name = "name" - values = ["bitnami-tomcat-*-x86_64-hvm-ebs-nami"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["979382823631"] # Bitnami -} - -resource "aws_instance" "web" { - ami = data.aws_ami.app_ami.id - instance_type = "t3.nano" - - tags = { - Name = "HelloWorld" - } -} diff --git a/modules/blog/main.tf b/modules/blog/main.tf new file mode 100644 index 0000000000..4dc2ea07cb --- /dev/null +++ b/modules/blog/main.tf @@ -0,0 +1,100 @@ +data "aws_ami" "app_ami" { + most_recent = true + + filter { + name = "name" + values = [var.ami_filter.name] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = [var.ami_filter.owner] # Bitnami +} + + +module "blog_vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "${var.environment.name}-blog-vpc" + cidr = "${var.environment.network_prefix}.0.0/16" + + azs = ["us-west-2a","us-west-2b","us-west-2c"] + public_subnets = ["${var.environment.network_prefix}.101.0/24", "${var.environment.network_prefix}.102.0/24", "${var.environment.network_prefix}.103.0/24"] + + + tags = { + Terraform = "true" + Environment = var.environment.name + } +} + + +module "blog_autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "9.0.1" + + name = "${var.environment.name}-blog-asg" + + min_size = var.asg_min + max_size = var.asg_max + vpc_zone_identifier = module.blog_vpc.public_subnets + + traffic_source_attachments = { + alb = { + traffic_source_identifier = module.blog_alb.target_group_arns[0] + } + } + + security_groups = [module.blog_sg.security_group_id] + instance_type = var.instance_type + image_id = data.aws_ami.app_ami.id +} + +module "blog_alb" { + source = "terraform-aws-modules/alb/aws" + version = "~> 6.0" + + name = "${var.environment.name}-blog-alb" + + load_balancer_type = "application" + + vpc_id = module.blog_vpc.vpc_id + subnets = module.blog_vpc.public_subnets + security_groups = [module.blog_sg.security_group_id] + + target_groups = [ + { + name_prefix = "blog-" + backend_protocol = "HTTP" + backend_port = 80 + target_type = "instance" + } + ] + + http_tcp_listeners = [ + { + port = 80 + protocol = "HTTP" + target_group_index = 0 + } + ] + + tags = { + Environment = var.environment.name + } +} + +module "blog_sg" { + source = "terraform-aws-modules/security-group/aws" + version = "4.13.0" + + vpc_id = module.blog_vpc.vpc_id + name = "${var.environment.name}-blog-sg" + ingress_rules = ["https-443-tcp","http-80-tcp"] + ingress_cidr_blocks = ["0.0.0.0/0"] + egress_rules = ["all-all"] + egress_cidr_blocks = ["0.0.0.0/0"] +} diff --git a/modules/blog/outputs.tf b/modules/blog/outputs.tf new file mode 100644 index 0000000000..585e2b7f34 --- /dev/null +++ b/modules/blog/outputs.tf @@ -0,0 +1,7 @@ +output "environment_url" { + value = module.blog_alb.lb_dns_name +} + +output "target_group_name" { + value = module.blog_alb.target_group_names +} \ No newline at end of file diff --git a/modules/blog/variables.tf b/modules/blog/variables.tf new file mode 100644 index 0000000000..1fed6f2562 --- /dev/null +++ b/modules/blog/variables.tf @@ -0,0 +1,41 @@ +variable "instance_type" { + description = "Type of EC2 instance to provision" + default = "t3.nano" +} + +variable "ami_filter" { + description = "Name filter and owner for AMI" + + type = object ({ + name = string + owner = string + }) + + default = { + name = "bitnami-tomcat-*-x86_64-hvm-ebs-nami" + owner = "979382823631" # Bitnami + } +} + +variable "environment" { + description = "Deployment environment" + + type = object ({ + name = string + network_prefix = string + }) + default = { + name = "dev" + network_prefix = "10.0" + } +} + +variable "asg_min" { + description = "Minimum instance count for the ASG" + default = 1 +} + +variable "asg_max" { + description = "Maximum instance count for the ASG" + default = 2 +} diff --git a/outputs.tf b/outputs.tf deleted file mode 100644 index b35171bef1..0000000000 --- a/outputs.tf +++ /dev/null @@ -1,7 +0,0 @@ -#output "instance_ami" { -# value = aws_instance.web.ami -#} - -#output "instance_arn" { -# value = aws_instance.web.arn -#} diff --git a/qa/main.tf b/qa/main.tf new file mode 100644 index 0000000000..cae44ce6fb --- /dev/null +++ b/qa/main.tf @@ -0,0 +1,12 @@ +module "qa" { + source = "../modules/blog" + + environment = { + name = "qa" + network_prefix = "10.1" + } + + asg_min = 1 + asg_max = 1 + +} \ No newline at end of file diff --git a/qa/outputs.tf b/qa/outputs.tf new file mode 100644 index 0000000000..9d8cda671f --- /dev/null +++ b/qa/outputs.tf @@ -0,0 +1,7 @@ +output "environment_url" { + value = module.qa.environment_url +} + +output "env_url" { + value = module.qa.environment_url +} \ No newline at end of file diff --git a/qa/providers.tf b/qa/providers.tf new file mode 100644 index 0000000000..fd07223def --- /dev/null +++ b/qa/providers.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.0" + } + } +} + +provider "aws" { + region = "us-west-2" +} diff --git a/variables.tf b/variables.tf deleted file mode 100644 index c750667e0f..0000000000 --- a/variables.tf +++ /dev/null @@ -1,4 +0,0 @@ -#variable "instance_type" { -# description = "Type of EC2 instance to provision" -# default = "t3.nano" -#}