Skip to content

Commit 982dde3

Browse files
author
Vibhas
committed
First commit
0 parents  commit 982dde3

File tree

7 files changed

+534
-0
lines changed

7 files changed

+534
-0
lines changed

index.yaml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
Parameters:
2+
EnviromentName:
3+
Description: An environment name that will be prefixed to resource names
4+
Type: String
5+
AllowedValues: [poc, dev, uat]
6+
Default: poc
7+
VPC:
8+
Description: Choose which VPC the security groups should be deployed to
9+
Type: AWS::EC2::VPC::Id
10+
Subnets:
11+
Description: Subnets for LoadBalancer
12+
Type: List<AWS::EC2::Subnet::Id>
13+
Ec2InstanceType:
14+
Description: Instance Type of ecs ec2 cluster
15+
Type: String
16+
AllowedValues: [ t2.micro, t2.small, t2.medium, t2.large, t2.xlarge, t2.2xlarge,
17+
m5.large, m5.xlarge, m5.2large, m5.4xlarge, m5.12xlarge, m5.24large,
18+
c5.large, c5.xlarge, c5.2xlarge, c5.4xlarge, c5.9xlarge, c5.18xlarge,
19+
r5.large, r5.xlarge, r5.2xlarge, r5.4xlarge, r5.12xlarge, r5.24xlarge ]
20+
UserServicePath:
21+
Description: The path to register with the Application Load Balancer
22+
Type: String
23+
Default: /users
24+
DeptServicePath:
25+
Description: The path to register with the Application Load Balancer
26+
Type: String
27+
Default: /dept
28+
29+
30+
Resources:
31+
SecurityGroupsStack:
32+
Type: AWS::CloudFormation::Stack
33+
Properties:
34+
TemplateURL: https://aws-practice-001.s3.amazonaws.com/securitygroups.yaml
35+
Parameters:
36+
EnviromentName: !Ref EnviromentName
37+
VPC: !Ref VPC
38+
Stackname: !Sub ${AWS::StackName}
39+
TimeoutInMinutes: 5
40+
41+
42+
LoadBalancerStack:
43+
Type: AWS::CloudFormation::Stack
44+
Properties:
45+
TemplateURL: https://aws-practice-001.s3.amazonaws.com/loadbalancer.yaml
46+
Parameters:
47+
EnviromentName: !Ref EnviromentName
48+
Stackname: !Sub ${AWS::StackName}
49+
LoadBalancerSecurityGroup: !GetAtt SecurityGroupsStack.Outputs.LoadBalancerSecurityGroup
50+
LoadBalancerSubnets: !Join [",",!Ref Subnets]
51+
VPC: !Ref VPC
52+
TimeoutInMinutes: 5
53+
54+
55+
EcsClusterStack:
56+
Type: AWS::CloudFormation::Stack
57+
Properties:
58+
TemplateURL: https://aws-practice-001.s3.amazonaws.com/ecs-cluster.yaml
59+
Parameters:
60+
EnviromentName: !Ref EnviromentName
61+
Stackname: !Sub ${AWS::StackName}
62+
LoadBalancerSubnets: !Join [",",!Ref Subnets]
63+
EC2ClusterSecurityGroup: !GetAtt SecurityGroupsStack.Outputs.EC2ClusterSecurityGroup
64+
SshSecurityGroup: !GetAtt SecurityGroupsStack.Outputs.SshSecurityGroup
65+
Ec2InstanceType: !Ref Ec2InstanceType
66+
TimeoutInMinutes: 5
67+
68+
LogGroupStack:
69+
Type: AWS::CloudFormation::Stack
70+
Properties:
71+
TemplateURL: https://aws-practice-001.s3.amazonaws.com/logs.yaml
72+
Parameters:
73+
EnviromentName: !Ref EnviromentName
74+
Stackname: !Sub ${AWS::StackName}
75+
TimeoutInMinutes: 5
76+
77+
UserService:
78+
Type: AWS::CloudFormation::Stack
79+
Properties:
80+
TemplateURL: https://aws-practice-001.s3.amazonaws.com/users-service.yaml
81+
Parameters:
82+
EnviromentName: !Ref EnviromentName
83+
Stackname: !Sub ${AWS::StackName}
84+
VPC: !Ref VPC
85+
Cluster: !GetAtt EcsClusterStack.Outputs.Cluster
86+
Path: !Ref UserServicePath
87+
LoadBalancerListener: !GetAtt LoadBalancerStack.Outputs.LoadBalancerListener
88+
LogGroupName: !GetAtt LogGroupStack.Outputs.LogGroupName
89+
TimeoutInMinutes: 5
90+
91+
DeptService:
92+
Type: AWS::CloudFormation::Stack
93+
Properties:
94+
TemplateURL: https://aws-practice-001.s3.amazonaws.com/dept-service.yaml
95+
Parameters:
96+
EnviromentName: !Ref EnviromentName
97+
Stackname: !Sub ${AWS::StackName}
98+
VPC: !Ref VPC
99+
Cluster: !GetAtt EcsClusterStack.Outputs.Cluster
100+
Path: !Ref DeptServicePath
101+
LoadBalancerListener: !GetAtt LoadBalancerStack.Outputs.LoadBalancerListener
102+
LogGroupName: !GetAtt LogGroupStack.Outputs.LogGroupName
103+
TimeoutInMinutes: 5
104+
105+
106+
107+
108+
109+

