Skip to content

Commit eb9dc31

Browse files
authored
Merge pull request easyawslearn#27 from easyawslearn/aws-alb
Aws alb
2 parents 33034c8 + 5b6d411 commit eb9dc31

File tree

8 files changed

+300
-1
lines changed

8 files changed

+300
-1
lines changed

.github/workflows/terraform.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ jobs:
1818
with:
1919
version: "0.12.15"
2020
- name: Build module 'aws-instance-first-script'
21-
run: cd aws-instance-first-script && terraform init && terraform validate && terraform plan
21+
run: cd aws-instance-first-script && terraform init && terraform validate && terraform plan -out plan_terraform && terraform apply plan_terraform
2222
- name: Build module 'aws-EC2-with-jenkins'
2323
run: cd EC2withJenkins && terraform init && terraform validate && terraform plan
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pipeline {
2+
parameters {
3+
string(defaultValue: 'vijaysnode', description: 'select node', name: 'node')
4+
}
5+
6+
agent {
7+
node {
8+
label params.node
9+
}
10+
}
11+
stages{
12+
stage('terraform-Demo') {
13+
steps {
14+
script {
15+
dir("ta-setup")
16+
{
17+
git branch: 'tf-jenkins', url: 'https://github.com/easyawslearn/Terraform-Tutorial.git'
18+
sh 'cd EC2withJenkins '
19+
sh 'terraform init -upgrade=true -get=true -input=false -force-copy'
20+
sh 'terraform workspace new "terraform-demo"'
21+
sh 'echo "INFO: New terraform-demo workspace added."'
22+
sh 'terraform workspace select terraform-demo'
23+
sh 'echo "INFO: Terraform -> Planning..."'
24+
sh 'terraform plan -out plan_terraform -lock=true'
25+
sh 'echo "INFO: Terraform -> Executing..."'
26+
sh 'terraform apply plan_plan_terraform'
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}

terraform-aws-elb-alb/elb.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
resource "aws_lb" "elb_example" {
6+
name = "elb"
7+
internal = false
8+
load_balancer_type = "application"
9+
security_groups = [aws_security_group.elb_sg.id]
10+
subnets = [aws_subnet.public_1.id,aws_subnet.public_2.id]
11+
12+
enable_deletion_protection = true
13+
tags = {
14+
Environment = "elb-example"
15+
}
16+
}
17+
18+
resource "aws_lb_target_group" "test" {
19+
name = "tf-example-lb-tg"
20+
port = 80
21+
protocol = "HTTP"
22+
target_type="instance"
23+
vpc_id = aws_vpc.vpc_demo.id
24+
}
25+
26+
resource "aws_lb_target_group_attachment" "test" {
27+
target_group_arn = aws_lb_target_group.test.arn
28+
target_id = aws_instance.elb_instance_example1.id
29+
port = 80
30+
}
31+
resource "aws_lb_target_group_attachment" "test1" {
32+
target_group_arn = aws_lb_target_group.test.arn
33+
target_id = aws_instance.elb_instance_example2.id
34+
port = 80
35+
}
36+
37+
resource "aws_lb_listener" "front_end" {
38+
load_balancer_arn = aws_lb.elb_example.arn
39+
port = "80"
40+
protocol = "HTTP"
41+
42+
default_action {
43+
type = "forward"
44+
target_group_arn = aws_lb_target_group.test.arn
45+
46+
}
47+
}
48+
49+
output "elb_example" {
50+
description = "The DNS name of the ELB"
51+
value = aws_lb.elb_example.dns_name
52+
}

terraform-aws-elb-alb/instances.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
resource "aws_instance" "elb_instance_example1" {
2+
ami = lookup(var.ami_id, var.region)
3+
instance_type = var.instance_type
4+
subnet_id = aws_subnet.public_1.id
5+
6+
# Security group assign to instance
7+
vpc_security_group_ids = [aws_security_group.elb_sg.id]
8+
9+
# key name
10+
key_name = var.key_name
11+
12+
user_data = <<EOF
13+
#! /bin/bash
14+
sudo yum update -y
15+
sudo yum install -y httpd.x86_64
16+
sudo service httpd start
17+
sudo service httpd enable
18+
echo "<h1>Deployed ELB Instance Example 1</h1>" | sudo tee /var/www/html/index.html
19+
EOF
20+
21+
tags = {
22+
Name = "EC2-Instance-1"
23+
}
24+
}
25+
26+
resource "aws_instance" "elb_instance_example2" {
27+
ami = lookup(var.ami_id, var.region)
28+
instance_type = var.instance_type
29+
subnet_id = aws_subnet.public_1.id
30+
31+
# Security group assign to instance
32+
vpc_security_group_ids = [aws_security_group.elb_sg.id]
33+
34+
# key name
35+
key_name = var.key_name
36+
37+
user_data = <<EOF
38+
#! /bin/bash
39+
sudo yum update -y
40+
sudo yum install -y httpd.x86_64
41+
sudo service httpd start
42+
sudo service httpd enable
43+
echo "<h1>Deployed ELB Instance Example 2</h1>" | sudo tee /var/www/html/index.html
44+
EOF
45+
46+
tags = {
47+
Name = "EC2-Instance-1"
48+
}
49+
}

terraform-aws-elb-alb/route53.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "aws_route53_zone" "easy_aws" {
2+
name = "easyaws.in"
3+
4+
tags = {
5+
Environment = "dev"
6+
}
7+
}
8+
9+
resource "aws_route53_record" "www" {
10+
zone_id = aws_route53_zone.easy_aws.zone_id
11+
name = "www.easyaws.in"
12+
type = "A"
13+
ttl = "300"
14+
records = [aws_lb.elb_example.dns_name]
15+
}
16+
17+
output "name_server"{
18+
value=aws_route53_zone.easy_aws.name_servers
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
resource "aws_security_group" "elb_sg" {
2+
name = "allow_SSH"
3+
description = "Allow SSH inbound traffic"
4+
vpc_id = aws_vpc.vpc_demo.id
5+
6+
ingress {
7+
# SSH Port 22 allowed from any IP
8+
from_port = 22
9+
to_port = 22
10+
protocol = "tcp"
11+
cidr_blocks = ["0.0.0.0/0"]
12+
}
13+
14+
ingress {
15+
# SSH Port 22 allowed from any IP
16+
from_port = 80
17+
to_port = 80
18+
protocol = "tcp"
19+
cidr_blocks = ["0.0.0.0/0"]
20+
}
21+
22+
egress {
23+
from_port = 0
24+
to_port = 0
25+
protocol = "-1"
26+
cidr_blocks = ["0.0.0.0/0"]
27+
}
28+
}

terraform-aws-elb-alb/variables.tf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
variable "region" {
2+
type = "string"
3+
default = "us-east-1"
4+
}
5+
variable "ami_id" {
6+
type = "map"
7+
default = {
8+
us-east-1 = "ami-035b3c7efe6d061d5"
9+
us-east-2 = "ami-02ccb28830b645a41"
10+
eu-central-1 = "ami-9787h5h6nsn75gd33"
11+
}
12+
}
13+
variable "instance_type" {
14+
type = "string"
15+
default = "t2.micro"
16+
}
17+
variable "key_name" {
18+
type = "string"
19+
default = "ec2-demo"
20+
}
21+
22+
variable "cidr" {
23+
description = "The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden"
24+
type = string
25+
default = "10.0.0.0/16"
26+
}
27+
variable "instance_tenancy" {
28+
description = "A tenancy option for instances launched into the VPC"
29+
type = string
30+
default = "default"
31+
}
32+
33+
variable "enable_dns_hostnames" {
34+
description = "Should be true to enable DNS hostnames in the VPC"
35+
type = bool
36+
default = true
37+
}
38+
39+
variable "enable_dns_support" {
40+
description = "Should be true to enable DNS support in the VPC"
41+
type = bool
42+
default = true
43+
}
44+
45+
variable "enable_classiclink" {
46+
description = "Should be true to enable ClassicLink for the VPC. Only valid in regions and accounts that support EC2 Classic."
47+
type = bool
48+
default = false
49+
}
50+
51+
variable "tags" {
52+
description = "A map of tags to add to all resources"
53+
type = string
54+
default = "Vpc-custom-demo"
55+
}

terraform-aws-elb-alb/vpc.tf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
resource "aws_vpc" "vpc_demo" {
2+
cidr_block = var.cidr
3+
instance_tenancy = var.instance_tenancy
4+
enable_dns_hostnames = var.enable_dns_hostnames
5+
enable_dns_support = var.enable_dns_support
6+
enable_classiclink = var.enable_classiclink
7+
8+
tags = {
9+
Name = var.tags
10+
}
11+
}
12+
13+
resource "aws_internet_gateway" "gw" {
14+
vpc_id = aws_vpc.vpc_demo.id
15+
16+
tags = {
17+
Name = "internet-gateway-demo"
18+
}
19+
}
20+
21+
resource "aws_subnet" "public_1" {
22+
availability_zone = "us-east-1a"
23+
vpc_id = aws_vpc.vpc_demo.id
24+
map_public_ip_on_launch = true
25+
cidr_block = "10.0.1.0/24"
26+
27+
tags = {
28+
Name = "public_1-demo"
29+
}
30+
}
31+
32+
resource "aws_subnet" "public_2" {
33+
availability_zone = "us-east-1b"
34+
vpc_id = aws_vpc.vpc_demo.id
35+
map_public_ip_on_launch = true
36+
cidr_block = "10.0.2.0/24"
37+
38+
tags = {
39+
Name = "public_1-demo"
40+
}
41+
}
42+
43+
resource "aws_route_table" "route-public" {
44+
vpc_id = aws_vpc.vpc_demo.id
45+
46+
route {
47+
cidr_block = "10.0.0.0/0"
48+
gateway_id = aws_internet_gateway.gw.id
49+
}
50+
51+
tags = {
52+
Name = "public-route-table-demo"
53+
}
54+
}
55+
56+
resource "aws_route_table_association" "public_1" {
57+
subnet_id = aws_subnet.public_1.id
58+
route_table_id = aws_route_table.route-public.id
59+
}
60+
61+
resource "aws_route_table_association" "public_2" {
62+
subnet_id = aws_subnet.public_2.id
63+
route_table_id = aws_route_table.route-public.id
64+
}

0 commit comments

Comments
 (0)