{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Deploy the Procon8 application to Elastic Beanstalk",
  "Parameters": {
	  "BucketENV": {
            "Type": "String",
            "AllowedValues": [
                "dev",
                "int",
                "qa",
                "prod"
            ],
            "Default": "dev"
        },
    "MinSize": {
      "Type": "String",
      "Default": "1",
      "Description": "Minimum number of instances to be launched"
    },
    "MaxSize": {
      "Type": "String",
	  
      "Default": "6",
      "Description": "Maximum number of instances to be launched"
    },
    "WarKey": {
      "Type": "String",
      "Default": "test.zip",
	  "Description": "Bundle source code file in zip format"
    },
    "KeyName": {
      "Type": "AWS::EC2::KeyPair::KeyName",
      "Default": "dev-procon8",
	  "Description": "Keypair to be used by Instances"
    },
	 "AWSLinuxImage": {
      "Description": "The AMI of dispatcher.",
      "Type": "String",
	  "Default":"ami-d7b9a2b1"
    },
    "InstanceType": {
      "Type": "String",
	  "AllowedValues": [
         "t2.micro",
        "t2.small",
        "t2.medium",
        "t2.large",
        "t2.xlarge",
        "t2.2xlarge",
        "m3.medium",
        "m3.large",
        "m3.xlarge",
        "m3.2xlarge",
        "c3.large",
        "c3.xlarge",
        "c3.2xlarge",
        "c3.4xlarge",
        "c3.8xlarge",
        "t1.micro",
        "t2.nano",
        "m1.small",
        "m1.medium",
        "m1.large",
        "m1.xlarge",
        "c1.medium",
        "c1.xlarge",
        "c4.large",
        "c4.xlarge",
        "c4.2xlarge",
        "c4.4xlarge",
        "c4.8xlarge",
        "c5.large",
        "c5.xlarge",
        "c5.2xlarge",
        "c5.4xlarge",
        "c5.9xlarge",
        "c5.18xlarge",
        "c5n.large",
        "c5n.xlarge",
        "c5n.2xlarge",
        "c5n.4xlarge",
        "c5n.9xlarge",
        "c5n.18xlarge",
        "m2.xlarge",
        "m2.2xlarge",
        "m2.4xlarge",
        "r4.large",
        "r4.xlarge",
        "r4.2xlarge",
        "r4.4xlarge",
        "r4.8xlarge",
        "r4.16xlarge",
        "m4.large",
        "m4.xlarge",
        "m4.2xlarge",
        "m4.4xlarge",
        "m4.10xlarge",
        "m4.16xlarge",
        "cc2.8xlarge",
        "hi1.4xlarge",
        "hs1.8xlarge",
        "cr1.8xlarge",
        "g2.2xlarge",
        "g2.8xlarge",
        "p2.xlarge",
        "p2.8xlarge",
        "p2.16xlarge",
        "p3.2xlarge",
        "p3.8xlarge",
        "p3.16xlarge",
        "i2.xlarge",
        "i2.2xlarge",
        "i2.4xlarge",
        "i2.8xlarge",
        "i3.large",
        "i3.xlarge",
        "i3.2xlarge",
        "i3.4xlarge",
        "i3.8xlarge",
        "i3.16xlarge",
        "r3.large",
        "r3.xlarge",
        "r3.2xlarge",
        "r3.4xlarge",
        "r3.8xlarge",
        "d2.xlarge",
        "d2.2xlarge",
        "d2.4xlarge",
        "d2.8xlarge",
        "x1.16xlarge",
        "x1.32xlarge",
        "x1e.xlarge",
        "x1e.2xlarge",
        "x1e.4xlarge",
        "x1e.8xlarge",
        "x1e.16xlarge",
        "x1e.32xlarge",
        "f1.2xlarge",
        "f1.4xlarge",
        "f1.16xlarge",
        "g3.4xlarge",
        "g3.8xlarge",
        "g3.16xlarge",
        "h1.2xlarge",
        "h1.4xlarge",
        "h1.8xlarge",
        "h1.16xlarge",
        "m5.large",
        "m5.xlarge",
        "m5.2xlarge",
        "m5.4xlarge",
        "m5.12xlarge",
        "m5.24xlarge",
        "m5.metal",
        "c5d.large",
        "c5d.xlarge",
        "c5d.2xlarge",
        "c5d.4xlarge",
        "c5d.9xlarge",
        "c5d.18xlarge",
        "m5d.large",
        "m5d.xlarge",
        "m5d.2xlarge",
        "m5d.4xlarge",
        "m5d.12xlarge",
        "m5d.24xlarge",
        "m5d.metal",
        "z1d.large",
        "z1d.xlarge",
        "z1d.2xlarge",
        "z1d.3xlarge",
        "z1d.6xlarge",
        "z1d.12xlarge",
        "z1d.metal",
        "r5.large",
        "r5.xlarge",
        "r5.2xlarge",
        "r5.4xlarge",
        "r5.12xlarge",
        "r5.24xlarge",
        "r5.metal",
        "t3.nano",
        "t3.micro",
        "t3.small",
        "t3.medium",
        "t3.large",
        "t3.xlarge",
        "t3.2xlarge",
        "g3s.xlarge",
        "r5.large",
        "r5.xlarge",
        "r5.2xlarge",
        "r5.4xlarge",
        "r5.12xlarge",
        "r5.24xlarge",
        "r5d.large",
        "r5d.xlarge",
        "r5d.2xlarge",
        "r5d.4xlarge",
        "r5d.12xlarge",
        "r5d.24xlarge",
        "m5a.large",
        "m5a.xlarge",
        "m5a.2xlarge",
        "m5a.4xlarge",
        "m5a.12xlarge",
        "m5a.24xlarge",
        "r5a.large",
        "r5a.xlarge",
        "r5a.2xlarge",
        "r5a.4xlarge",
        "r5a.12xlarge",
        "r5a.24xlarge"
      ],
      "Default": "t2.micro",
	    "Description": "Instance type for Servers "
    },
    "ApplicationName": {
      "Type": "String",
      "Default": "Dev-procon8",
	  "Description": "Name of the Application"
    },
  
    "EnvironmentName": {
      "Type": "String",
      "Default": "dev-Procon8",
	  "Description": " Elastic Beanstalk Environmet name"
    },
    "VPC": {
      "Type": "AWS::EC2::VPC::Id",
      "Description": "The VPC ID to launch the application into."
    },
    "PublicSubnets": {
      "Type": "String",
	 
      "Description": "A comma-delimited list of public VPC subnets. ELBs will be launched into this subnet."
    },
    "PrivateSubnets": {
      "Type": "String",
	
      "Description": "A comma-delimited list of private VPC subnets. Auto Scaling and RDS will be launched into this subnet."
    }
  },
  "Resources": {
    "BeanstalkSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Allow the Elastic Beanstalk instances to access the NAT Gateway",
		"SecurityGroupIngress" : [ {
        "IpProtocol" : "tcp",
        "FromPort" : "80",
        "ToPort" : "80",
        "CidrIp" : "0.0.0.0/0"
      }],
        "VpcId": {
          "Ref": "VPC"
        }
      }
    },
    "Application": {
      "Type": "AWS::ElasticBeanstalk::Application",
      "Properties": {
        "Description": {
          "Ref": "ApplicationName"
        },
        "ApplicationName": {
          "Fn::Join": [
            "-",
            [
              {
                "Ref": "ApplicationName"
              }
            ]
          ]
        }
      }
    },
    "Procon8ApplicationVersion": {
      "Type": "AWS::ElasticBeanstalk::ApplicationVersion",
      "Properties": {
        "ApplicationName": {
          "Ref": "Application"
        },
        "Description": "AWS ElasticBeanstalk Sample Application Version",
        "SourceBundle": {
          "S3Bucket":  {
                    "Fn::Join": ["-", [{
                        "Ref": "BucketENV"
                    }, "procon8", {"Ref": "AWS::Region"}]]
                },
          "S3Key": {
            "Ref": "WarKey"
          }
        }
      }
    },
    "ProconConfigurationTemplate": {
      "Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
      "Properties": {
        "ApplicationName": {
          "Ref": "Application"
        },
        "Description": "AWS ElasticBeanstalk Procon8 Configuration Template",
        "SolutionStackName": "64bit Amazon Linux 2018.03 v2.8.9 running PHP 7.2",
        "OptionSettings": [
         
          {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "EC2KeyName",
            "Value": {
              "Ref": "KeyName"
            }
          },
		     {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "ImageId",
            "Value": {
              "Ref": "AWSLinuxImage"
            }
          },
          {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "VPCId",
            "Value": {
              "Ref": "VPC"
            }
          },
          {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "Subnets",
            "Value": {
              "Ref": "PrivateSubnets"
            }
          },
		   {
            "Namespace": "aws:ec2:vpc",
            "OptionName": "ELBSubnets",
            "Value": {
              "Ref": "PublicSubnets"
            }
          },
          {
            "Namespace": "aws:autoscaling:asg",
            "OptionName": "MinSize",
            "Value": {
              "Ref": "MinSize"
            }
          },
          {
            "Namespace": "aws:autoscaling:asg",
            "OptionName": "MaxSize",
            "Value": {
              "Ref": "MaxSize"
            }
          },
          {
            "Namespace": "aws:elasticbeanstalk:environment",
            "OptionName": "EnvironmentType",
            "Value": "LoadBalanced"
          },
		   
          {
            "Namespace": "aws:elasticbeanstalk:environment",
            "OptionName": "LoadBalancerType",
            "Value": "application"
          },
          {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "SecurityGroups",
            "Value": {
              "Ref": "BeanstalkSecurityGroup"
            }
          },
          {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "IamInstanceProfile",
            "Value": {
              "Ref": "EbAppInstanceProfile"
            }
          },
          {
            "Namespace": "aws:autoscaling:launchconfiguration",
            "OptionName": "InstanceType",
            "Value": {
              "Ref": "InstanceType"
            }
          }
        ]
      }
    },
    "Environment": {
      "Type": "AWS::ElasticBeanstalk::Environment",
      "Properties": {
        "ApplicationName": {
          "Ref": "Application"
        },
        "EnvironmentName": {
          "Fn::Join": [
            "-",
            [
              {
                "Ref": "EnvironmentName"
              }
            ]
          ]
        },
        "Description": "Default Environment",
        "TemplateName": {
          "Ref": "ProconConfigurationTemplate"
        },
        "VersionLabel": {
          "Ref": "Procon8ApplicationVersion"
        }
      }
    },
    "EbAppRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ec2.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/"
      }
    },
    "RolePolicies": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "EbApp",
        "PolicyDocument": {
          "Statement": [
            {
              "Effect": "Allow",
              "Action": "*",
              "Resource": "*"
            }
          ]
        },
        "Roles": [
          {
            "Ref": "EbAppRole"
          }
        ]
      }
    },
    "EbAppInstanceProfile": {
      "Type": "AWS::IAM::InstanceProfile",
      "Properties": {
        "Path": "/",
        "Roles": [
          {
            "Ref": "EbAppRole"
          }
        ]
      }
    }
  },
  "Outputs": {
    "URL": {
      "Description": "URL of the AWS Elastic Beanstalk Environment",
      "Value": {
        "Fn::Join": [
          "",
          [
            "http://",
            {
              "Fn::GetAtt": [
                "Environment",
                "EndpointURL"
              ]
            }
          ]
        ]
      }
    }
  }
}