infrastructure/ecs-cluster.yaml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Parameters:
2+
LoadBalancerSubnets:
3+
Type: String
4+
Description: Subnets for ASG
5+
EnviromentName:
6+
Description: An environment name that will be prefixed to resource names
7+
Type: String
8+
Stackname:
9+
Description: Stack name to be appended to nested security group stack
10+
Type: String
11+
EC2ClusterSecurityGroup:
12+
Description: Ecs Sg for ASGLC
13+
Type: AWS::EC2::SecurityGroup::Id
14+
SshSecurityGroup:
15+
Description: SSH Sg for ASGLC
16+
Type: AWS::EC2::SecurityGroup::Id
17+
Ec2InstanceType:
18+
Description: Instance Type of ecs ec2 cluster
19+
Type: String
20+
AllowedValues: [ t2.micro, t2.small, t2.medium, t2.large, t2.xlarge, t2.2xlarge,
21+
m5.large, m5.xlarge, m5.2large, m5.4xlarge, m5.12xlarge, m5.24large,
22+
c5.large, c5.xlarge, c5.2xlarge, c5.4xlarge, c5.9xlarge, c5.18xlarge,
23+
r5.large, r5.xlarge, r5.2xlarge, r5.4xlarge, r5.12xlarge, r5.24xlarge ]
24+
25+
26+
27+
28+
Resources:
29+
Cluster:
30+
Type: AWS::ECS::Cluster
31+
Properties:
32+
ClusterName: !Join ['-',[!Ref Stackname,'cluster',!Ref EnviromentName]]
33+
34+
AutoScalingGroup:
35+
Type: AWS::AutoScaling::AutoScalingGroup
36+
Properties:
37+
AutoScalingGroupName: !Join ['-',[!Ref Stackname,'asg',!Ref EnviromentName]]
38+
VPCZoneIdentifier: !Split [",",!Ref LoadBalancerSubnets]
39+
LaunchConfigurationName: !Ref AutoScalingGroupLaunchConfiguration
40+
MaxSize: 2
41+
MinSize: 1
42+
DesiredCapacity: 1
43+
Tags:
44+
- Key: Name
45+
Value: !Join ['-',[!Ref Stackname,'asg',!Ref EnviromentName]]
46+
PropagateAtLaunch: true
47+
48+
49+
# Note Can create role similar to ecsInstanceRole
50+
51+
AutoScalingGroupLaunchConfiguration:
52+
Type: AWS::AutoScaling::LaunchConfiguration
53+
Properties:
54+
KeyName: ecs-poc
55+
ImageId: ami-07a63940735aebd38
56+
InstanceType: !Ref Ec2InstanceType
57+
IamInstanceProfile: ecsInstanceRole
58+
SecurityGroups:
59+
- !Ref EC2ClusterSecurityGroup
60+
- !Ref SshSecurityGroup
61+
UserData:
62+
Fn::Base64:
63+
Fn::Sub:
64+
- |
65+
#!/bin/bash -xe
66+
echo ECS_CLUSTER=${ECSCluster} >> /etc/ecs/ecs.config
67+
yum install -y aws-cfn-bootstrap
68+
- { ECSCluster: !Ref Cluster }
69+
70+
Outputs:
71+
Cluster:
72+
Description: Name of cluster
73+
Value: !Ref Cluster
74+
ECSAutoScalingGroupName:
75+
Description: A reference to ECS AutoScaling Group Name
76+
Value: !Ref AutoScalingGroup
77+
78+
79+

infrastructure/loadbalancer.yaml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Parameters:
2+
EnviromentName:
3+
Description: An environment name that will be prefixed to resource names
4+
Type: String
5+
Stackname:
6+
Description: Stack name to be appended to nested security group stack
7+
Type: String
8+
LoadBalancerSubnets:
9+
Type: String
10+
Description: Subnets for LoadBalancer
11+
LoadBalancerSecurityGroup:
12+
Description: Security group for internet facing alb
13+
Type: AWS::EC2::SecurityGroup::Id
14+
VPC:
15+
Description: Choose which VPC the security groups should be deployed to
16+
Type: AWS::EC2::VPC::Id
17+
18+
Resources:
19+
LoadBalancer:
20+
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
21+
Properties:
22+
Name: !Join ['-',[!Ref Stackname,'alb',!Ref EnviromentName]]
23+
Subnets: !Split [",",!Ref LoadBalancerSubnets]
24+
SecurityGroups:
25+
- !Ref LoadBalancerSecurityGroup
26+
27+
28+
LoadBalancerListener:
29+
Type: AWS::ElasticLoadBalancingV2::Listener
30+
Properties:
31+
LoadBalancerArn: !Ref LoadBalancer
32+
Port: 80
33+
Protocol: HTTP
34+
DefaultActions:
35+
- Type: fixed-response
36+
FixedResponseConfig:
37+
ContentType: text/plain
38+
MessageBody: No Route Detected
39+
StatusCode: 404
40+
41+
DefaultTargetGroup:
42+
Type: AWS::ElasticLoadBalancingV2::TargetGroup
43+
Properties:
44+
Name: !Join ['-',[!Ref Stackname,'default','tg',!Ref EnviromentName]]
45+
Protocol: HTTP
46+
VpcId: !Ref VPC
47+
Port: 80
48+
49+
50+
51+
Outputs:
52+
LoadBalancerDns:
53+
Description: DNS name of loadbalancer
54+
Value: !GetAtt LoadBalancer.DNSName
55+
LoadBalancerListener:
56+
Description: Listener of alb
57+
Value: !Ref LoadBalancerListener
58+
59+

