Created
September 22, 2025 13:14
-
-
Save imShakil/9ac45bc841d4b1df33a486c619204cd1 to your computer and use it in GitHub Desktop.
high availability application AWS CF template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AWSTemplateFormatVersion: '2010-09-09' | |
| Description: 'Static Website High Availability with Autoscaling' | |
| Parameters: | |
| KeyName: | |
| Type: AWS::EC2::KeyPair::KeyName | |
| Description: The name of an existing EC2 KeyPair to enable SSH access to the instances | |
| Resources: | |
| VPC: | |
| Type: AWS::EC2::VPC | |
| Properties: | |
| CidrBlock: 10.10.0.0/16 | |
| EnableDnsSupport: true | |
| EnableDnsHostnames: true | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-VPC | |
| InternetGateway: | |
| Type: AWS::EC2::InternetGateway | |
| Properties: | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-igw | |
| GatewayAttachment: | |
| Type: AWS::EC2::VPCGatewayAttachment | |
| Properties: | |
| InternetGatewayId: !Ref InternetGateway | |
| VpcId: !Ref VPC | |
| PublicSubnet1: | |
| Type: AWS::EC2::Subnet | |
| Properties: | |
| VpcId: !Ref VPC | |
| CidrBlock: 10.10.1.0/24 | |
| AvailabilityZone: !Select [0, !GetAZs ''] | |
| MapPublicIpOnLaunch: true | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-pub-sub-AZ1 | |
| PublicSubnet2: | |
| Type: AWS::EC2::Subnet | |
| Properties: | |
| VpcId: !Ref VPC | |
| CidrBlock: 10.10.2.0/24 | |
| AvailabilityZone: !Select [1, !GetAZs ''] | |
| MapPublicIpOnLaunch: true | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-pub-sub-AZ2 | |
| PrivateSubnet1: | |
| Type: AWS::EC2::Subnet | |
| Properties: | |
| VpcId: !Ref VPC | |
| CidrBlock: 10.10.3.0/24 | |
| AvailabilityZone: !Select [0, !GetAZs ''] | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-pri-sub-AZ1 | |
| PrivateSubnet2: | |
| Type: AWS::EC2::Subnet | |
| Properties: | |
| VpcId: !Ref VPC | |
| CidrBlock: 10.10.4.0/24 | |
| AvailabilityZone: !Select [1, !GetAZs ''] | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-pri-sub-AZ2 | |
| PublicRouteTable: | |
| Type: AWS::EC2::RouteTable | |
| Properties: | |
| VpcId: !Ref VPC | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-pubRT | |
| PrivateRouteTable: | |
| Type: AWS::EC2::RouteTable | |
| Properties: | |
| VpcId: !Ref VPC | |
| Tags: | |
| - Key: Name | |
| Value: !Sub ${AWS::StackName}-priRT | |
| PublicRoute: | |
| Type: AWS::EC2::Route | |
| Properties: | |
| RouteTableId: !Ref PublicRouteTable | |
| DestinationCidrBlock: 0.0.0.0/0 | |
| GatewayId: !Ref InternetGateway | |
| PublicSubnet1RouteTableAssociation: | |
| Type: AWS::EC2::SubnetRouteTableAssociation | |
| Properties: | |
| RouteTableId: !Ref PublicRouteTable | |
| SubnetId: !Ref PublicSubnet1 | |
| PublicSubnet2RouteTableAssociation: | |
| Type: AWS::EC2::SubnetRouteTableAssociation | |
| Properties: | |
| RouteTableId: !Ref PublicRouteTable | |
| SubnetId: !Ref PublicSubnet2 | |
| PrivateSubnet1RouteTableAssociation: | |
| Type: AWS::EC2::SubnetRouteTableAssociation | |
| Properties: | |
| RouteTableId: !Ref PrivateRouteTable | |
| SubnetId: !Ref PrivateSubnet1 | |
| PrivateSubnet2RouteTableAssociation: | |
| Type: AWS::EC2::SubnetRouteTableAssociation | |
| Properties: | |
| RouteTableId: !Ref PrivateRouteTable | |
| SubnetId: !Ref PrivateSubnet2 | |
| ALBSecurityGroup: | |
| Type: AWS::EC2::SecurityGroup | |
| Properties: | |
| GroupDescription: Security group for Application Load Balancer | |
| VpcId: !Ref VPC | |
| SecurityGroupIngress: | |
| - IpProtocol: tcp | |
| FromPort: 80 | |
| ToPort: 80 | |
| CidrIp: 0.0.0.0/0 | |
| WebServerSecurityGroup: | |
| Type: AWS::EC2::SecurityGroup | |
| Properties: | |
| GroupDescription: Security group for web server instances | |
| VpcId: !Ref VPC | |
| SecurityGroupIngress: | |
| - IpProtocol: tcp | |
| FromPort: 80 | |
| ToPort: 80 | |
| SourceSecurityGroupId: !Ref ALBSecurityGroup | |
| - IpProtocol: tcp | |
| FromPort: 22 | |
| ToPort: 22 | |
| CidrIp: 0.0.0.0/0 | |
| ApplicationLoadBalancer: | |
| Type: AWS::ElasticLoadBalancingV2::LoadBalancer | |
| Properties: | |
| Type: application | |
| Subnets: | |
| - !Ref PublicSubnet1 | |
| - !Ref PublicSubnet2 | |
| SecurityGroups: | |
| - !Ref ALBSecurityGroup | |
| ALBTargetGroup: | |
| Type: AWS::ElasticLoadBalancingV2::TargetGroup | |
| Properties: | |
| Port: 80 | |
| Protocol: HTTP | |
| VpcId: !Ref VPC | |
| HealthCheckPath: / | |
| HealthCheckProtocol: HTTP | |
| ALBListener: | |
| Type: AWS::ElasticLoadBalancingV2::Listener | |
| Properties: | |
| DefaultActions: | |
| - Type: forward | |
| TargetGroupArn: !Ref ALBTargetGroup | |
| LoadBalancerArn: !Ref ApplicationLoadBalancer | |
| Port: 80 | |
| Protocol: HTTP | |
| LaunchTemplate: | |
| Type: AWS::EC2::LaunchTemplate | |
| Properties: | |
| LaunchTemplateData: | |
| ImageId: ami-05fd46f12b86c4a6c | |
| InstanceType: t2.micro | |
| KeyName: !Ref KeyName | |
| SecurityGroupIds: | |
| - !Ref WebServerSecurityGroup | |
| UserData: | |
| Fn::Base64: | | |
| #!/bin/bash | |
| yum update -y | |
| yum install -y python3 python3-pip | |
| pip3 install requests | |
| pip3 install flask | |
| wget https://gist.githubusercontent.com/imShakil/97f0b1156129bacd7d6b513dbc593a9f/raw/994aeabdb734b80e9b64f9cdff107f704b3e54ba/app.py -O /home/ec2-user/app.py | |
| nohup python3 /home/ec2-user/app.py > /home/ec2-user/app.log 2>&1 & | |
| AutoScalingGroup: | |
| Type: AWS::AutoScaling::AutoScalingGroup | |
| Properties: | |
| LaunchTemplate: | |
| LaunchTemplateId: !Ref LaunchTemplate | |
| Version: !GetAtt LaunchTemplate.LatestVersionNumber | |
| MinSize: 1 | |
| MaxSize: 3 | |
| DesiredCapacity: 2 | |
| VPCZoneIdentifier: | |
| - !Ref PublicSubnet1 | |
| - !Ref PublicSubnet2 | |
| TargetGroupARNs: | |
| - !Ref ALBTargetGroup | |
| HealthCheckType: ELB | |
| HealthCheckGracePeriod: 300 | |
| Outputs: | |
| VpcId: | |
| Value: !Ref VPC | |
| Export: | |
| Name: !Sub ${AWS::StackName}-VpcId | |
| WebsiteURL: | |
| Description: Static Website URL | |
| Value: !Sub http://${ApplicationLoadBalancer.DNSName} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment