my first project AWSTemplateFormatVersion: '2010-09-09' Description: 'Create an Project'
Parameters: InstanceTypeParameter: Type: String Default: t2.micro ImageId: Type: String Default: ami-0a242269c4b530c5e AvailabilityZone: Type: String Default: eu-west-2b KeyName: Type: String Default: project-1
Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: London-VPC1
PublicSubnet1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 10.0.0.0/24 AvailabilityZone: eu-west-2a MapPublicIpOnLaunch: true Tags: - Key: Name Value: Public-Subnet-1
PublicSubnet2: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 10.0.1.0/24 AvailabilityZone: eu-west-2b MapPublicIpOnLaunch: true Tags: - Key: Name Value: Public-Subnet-2
InternetGateway: Type: AWS::EC2::InternetGateway
GatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref MyVPC InternetGatewayId: !Ref InternetGateway
PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref MyVPC Tags: - Key: Name Value: Public-Route-Table
PublicRoute: Type: AWS::EC2::Route DependsOn: GatewayAttachment Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway
Subnet1RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet1 RouteTableId: !Ref PublicRouteTable
Subnet2RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet2 RouteTableId: !Ref PublicRouteTable
DefaultSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: VpcId: !Ref MyVPC GroupName: SecurityGroup-EC22 GroupDescription: sg for ec2 SecurityGroupIngress: - IpProtocol: tcp FromPort: 80 ToPort: 80 CidrIp: 0.0.0.0/0
EC2Instance: Type: "AWS::EC2::Instance" Properties: InstanceType: !Ref InstanceTypeParameter ImageId: !Ref ImageId AvailabilityZone: !Ref AvailabilityZone KeyName: !Ref KeyName SubnetId: !Ref PublicSubnet2 SecurityGroupIds: - !Ref DefaultSecurityGroup Tags: - Key: "Name" Value: "Hemanth" UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd wget https://raw.githubusercontent.com/HemanthYadavG7/project/main/index.html cp index.html /var/www/html/index.html
MyLaunchConfig: Type: AWS::AutoScaling::LaunchConfiguration Properties: ImageId: !Ref ImageId InstanceType: !Ref InstanceTypeParameter KeyName: !Ref KeyName SecurityGroups: - !Ref DefaultSecurityGroup UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd wget https://raw.githubusercontent.com/HemanthYadavG7/project/main/index.html cp index.html /var/www/html/index.html MyAutoScalingGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: LaunchConfigurationName: !Ref MyLaunchConfig MinSize: 0 MaxSize: 1 DesiredCapacity: 0 VPCZoneIdentifier: - !Ref PublicSubnet1 - !Ref PublicSubnet2 TargetGroupARNs: - !Ref MyTargetGroup
MyTargetGroup: Type: AWS::ElasticLoadBalancingV2::TargetGroup Properties: Name: MyTargetGroup Port: 80 Protocol: HTTP VpcId: !Ref MyVPC TargetType: instance Targets: - Id: !Ref EC2Instance Port: 80
MyLoadBalancer: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Name: MyLoadBalancer2 Subnets: - !Ref PublicSubnet1 - !Ref PublicSubnet2 SecurityGroups: - !Ref DefaultSecurityGroup Type: application LoadBalancerAttributes: - Key: deletion_protection.enabled Value: true Tags: - Key: Name Value: MyLoadBalancer
MyListener: Type: AWS::ElasticLoadBalancingV2::Listener Properties: LoadBalancerArn: !Ref MyLoadBalancer Port: 80 Protocol: HTTP DefaultActions: - Type: forward TargetGroupArn: !Ref MyTargetGroup
Outputs: InstanceId: Description: InstanceId of the newly created EC2 instance Value: !Ref 'EC2Instance'