├── README.md ├── alb ├── main.tf ├── outputs.tf └── variables.tf ├── auto_scaling ├── main.tf └── variables.tf ├── cloudtrail ├── main.tf └── variables.tf ├── cloudwatch ├── main.tf └── variables.tf ├── config └── main.tf ├── ec2 ├── main.tf ├── outputs.tf ├── userdata.tpl └── variables.tf ├── iam ├── main.tf ├── outputs.tf └── variables.tf ├── introduction_to_terraform └── main.tf ├── kms ├── main.tf └── variables.tf ├── main.tf ├── rds ├── main.tf └── variables.tf ├── route53 ├── main.tf └── variables.tf ├── s3 ├── main.tf └── variables.tf ├── sns ├── main.tf ├── outputs.tf └── variables.tf ├── transit_gateway ├── main.tf ├── outputs.tf └── variables.tf └── vpc ├── main.tf ├── outputs.tf └── variables.tf /README.md: -------------------------------------------------------------------------------- 1 | #### 21_days_of_aws_using_terraform 2 | -------------------------------------------------------------------------------- /alb/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-west-2" 3 | } 4 | 5 | resource "aws_lb_target_group" "my-target-group" { 6 | health_check { 7 | interval = 10 8 | path = "/" 9 | protocol = "HTTP" 10 | timeout = 5 11 | healthy_threshold = 5 12 | unhealthy_threshold = 2 13 | } 14 | 15 | name = "my-test-tg" 16 | port = 80 17 | protocol = "HTTP" 18 | target_type = "instance" 19 | vpc_id = "${var.vpc_id}" 20 | } 21 | 22 | /*resource "aws_lb_target_group_attachment" "my-alb-target-group-attachment1" { 23 | target_group_arn = "${aws_lb_target_group.my-target-group.arn}" 24 | target_id = "${var.instance1_id}" 25 | port = 80 26 | } 27 | 28 | resource "aws_lb_target_group_attachment" "my-alb-target-group-attachment2" { 29 | target_group_arn = "${aws_lb_target_group.my-target-group.arn}" 30 | target_id = "${var.instance2_id}" 31 | port = 80 32 | }*/ 33 | 34 | resource "aws_lb" "my-aws-alb" { 35 | name = "my-test-alb" 36 | internal = false 37 | 38 | security_groups = [ 39 | "${aws_security_group.my-alb-sg.id}", 40 | ] 41 | 42 | subnets = [ 43 | "${var.subnet1}", 44 | "${var.subnet2}", 45 | ] 46 | 47 | tags = { 48 | Name = "my-test-alb" 49 | } 50 | 51 | ip_address_type = "ipv4" 52 | load_balancer_type = "application" 53 | } 54 | 55 | resource "aws_lb_listener" "my-test-alb-listner" { 56 | load_balancer_arn = "${aws_lb.my-aws-alb.arn}" 57 | port = 80 58 | protocol = "HTTP" 59 | 60 | default_action { 61 | type = "forward" 62 | target_group_arn = "${aws_lb_target_group.my-target-group.arn}" 63 | } 64 | } 65 | 66 | resource "aws_security_group" "my-alb-sg" { 67 | name = "my-alb-sg" 68 | vpc_id = "${var.vpc_id}" 69 | } 70 | 71 | resource "aws_security_group_rule" "inbound_ssh" { 72 | from_port = 22 73 | protocol = "tcp" 74 | security_group_id = "${aws_security_group.my-alb-sg.id}" 75 | to_port = 22 76 | type = "ingress" 77 | cidr_blocks = ["0.0.0.0/0"] 78 | } 79 | 80 | resource "aws_security_group_rule" "inbound_http" { 81 | from_port = 80 82 | protocol = "tcp" 83 | security_group_id = "${aws_security_group.my-alb-sg.id}" 84 | to_port = 80 85 | type = "ingress" 86 | cidr_blocks = ["0.0.0.0/0"] 87 | } 88 | 89 | resource "aws_security_group_rule" "outbound_all" { 90 | from_port = 0 91 | protocol = "-1" 92 | security_group_id = "${aws_security_group.my-alb-sg.id}" 93 | to_port = 0 94 | type = "egress" 95 | cidr_blocks = ["0.0.0.0/0"] 96 | } 97 | -------------------------------------------------------------------------------- /alb/outputs.tf: -------------------------------------------------------------------------------- 1 | output "alb_dns_name" { 2 | value = "${aws_lb.my-aws-alb.dns_name}" 3 | } 4 | 5 | output "alb_target_group_arn" { 6 | value = "${aws_lb_target_group.my-target-group.arn}" 7 | } 8 | -------------------------------------------------------------------------------- /alb/variables.tf: -------------------------------------------------------------------------------- 1 | variable "vpc_id" {} 2 | 3 | /*variable "instance1_id" {} 4 | variable "instance2_id" {}*/ 5 | variable "subnet1" {} 6 | 7 | variable "subnet2" {} 8 | -------------------------------------------------------------------------------- /auto_scaling/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-west-2" 3 | } 4 | 5 | resource "aws_launch_configuration" "my-test-launch-config" { 6 | image_id = "ami-01ed306a12b7d1c96" 7 | instance_type = "t2.micro" 8 | security_groups = ["${aws_security_group.my-asg-sg.id}"] 9 | 10 | user_data = <<-EOF 11 | #!/bin/bash 12 | yum -y install httpd 13 | echo "Hello, from Terraform" > /var/www/html/index.html 14 | service httpd start 15 | chkconfig httpd on 16 | EOF 17 | 18 | lifecycle { 19 | create_before_destroy = true 20 | } 21 | } 22 | 23 | resource "aws_autoscaling_group" "example" { 24 | launch_configuration = "${aws_launch_configuration.my-test-launch-config.name}" 25 | vpc_zone_identifier = ["${var.subnet1}","${var.subnet2 }"] 26 | target_group_arns = ["${var.target_group_arn}"] 27 | health_check_type = "ELB" 28 | 29 | min_size = 2 30 | max_size = 10 31 | 32 | tag { 33 | key = "Name" 34 | value = "my-test-asg" 35 | propagate_at_launch = true 36 | } 37 | } 38 | 39 | resource "aws_security_group" "my-asg-sg" { 40 | name = "my-asg-sg" 41 | vpc_id = "${var.vpc_id}" 42 | } 43 | 44 | resource "aws_security_group_rule" "inbound_ssh" { 45 | from_port = 22 46 | protocol = "tcp" 47 | security_group_id = "${aws_security_group.my-asg-sg.id}" 48 | to_port = 22 49 | type = "ingress" 50 | cidr_blocks = ["0.0.0.0/0"] 51 | } 52 | 53 | resource "aws_security_group_rule" "inbound_http" { 54 | from_port = 80 55 | protocol = "tcp" 56 | security_group_id = "${aws_security_group.my-asg-sg.id}" 57 | to_port = 80 58 | type = "ingress" 59 | cidr_blocks = ["0.0.0.0/0"] 60 | } 61 | 62 | resource "aws_security_group_rule" "outbound_all" { 63 | from_port = 0 64 | protocol = "-1" 65 | security_group_id = "${aws_security_group.my-asg-sg.id}" 66 | to_port = 0 67 | type = "egress" 68 | cidr_blocks = ["0.0.0.0/0"] 69 | } 70 | -------------------------------------------------------------------------------- /auto_scaling/variables.tf: -------------------------------------------------------------------------------- 1 | variable "vpc_id" {} 2 | 3 | 4 | variable "target_group_arn" {} 5 | 6 | variable "subnet1" {} 7 | variable "subnet2" {} 8 | 9 | 10 | -------------------------------------------------------------------------------- /cloudtrail/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-west-2" 3 | } 4 | 5 | resource "aws_cloudtrail" "my-demo-cloudtrail" { 6 | name = "${var.cloudtrail_name}" 7 | s3_bucket_name = "${aws_s3_bucket.s3_bucket_name.id}" 8 | include_global_service_events = true 9 | is_multi_region_trail = true 10 | enable_log_file_validation = true 11 | } 12 | 13 | resource "aws_s3_bucket" "s3_bucket_name" { 14 | bucket = "${var.s3_bucket_name}" 15 | 16 | policy = <> /etc/fstab 5 | 6 | yum -y install httpd 7 | echo "this is coming from terraform" >> /var/www/html/index.html 8 | service httpd start 9 | chkconfig httpd on -------------------------------------------------------------------------------- /ec2/variables.tf: -------------------------------------------------------------------------------- 1 | variable "my_public_key" {} 2 | 3 | variable "instance_type" {} 4 | 5 | variable "security_group" {} 6 | 7 | variable "subnets" { 8 | type = "list" 9 | } 10 | -------------------------------------------------------------------------------- /iam/main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "us-west-2" 3 | } 4 | 5 | resource "aws_iam_user" "my-test-user" { 6 | name = "${element(var.username,count.index)}" 7 | count = "${length(var.username)}" 8 | } 9 | 10 | resource "aws_iam_role_policy" "my-test-policy" { 11 | name = "my-test-iam-policy" 12 | role = "${aws_iam_role.my-test-iam-role.id}" 13 | 14 | policy = <