infrastructure/logs.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Parameters:
2+
EnviromentName:
3+
Description: An environment name that will be prefixed to resource names
4+
Type: String
5+
Stackname:
6+
Description: Stack name to be appended to nested security group stack
7+
Type: String
8+
9+
Resources:
10+
CloudWatchLogGroup:
11+
Type: AWS::Logs::LogGroup
12+
Properties:
13+
LogGroupName: !Join ['-',[!Ref Stackname,!Ref EnviromentName]]
14+
RetentionInDays: 7
15+
16+
17+
Outputs:
18+
LogGroupName:
19+
Description: Cloudwatch log group
20+
Value: !Ref CloudWatchLogGroup

infrastructure/securitygroups.yaml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Parameters:
2+
EnviromentName:
3+
Description: An environment name that will be prefixed to resource names
4+
Type: String
5+
VPC:
6+
Description: Choose which VPC the security groups should be deployed to
7+
Type: AWS::EC2::VPC::Id
8+
Stackname:
9+
Description: Stack name to be appended to nested security group stack
10+
Type: String
11+
12+
13+
Resources:
14+
15+
SshSecurityGroup:
16+
Type: AWS::EC2::SecurityGroup
17+
Properties:
18+
GroupName: !Join ['-',[!Ref Stackname,'ssh']]
19+
VpcId: !Ref VPC
20+
GroupDescription: Enable SSH access via port 22
21+
SecurityGroupIngress:
22+
- IpProtocol: tcp
23+
FromPort: 22
24+
ToPort: 22
25+
CidrIp: 0.0.0.0/0
26+
Tags:
27+
- Key: Name
28+
Value: !Join ['-',[!Ref Stackname,'ssh',!Ref EnviromentName]]
29+
30+
31+
32+
HttpSecurityGroup:
33+
Type: AWS::EC2::SecurityGroup
34+
Properties:
35+
GroupName: !Join ['-',[!Ref Stackname,'http']]
36+
VpcId: !Ref VPC
37+
GroupDescription: Enable HTTP access via port 80
38+
SecurityGroupIngress:
39+
- IpProtocol: tcp
40+
FromPort: 80
41+
ToPort: 80
42+
CidrIp: 0.0.0.0/0
43+
Tags:
44+
- Key: Name
45+
Value: !Join ['-',[!Ref Stackname,'http',!Ref EnviromentName]]
46+
47+
48+
LoadBalancerSecurityGroup:
49+
Type: AWS::EC2::SecurityGroup
50+
Properties:
51+
GroupName: !Join ['-',[!Ref Stackname,'alb']]
52+
GroupDescription: Access to the public facing load balancer
53+
VpcId: !Ref VPC
54+
SecurityGroupIngress:
55+
- CidrIp: 0.0.0.0/0
56+
IpProtocol: -1
57+
Tags:
58+
- Key: Name
59+
Value: !Join ['-',[!Ref Stackname,'alb',!Ref EnviromentName]]
60+
61+
EC2ClusterSecurityGroup:
62+
Type: AWS::EC2::SecurityGroup
63+
Properties:
64+
GroupName: !Join ['-',[!Ref Stackname,'ecs']]
65+
GroupDescription: Access to the ecs cluster behind load balancer
66+
VpcId: !Ref VPC
67+
SecurityGroupIngress:
68+
- IpProtocol: tcp
69+
FromPort: 0
70+
ToPort: 65535
71+
SourceSecurityGroupId: !Ref LoadBalancerSecurityGroup
72+
Tags:
73+
- Key: Name
74+
Value: !Join ['-',[!Ref Stackname,'cluster',!Ref EnviromentName]]
75+
76+
77+
Outputs:
78+
SshSecurityGroup:
79+
Description: A reference to security group for SSH
80+
Value: !Ref SshSecurityGroup
81+
HttpSecurityGroup:
82+
Description: A reference to security group for HTTP
83+
Value: !Ref HttpSecurityGroup
84+
LoadBalancerSecurityGroup:
85+
Description: A reference to security group for public facing ALB
86+
Value: !Ref LoadBalancerSecurityGroup
87+
EC2ClusterSecurityGroup:
88+
Description: A reference to security group for ECS cluster from ALB
89+
Value: !Ref EC2ClusterSecurityGroup
90+
91+
92+
93+

0 commit comments

Comments
 (0)