Skip to content

Commit 783705b

Browse files
authored
Merge pull request #18 from Aswin-Vijayan/master
[CD-5][Changes] - Made required changes in alb, asg and iam policy
2 parents 83a4653 + 2d344bf commit 783705b

File tree

12 files changed

+119
-71
lines changed

12 files changed

+119
-71
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,56 @@ terraform destroy \
2222
-backend-config="dynamodb_table=terraform-state-lock"
2323
```
2424

25-
## RDS Provisioning
25+
#### RDS Provisioning
2626

2727
cd into the `environments/dev/rds` directory and run the following commands:
2828

29+
1. Init Terraform in the directory `environments/dev/rds`
30+
2931
```
3032
terraform init
33+
```
34+
2. To preview the changes in code
3135

36+
```
3237
terraform plan -var-file=../../../vars/dev/rds.tfvars
38+
```
39+
3. To apply the changes
3340

41+
```
3442
terraform apply -var-file=../../../vars/dev/rds.tfvars
43+
```
44+
4. To destroy the resources created using the code
3545

46+
```
3647
terraform destroy -var-file=../../../vars/dev/rds.tfvars
3748
```
3849

50+
### ALB and ASG Provisioning
51+
52+
cd into the `environments/dev/alb-asg` directory and run the following commands:
53+
54+
1. Init Terraform in the directory `environments/dev/alb-asg`
55+
56+
```
57+
terraform init
58+
```
59+
2. To preview the changes in code
60+
61+
```
62+
terraform plan -var-file=../../../vars/dev/alb-asg.tfvars
63+
```
64+
3. To apply the changes
65+
66+
```
67+
terraform apply -var-file=../../../vars/dev/alb-asg.tfvars
68+
```
69+
4. To destroy the resources created using the code
70+
71+
```
72+
terraform destroy -var-file=../../../vars/dev/alb-asg.tfvars
73+
```
74+
3975
## EC2 Instance Provisioning
4076

4177
1. Navigate to the `environment/dev` folder:

environments/dev/alb-asg/main.tf

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ provider "aws" {
44

55
module "iam-policy" {
66
source = "../../../modules/iam-policy"
7-
instance_role = var.instance_role
7+
owner = var.owner
8+
environment = var.environment
9+
cost_center = var.cost_center
10+
application = var.application
811
}
912

1013
module "alb-sg" {
@@ -14,7 +17,7 @@ module "alb-sg" {
1417
environment = var.environment
1518
owner = var.owner
1619
cost_center = var.cost_center
17-
application = var.application
20+
application ="${var.application}-alb"
1821
vpc_id = var.vpc_id
1922
ingress_from_port = var.alb_ingress_from_port
2023
ingress_to_port = var.alb_ingress_to_port
@@ -32,7 +35,7 @@ module "alb" {
3235
internal = var.internal
3336
loadbalancer_type = var.loadbalancer_type
3437
vpc_id = var.vpc_id
35-
subnets = var.subnets
38+
alb_subnets = var.alb_subnets
3639
target_group_port = var.target_group_port
3740
target_group_protocol = var.target_group_protocol
3841
target_type = var.target_type
@@ -75,13 +78,11 @@ module "instance-sg" {
7578

7679
module "asg" {
7780
source = "../../../modules/asg"
78-
instance_profile = var.instance_profile
79-
instance_role = var.instance_role
8081
ami_id = var.ami_id
8182
instance_type = var.instance_type
8283
key_name = var.key_name
8384
vpc_id = var.vpc_id
84-
subnets = var.subnets
85+
asg_subnets = var.asg_subnets
8586
public_access = var.public_access
8687
user_data = var.user_data
8788
max_size = var.max_size
@@ -92,7 +93,7 @@ module "asg" {
9293
environment = var.environment
9394
cost_center = var.cost_center
9495
application = var.application
95-
alb_target_group_arn = module.alb.lb_target_group_arn
96+
alb_target_group_arn = module.alb.alb_target_group_arn
9697
iam_role = module.iam-policy.iam_role
9798
security_group_ids = module.instance-sg.security_group_ids
9899
tags = {

environments/dev/alb-asg/variables.tf

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ variable "region" {
99
description = "Region of the alb-asg"
1010
}
1111

12-
variable "instance_profile" {
13-
description = "Instance profile for the instance which the instance role is associated with"
14-
type = string
15-
}
16-
17-
variable "instance_role" {
18-
description = "Instance role for the instance"
19-
type = string
20-
}
21-
2212
variable "internal" {
2313
description = "Whether the load balancer is internal or not"
2414
type = bool
@@ -29,6 +19,11 @@ variable "loadbalancer_type" {
2919
type = string
3020
}
3121

22+
variable "alb_subnets" {
23+
description = "A list of subnet IDs to use for the resources."
24+
type = list(string)
25+
}
26+
3227
variable "target_group_port" {
3328
description = "Target group port"
3429
type = number
@@ -120,7 +115,7 @@ variable "vpc_id" {
120115
description = "The ID of the VPC to use for the resources."
121116
}
122117

123-
variable "subnets" {
118+
variable "asg_subnets" {
124119
description = "A list of subnet IDs to use for the resources."
125120
type = list(string)
126121
}
@@ -221,9 +216,6 @@ variable "egress_protocol" {
221216
type = list(any)
222217
}
223218

224-
#####
225-
226-
227219
variable "alb_ingress_cidr_block" {
228220
type = list(string)
229221
description = "CIDR blocks for EC2 security group ingress rules"
File renamed without changes.

modules/alb/main.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
resource "aws_lb" "application_load_balancer" {
2-
name = "${var.environment}-${var.application}-lb"
1+
resource "aws_alb" "application_load_balancer" {
2+
name = "${var.environment}-${var.application}-alb"
33
internal = var.internal
44
load_balancer_type = var.loadbalancer_type
55

6-
subnets = var.subnets
6+
subnets = var.alb_subnets
77
security_groups = var.security_group_ids
88

99
tags = merge(
1010
{
11-
Name = "${var.environment}-${var.application}-lb",
11+
Name = "${var.environment}-${var.application}-alb",
1212
Environment = var.environment,
1313
Owner = var.owner,
1414
CostCenter = var.cost_center,
@@ -18,7 +18,7 @@ resource "aws_lb" "application_load_balancer" {
1818
)
1919
}
2020

21-
resource "aws_lb_target_group" "alb_tg" {
21+
resource "aws_alb_target_group" "alb_tg" {
2222
name_prefix = "alb-tg"
2323
port = var.target_group_port
2424
protocol = var.target_group_protocol
@@ -39,7 +39,7 @@ resource "aws_lb_target_group" "alb_tg" {
3939

4040
tags = merge(
4141
{
42-
Name = "${var.environment}-${var.application}-lb-target-group"
42+
Name = "${var.environment}-${var.application}-alb-target-group"
4343
Environment = var.environment,
4444
Owner = var.owner,
4545
CostCenter = var.cost_center,
@@ -49,13 +49,13 @@ resource "aws_lb_target_group" "alb_tg" {
4949
)
5050
}
5151

52-
resource "aws_lb_listener" "application_listener" {
53-
load_balancer_arn = aws_lb.application_load_balancer.arn
52+
resource "aws_alb_listener" "application_listener" {
53+
load_balancer_arn = aws_alb.application_load_balancer.arn
5454
port = var.listener_port
5555
protocol = var.listener_protocol
5656

5757
default_action {
58-
target_group_arn = aws_lb_target_group.alb_tg.arn
58+
target_group_arn = aws_alb_target_group.alb_tg.arn
5959
type = var.listener_type
6060
}
6161
}

modules/alb/outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "load_balancer_dns_name" {
22
description = "LoadBalancer dns name"
3-
value = aws_lb.application_load_balancer.dns_name
3+
value = aws_alb.application_load_balancer.dns_name
44
}
55

6-
output "lb_target_group_arn" {
7-
description = "LB Target Grouparn"
8-
value = aws_lb_target_group.alb_tg.arn
6+
output "alb_target_group_arn" {
7+
description = "ALB Target Grouparn"
8+
value = aws_alb_target_group.alb_tg.arn
99
}

modules/alb/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ variable "vpc_id" {
2929
description = "The ID of the VPC to use for the resources."
3030
}
3131

32-
variable "subnets" {
32+
variable "alb_subnets" {
3333
description = "A list of subnet IDs to use for the resources."
3434
type = list(string)
3535
}

modules/asg/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ locals {
66
}
77

88
resource "aws_iam_instance_profile" "instance_profile" {
9-
name = var.instance_profile
9+
name = "${var.environment}-${var.application}-instance_profile"
1010

1111
role = var.iam_role
1212
}
@@ -18,7 +18,7 @@ resource "aws_launch_template" "application_lt" {
1818
key_name = var.key_name
1919

2020
iam_instance_profile {
21-
name = var.instance_profile
21+
name = "${var.environment}-${var.application}-instance_profile"
2222
}
2323

2424
network_interfaces {
@@ -35,7 +35,7 @@ resource "aws_autoscaling_group" "application_asg" {
3535
max_size = var.max_size
3636
min_size = var.min_size
3737
desired_capacity = var.desired_capacity
38-
vpc_zone_identifier = var.subnets
38+
vpc_zone_identifier = var.asg_subnets
3939

4040
launch_template {
4141
id = aws_launch_template.application_lt.id

modules/asg/variables.tf

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ variable "tags" {
44
description = "Extra tags to attach to the alb-asg resources"
55
}
66

7-
variable "instance_profile" {
8-
description = "Instance profile for the instance which the instance role is associated with"
9-
type = string
10-
}
11-
12-
variable "instance_role" {
13-
description = "Instance role for the instance"
7+
variable "iam_role" {
8+
description = "IAM role for the instance"
149
type = string
1510
}
1611

@@ -34,7 +29,7 @@ variable "vpc_id" {
3429
description = "The ID of the VPC to use for the resources."
3530
}
3631

37-
variable "subnets" {
32+
variable "asg_subnets" {
3833
description = "A list of subnet IDs to use for the resources."
3934
type = list(string)
4035
}
@@ -98,8 +93,3 @@ variable "alb_target_group_arn" {
9893
description = "load balancer target group arn"
9994
type = string
10095
}
101-
102-
variable "iam_role" {
103-
description = "iam role name"
104-
type = string
105-
}

modules/iam-policy/main.tf

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_iam_role" "iam_role" {
2-
name = var.instance_role
2+
name = "${var.environment}-${var.application}-iam-role"
33
assume_role_policy = jsonencode({
44
Version = "2012-10-17"
55
Statement = [
@@ -12,11 +12,21 @@ resource "aws_iam_role" "iam_role" {
1212
}
1313
]
1414
})
15+
tags = merge(
16+
{
17+
Name = "${var.environment}-${var.application}-iam-role",
18+
Environment = var.environment,
19+
Owner = var.owner,
20+
CostCenter = var.cost_center,
21+
Application = var.application
22+
},
23+
var.tags
24+
)
1525
}
1626

1727
resource "aws_iam_policy" "iam_policy" {
18-
name = "iam_policy"
19-
policy = file("${path.module}../../../environments/dev/file/lb-asg.json")
28+
name = "${var.environment}-${var.application}-iam-policy"
29+
policy = file("${path.module}../../../environments/dev/iam-policies/alb-asg.json")
2030
}
2131

2232
resource "aws_iam_role_policy_attachment" "iam_role_policy_attachment" {

0 commit comments

Comments
 (0)