0

I'm very new to the AWS services. I'm trying to use the AWS cloud formation and I created a template. I am unable to create machines using that cause everytime the launch config tries to install the tomcat , it fails and then everything is rolled back.

The reason it rollback is because it doesnt find java_home even though i m setting it in my script

Below is my script. Please help me with fixing the tomcat installation.

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "Environment": {
            "Description": "Environment name. Allowed values dev, qa and live",
            "Type": "String",
            "Default": "dev",
            "AllowedValues": [ "dev", "qa", "live", "test" ]
        },
        "VPC": {
            "Description": "Select VPC",
            "Type": "AWS::EC2::VPC::Id"
        },
        "InstanceType": {
            "Description": "[type=vCPU,Mem,Storage] t2.micro=1,1,ebs t2.small=1,2,ebs t2.medium=2,4,ebs m3.medium=1,3.75,ssd(4gb) m3.large=2,7.5,ssd(32) m3.xlarge=4,15,ssd(2x40gb) m3.2xlarge=8,30,ssd(2x80)",
            "Type": "String",
            "Default": "t2.micro",
            "AllowedValues": [
                "t2.micro",
                "t2.small",
                "t2.medium",
                "m3.medium",
                "m3.large",
                "m3.xlarge",
                "m3.2xlarge"
            ],
            "ConstraintDescription": "[type=vCPU,Mem,Storage] t2.micro=1,1,ebs t2.small=1,2,ebs t2.medium=2,4,ebs m3.medium=1,3.75,ssd(4gb) m3.large=2,7.5,ssd(32) m3.xlarge=4,15,ssd(2x40gb) m3.2xlarge=8,30,ssd(2x80)"
        },
        "KeyPair": {
            "Description": "Select the EC2 keypair you want to use for Remote Desktop access",
            "Type": "AWS::EC2::KeyPair::KeyName"
        },
        "BuildNumber": {
            "Type": "String",
            "Default": "1.0.0"
        },
        "MinInstancesInService": {
            "Type": "String",
            "Default": "1"
        },
        "MaxInstancesInService": {
            "Type": "String",
            "Default": "3"
        },
        "WindowsType": {
            "Description": "Windows Core or Base",
            "Type": "String",
            "Default": "Core",
            "AllowedValues": [ "Base", "Core" ]
        }
    },

    "Mappings": {
        "Region2AMI": {
            "us-east-1": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            },
            "us-west-1": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            },
            "us-west-2": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            },
            "eu-west-1": {
                "Base": "ami-77ea3e00",
                "Core": "ami-1ba25a6c"
            },
            "sa-east-1": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            },
            "ap-southeast-1": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            },
            "ap-southeast-2": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            },
            "ap-northeast-1": {
                "Base": "ami-c1740ab6",
                "Core": "ami-a1bac4d6"
            }
        }
    },

    "Resources": {
        "RootRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [ "ec2.amazonaws.com" ]
                            },
                            "Action": [ "sts:AssumeRole" ]
                        }
                    ]
                },
                "Path": "/"
            }
        },
        "RolePolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": "S3-LEGO-ProdReg-WWW-FullAccess-LEGO-ProdReg-WWW",
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": [ "s3:ListAllMyBuckets" ],
                            "Resource": "arn:aws:s3:::*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "s3:ListBucket",
                                "s3:GetBucketLocation"
                            ],
                            "Resource": { "Fn::Join": [ "", [ "arn:aws:s3:::lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1" ] ] }
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "s3:PutObject",
                                "s3:GetObject",
                                "s3:DeleteObject"
                            ],
                            "Resource": { "Fn::Join": [ "", [ "arn:aws:s3:::lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1/*" ] ] }
                        },
                        {
                            "Effect": "Allow",
                            "Action": [ "ec2:Describe*" ],
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "dynamodb:GetItem",
                                "dynamodb:BatchGetItem",
                                "dynamodb:Query",
                                "dynamodb:PutItem",
                                "dynamodb:UpdateItem",
                                "dynamodb:DeleteItem",
                                "dynamodb:DescribeTable"
                            ],
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [
                                "sqs:SendMessage",
                                "sqs:ReceiveMessage",
                                "sqs:DeleteMessage",
                                "sqs:GetQueueUrl"
                            ],
                            "Resource": [ { "Fn::Join": [ "", [ "arn:aws:sqs:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":*" ] ] } ]
                        },
                        {
                            "Effect": "Allow",
                            "Action": [ "ec2:CreateTags" ],
                            "Resource": "*"
                        },
                        {
                            "Effect": "Allow",
                            "Action": [ "logs:*" ],
                            "Resource": "arn:aws:logs:*:*:*"
                        }
                    ]
                },
                "Roles": [ { "Ref": "RootRole" } ]
            }
        },
        "RootInstanceProfile": {
            "Type": "AWS::IAM::InstanceProfile",
            "Properties": {
                "Path": "/",
                "Roles": [ { "Ref": "RootRole" } ]
            }
        },

        "WWW": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": { "Fn::Join": [ "", [ "Security group for WWW on ", { "Ref": "Environment" }, " environment" ] ] },
                "VpcId": { "Ref": "VPC" },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "WWW"
                    },
                    {
                        "Key": "Environment",
                        "Value": { "Ref": "Environment" }
                    }
                ],
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "8080",
                        "ToPort": "8080",
                        "CidrIp": "171.20.68.68/32"
                    },
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "3389",
                        "ToPort": "3389",
                        "CidrIp": "171.20.68.68/32"
                    }
                ],
                "SecurityGroupEgress": [ ]
            }
        },
        "WWWIngress1": {
            "Type": "AWS::EC2::SecurityGroupIngress",
            "Properties": {
                "GroupId": {
                    "Ref": "WWW"
                },
                "IpProtocol": "tcp",
                "FromPort": "8080",
                "ToPort": "8080",
                "SourceSecurityGroupId": {
                    "Ref": "ElbWWW"
                }
            }
        },
        "ElbWWW": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": { "Fn::Join": [ "", [ "Security group for ELB for WWW for galleries on ", { "Ref": "Environment" }, " environment" ] ] },
                "VpcId": { "Ref": "VPC" },
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": "ELB WWW"
                    },
                    {
                        "Key": "Environment",
                        "Value": { "Ref": "Environment" }
                    }
                ],
                "SecurityGroupIngress": [
                    {
                        "IpProtocol": "tcp",
                        "FromPort": "8080",
                        "ToPort": "8080",
                        "CidrIp": "0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress": [ ]
            }
        },

        "WwwElasticLoadBalancer": {
            "Type": "AWS::ElasticLoadBalancing::LoadBalancer",
            "Properties": {
                "LoadBalancerName": { "Fn::Join": [ "-", [ "LEGO-ProdReg-WWW", { "Ref": "Environment" } ] ] },
                "AvailabilityZones": { "Fn::GetAZs": "" },
                "SecurityGroups": [ { "Ref": "ElbWWW" } ],
                "CrossZone": true,
                "Listeners": [
                    {
                        "LoadBalancerPort": "8080",
                        "InstancePort": "8080",
                        "Protocol": "HTTP"
                    }
                ],
                "HealthCheck": {
                    "Target": { "Fn::Join": [ "", [ "HTTP:", "8080", "/" ] ] },
                    "HealthyThreshold": "3",
                    "UnhealthyThreshold": "5",
                    "Interval": "10",
                    "Timeout": "5"
                },
                "Tags": [
                    { "Key": "Name", "Value": "LEGO-ProdReg-WWW" },
                    { "Key": "Environment", "Value": { "Ref": "Environment" } }
                ]
            }
        },

        "LaunchConfig": {
            "Type": "AWS::AutoScaling::LaunchConfiguration",
            "Properties": {
                "ImageId": { "Fn::FindInMap": [ "Region2AMI", { "Ref": "AWS::Region" }, { "Ref": "WindowsType" } ] },
                "SecurityGroups": [ { "Ref": "WWW" } ],
                "KeyName": { "Ref": "KeyPair" },
                "InstanceType": { "Ref": "InstanceType" },
                "IamInstanceProfile": { "Ref": "RootInstanceProfile" },
                "UserData": {
                    "Fn::Base64": {
                        "Fn::Join": [
                            "\r\n",
                            [
                                "",
                                { "Fn::Join": [ "", [ "cfn-init.exe -v -c \"full\" -s ", { "Ref": "AWS::StackId" }, " -r LaunchConfig", " --region ", { "Ref": "AWS::Region" } ] ] },
                                { "Fn::Join": [ "", [ "cfn-signal.exe -e %ERRORLEVEL% \"", { "Fn::Base64": { "Ref": "WindowsServerWaitHandle" } }, "\"" ] ] },
                                ""
                            ]
                        ]
                    }
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Authentication": {
                    "S3AccessCreds": {
                        "type": "s3",
                        "roleName": { "Ref": "RootRole" },
                        "buckets": { "Fn::Join": [ "", [ "lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1" ] ] }
                    }
                },
                "AWS::CloudFormation::Init": {
                    "configSets": {
                        "full": [ "configuration", "setup", "reset", "start" ],
                        "update": [ "configuration", "reset", "start" ]
                    },
                    "configuration": {
                        "sources": {
                            "C:\\tomcat": {
                                "Fn::Join": [ "", [ "https://s3-", { "Ref": "AWS::Region" }, ".amazonaws.com/lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1/apache-tomcat-8.0.21.zip" ] ]
                            },
                            "C:\\tomcat\\apache-tomcat-8.0.21": {
                                "Fn::Join": [ "", [ "https://s3-", { "Ref": "AWS::Region" }, ".amazonaws.com/lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1/conf.zip" ] ]
                            },
                            "C:\\tomcat\\apache-tomcat-8.0.21\\webapps": {
                                "Fn::Join": [ "", [ "https://s3-", { "Ref": "AWS::Region" }, ".amazonaws.com/lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1/ProductKeyGenerator.zip" ] ]
                            },
                            "c:\\cfn\\": {
                                "Fn::Join": [ "", [ "https://s3-", { "Ref": "AWS::Region" }, ".amazonaws.com/lego-", { "Ref": "Environment" }, "-prodreg-www-builds-r1/jdk-7u80-windows-x64.zip" ] ]
                            }
                        },
                        "files": {
                            "c:\\cfn\\cfn-hup.conf": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "[main]\n",
                                            "stack=",
                                            { "Ref": "AWS::StackId" },
                                            "\n",
                                            "region=",
                                            { "Ref": "AWS::Region" },
                                            "\n",
                                            "interval=1",
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf": {
                                "content": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "[cfn-auto-reloader-hook]\n",
                                            "triggers=post.update\n",
                                            "path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n",
                                            "action=cfn-init.exe -v -c \"update\" -s ",
                                            { "Ref": "AWS::StackId" },
                                            " -r LaunchConfig",
                                            " --region ",
                                            { "Ref": "AWS::Region" },
                                            "\n"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\time-server.cmd": {
                                "content": {
                                    "Fn::Join": [
                                        "\r\n",
                                        [
                                            "sc triggerinfo w32time start/networkon stop/networkoff",
                                            "reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\W32Time\\TimeProviders\\NtpClient\" /v SpecialPollInterval /t REG_DWORD /d 0x3c /f",
                                            "reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\W32Time\\Parameters\" /v Type /t REG_SZ /d \"NTP\" /f",
                                            "net stop w32time",
                                            "w32tm /config /syncfromflags:manual /manualpeerlist:\"0.pool.ntp.org,0x1 time.nist.gov,0x1 1.pool.ntp.org,0x1 2.pool.ntp.org,0x1\"",
                                            "net start w32time",
                                            "exit /B 0"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\windows-update.cmd": {
                                "content": {
                                    "Fn::Join": [
                                        "\r\n",
                                        [
                                            "reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\" /v AUOptions /t REG_DWORD /d 1 /f",
                                            "sc config wuauserv start= disabled",
                                            "net stop wuauserv",
                                            "sc config trustedinstaller start= disabled",
                                            "net stop trustedinstaller",
                                            "exit /B 0"
                                        ]
                                    ]
                                }
                            },
                            "c:\\cfn\\call-tomcat-service.cmd": {
                                "content": {
                                    "Fn::Join": [
                                        "\r\n",
                                        [
                        "netsh advfirewall firewall add rule name=\"Open Port 8080\" dir=in action=allow protocol=TCP localport=8080",
                                            "set JAVA_HOME=C:\\Program Files\\Java\\jdk1.7.0_80",
                                            "cd C:\\tomcat\\apache-tomcat-8.0.21\\bin",
                                            "call Service install",
                                            ":: sc config Tomcat8 start= auto",
                                            "exit /B 0"
                                        ]
                                    ]
                                }
                            }
                        },
                        "commands": {
                            "1-set-execution-policy": {
                                "command": "powershell set-executionpolicy remotesigned",
                                "waitAfterCompletion": 0
                            }
                        }
                    },
                    "setup": {
                        "commands": {
                            "1-windows-update": {
                                "command": "c:\\cfn\\windows-update.cmd",
                                "waitAfterCompletion": 0
                            },
                            "2-time-server": {
                                "command": "c:\\cfn\\time-server.cmd",
                                "waitAfterCompletion": 0
                            },
                            "3-Install-JDK": {
                                "command": "c:\\cfn\\jdk-7u80-windows-x64.exe /s",
                                "waitAfterCompletion": 120
                            },
                            "4-SetJavaHome": {
                                "command": "echo c:\\cfn\\set_JAVA_HOME.cmd",
                                "waitAfterCompletion": 0
                            },
                            "5-Install-Tomcat8-as-service": {
                                "command": "c:\\cfn\\call-tomcat-service.cmd",
                                "waitAfterCompletion": 5
                            }


                        },
                        "packages": {
                            "msi": {
                                "awscli": "https://s3.amazonaws.com/aws-cli/AWSCLI64.msi"
                            }
                        },
                        "services": {
                            "windows": {
                                "cfn-hup": { "enabled": "true", "ensureRunning": "true", "files": [ "c:\\cfn\\cfn-hup.conf", "c:\\cfn\\hooks.d\\cfn-auto-reloader.conf" ] }
                            }
                        }
                    },
                    "reset": {
                        "commands": {

                            "1-Stop-Tomcat": {
                                "command": "C:\\tomcat\\apache-tomcat-8.0.21\\bin\\Tomcat8 stop",
                                "waitAfterCompletion": 5
                            },
                            "2-Start-Tomcat": {
                                "command": "C:\\tomcat\\apache-tomcat-8.0.21\\bin\\Tomcat8 start",
                                "waitAfterCompletion": 5
                            }
                        }
                    },
                    "start": {
                        "commands": {
                            "1-Start-Tomcat": {
                                "command": "C:\\tomcat\\apache-tomcat-8.0.21\\bin\\Tomcat8 start",
                                "waitAfterCompletion": 5
                            }
                        }

                    }
                }
            }
        },

        "WindowsServerWaitHandle": {
            "Type": "AWS::CloudFormation::WaitConditionHandle"
        },
        "WindowsServerWaitCondition": {
            "Type": "AWS::CloudFormation::WaitCondition",
            "DependsOn": "LaunchConfig",
            "Properties": {
                "Handle": { "Ref": "WindowsServerWaitHandle" },
                "Timeout": "3600"
            }
        },

        "AutoScalingGroup": {
            "Type": "AWS::AutoScaling::AutoScalingGroup",
            "Properties": {
                "AvailabilityZones": { "Fn::GetAZs": "" },
                "Cooldown": 300,
                "LaunchConfigurationName": { "Ref": "LaunchConfig" },
                "MaxSize": { "Ref": "MaxInstancesInService" },
                "MinSize": { "Ref": "MinInstancesInService" },
                "HealthCheckGracePeriod": "900",
                "HealthCheckType": "ELB",
                "LoadBalancerNames": [
                    { "Fn::Join": [ "-", [ "LEGO", "ProdReg-WWW", { "Ref": "Environment" } ] ] }
                ],
                "Tags": [
                    {
                        "Key": "Name",
                        "Value": { "Fn::Join": [ "", [ "LEGO-ProdReg-WWW-", { "Ref": "Environment" }, "-", { "Ref": "BuildNumber" } ] ] },
                        "PropagateAtLaunch": "true"
                    },
                    { "Key": "Environment", "Value": { "Ref": "Environment" }, "PropagateAtLaunch": "true" }
                ]
            },
            "UpdatePolicy": {
                "AutoScalingRollingUpdate": {
                    "MinInstancesInService": "1",
                    "MaxBatchSize": "1",
                    "PauseTime": "PT5M"
                }
            }
        },

        "ScaleUpPolicy": {
            "Type": "AWS::AutoScaling::ScalingPolicy",
            "Properties": {
                "AdjustmentType": "ChangeInCapacity",
                "AutoScalingGroupName": { "Ref": "AutoScalingGroup" },
                "Cooldown": "600",
                "ScalingAdjustment": "1"
            }
        },
        "ScaleDownPolicy": {
            "Type": "AWS::AutoScaling::ScalingPolicy",
            "Properties": {
                "AdjustmentType": "ChangeInCapacity",
                "AutoScalingGroupName": { "Ref": "AutoScalingGroup" },
                "Cooldown": "600",
                "ScalingAdjustment": "-1"
            }
        },

        "CPUAlarmHigh": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties": {
                "EvaluationPeriods": "3",
                "Statistic": "Average",
                "Threshold": "90",
                "AlarmDescription": "Alarm if CPU too high or metric disappears indicating instance is down",
                "Period": "60",
                "AlarmActions": [ { "Ref": "ScaleUpPolicy" } ],
                "Namespace": "AWS/EC2",
                "Dimensions": [
                    {
                        "Name": "AutoScalingGroupName",
                        "Value": { "Ref": "AutoScalingGroup" }
                    }
                ],
                "ComparisonOperator": "GreaterThanThreshold",
                "MetricName": "CPUUtilization"
            }
        },
        "CPUAlarmLow": {
            "Type": "AWS::CloudWatch::Alarm",
            "Properties": {
                "EvaluationPeriods": "3",
                "Statistic": "Average",
                "Threshold": "30",
                "AlarmDescription": "Alarm if CPU is low, to trigger scale down",
                "Period": "60",
                "AlarmActions": [ { "Ref": "ScaleDownPolicy" } ],
                "Namespace": "AWS/EC2",
                "Dimensions": [
                    {
                        "Name": "AutoScalingGroupName",
                        "Value": { "Ref": "AutoScalingGroup" }
                    }
                ],
                "ComparisonOperator": "LessThanThreshold",
                "MetricName": "CPUUtilization"
            }
        }
    },
    "Description": "EC2-Tomcat-Cluster for Product Registrations DEV"
}
Razique
  • 2,276
  • 1
  • 19
  • 23

0 Answers0