Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation ValidationError: Stack (stackname) is in CREATE_COMPLETE state and cannot be signaled

I have created an aws Cloudformation script and it launches but when i look at the logs its showing the following error:

ValidationError: Stack stackarn is in CREATE_COMPLETE state and cannot be signaled

I have looked at other posts regarding this error that have suggested the issue is with the instance id being derived from the first instance or the autoscaling group not having a desired capacity set neither of which is true for me.

Here is my cloud formation template

{
"AWSTemplateFormatVersion": "2010-09-09",
"Metadata": {
    "AWS::CloudFormation::Designer": {
        "90d29d54-61c0-4b64-8f16-bab58c2da737": {
            "size": {
                "width": 510,
                "height": 330
            },
            "position": {
                "x": 650,
                "y": 200
            },
            "z": 0,
            "embeds": [
                "9da7a6f1-462f-45c1-bc0c-10fe6a297ea0",
                "791e1b81-a4ed-4b9a-a1b6-e37f92201579",
                "d4706141-a8d6-4d08-b092-cabedf2d351f",
                "25f02a3f-31da-4e2a-8a3b-31cc9c860451"
            ]
        },
        "25f02a3f-31da-4e2a-8a3b-31cc9c860451": {
            "size": {
                "width": 250,
                "height": 110
            },
            "position": {
                "x": 850,
                "y": 250
            },
            "z": 1,
            "parent": "90d29d54-61c0-4b64-8f16-bab58c2da737",
            "embeds": [
                "9eb6713b-7a7f-46a2-8a53-76738c2069e7",
                "ee1affdf-e6fe-453f-b754-cf2de003f1d8",
                "e8849c27-1095-4667-906a-6f194c39e5bb"
            ]
        },
        "791e1b81-a4ed-4b9a-a1b6-e37f92201579": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 940,
                "y": 450
            },
            "z": 1,
            "parent": "90d29d54-61c0-4b64-8f16-bab58c2da737",
            "embeds": []
        },
        "11d6e6db-f8bf-4162-83f4-3b4cdd3ecd9c": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 280,
                "y": 340
            },
            "z": 0,
            "embeds": []
        },
        "75f9474e-7e06-4879-8e2e-42f9063117bf": {
            "source": {
                "id": "90d29d54-61c0-4b64-8f16-bab58c2da737"
            },
            "target": {
                "id": "11d6e6db-f8bf-4162-83f4-3b4cdd3ecd9c"
            },
            "z": 0
        },
        "9da7a6f1-462f-45c1-bc0c-10fe6a297ea0": {
            "size": {
                "width": 120,
                "height": 120
            },
            "position": {
                "x": 700,
                "y": 270
            },
            "z": 1,
            "parent": "90d29d54-61c0-4b64-8f16-bab58c2da737",
            "embeds": [
                "93a62438-214b-4c30-906d-c45d73f0a288"
            ]
        },
        "93a62438-214b-4c30-906d-c45d73f0a288": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 730,
                "y": 310
            },
            "z": 2,
            "parent": "9da7a6f1-462f-45c1-bc0c-10fe6a297ea0",
            "embeds": [],
            "isassociatedwith": [
                "11d6e6db-f8bf-4162-83f4-3b4cdd3ecd9c"
            ]
            "dependson": [
                "75f9474e-7e06-4879-8e2e-42f9063117bf"
            ]
        },
        "e9956922-4cfe-4d1a-a4c9-814613d2517e": {
            "source": {
                "id": "9da7a6f1-462f-45c1-bc0c-10fe6a297ea0"
            },
            "target": {
                "id": "25f02a3f-31da-4e2a-8a3b-31cc9c860451"
            },
            "z": 1
        },
        "e8849c27-1095-4667-906a-6f194c39e5bb": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 870,
                "y": 280
            },
            "z": 2,
            "parent": "25f02a3f-31da-4e2a-8a3b-31cc9c860451",
            "embeds": [],
            "isassociatedwith": [
                "ee1affdf-e6fe-453f-b754-cf2de003f1d8",
                "9eb6713b-7a7f-46a2-8a53-76738c2069e7"
            ]
            "dependson": [
                "93a62438-214b-4c30-906d-c45d73f0a288"
            ]
        },
        "9eb6713b-7a7f-46a2-8a53-76738c2069e7": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 940,
                "y": 280
            },
            "z": 2,
            "parent": "25f02a3f-31da-4e2a-8a3b-31cc9c860451",
            "embeds": [],
            "isassociatedwith": [
                "d4706141-a8d6-4d08-b092-cabedf2d351f"
            ]
        },
        "ee1affdf-e6fe-453f-b754-cf2de003f1d8": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 1020,
                "y": 280
            },
            "z": 2,
            "parent": "25f02a3f-31da-4e2a-8a3b-31cc9c860451",
            "embeds": [],
            "isassociatedwith": [
                "791e1b81-a4ed-4b9a-a1b6-e37f92201579"
            ]
        },
        "d4706141-a8d6-4d08-b092-cabedf2d351f": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 802.9767066348955,
                "y": 452.6381721949601
            },
            "z": 1,
            "parent": "90d29d54-61c0-4b64-8f16-bab58c2da737",
            "embeds": []

        },
        "f2021951-c570-4233-8ccd-a05be043632c": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 1260,
                "y": 210
            },
            "z": 0,
            "embeds": []
        },
        "c892fb7e-40c0-4391-be11-3e005ed16251": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 1370,
                "y": 210
            },
            "z": 0,
            "embeds": []
        },
        "582ee47a-34d2-4faa-956f-72bbe48b5ca1": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 1260,
                "y": 340
            },
            "z": 0,
            "embeds": []
        },
        "68ddbe84-8848-4920-8700-df090e65d755": {
            "size": {
                "width": 60,
                "height": 60
            },
            "position": {
                "x": 1370,
                "y": 340
            },
            "z": 0,
            "embeds": []
        }
    }
},
"Resources": {
    "VPC": {
        "Type": "AWS::EC2::VPC",
        "Properties": {
            "EnableDnsSupport": "true",
            "EnableDnsHostnames": "true",
            "CidrBlock": "10.0.0.0/16"
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "90d29d54-61c0-4b64-8f16-bab58c2da737"
            }
        }
    },
    "PublicSubnet": {
        "Type": "AWS::EC2::Subnet",
        "Properties": {
            "VpcId": {
                "Ref": "VPC"
            },
            "CidrBlock": "10.0.0.0/24"
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "25f02a3f-31da-4e2a-8a3b-31cc9c860451"
            }
        }
    },
    "PublicLoadBalancerSecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupDescription": "Public Elastic Load Balancing security group with HTTP access on port 80 from the Internet",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "CidrIp": "0.0.0.0/0"
                }
            ],
            "SecurityGroupEgress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "CidrIp": "0.0.0.0/0"
                }
            ],
            "VpcId": {
                "Ref": "VPC"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "791e1b81-a4ed-4b9a-a1b6-e37f92201579"
            }
        }
    },
    "InternetGateway": {
        "Type": "AWS::EC2::InternetGateway",
        "Properties": {},
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "11d6e6db-f8bf-4162-83f4-3b4cdd3ecd9c"
            }
        }
    },
    "EC2VPCG4YNHI": {
        "Type": "AWS::EC2::VPCGatewayAttachment",
        "Properties": {
            "VpcId": {
                "Ref": "VPC"
            },
            "InternetGatewayId": {
                "Ref": "InternetGateway"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "75f9474e-7e06-4879-8e2e-42f9063117bf"
            }
        }
    },
    "PublicRouteTable": {
        "Type": "AWS::EC2::RouteTable",
        "Properties": {
            "VpcId": {
                "Ref": "VPC"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "9da7a6f1-462f-45c1-bc0c-10fe6a297ea0"
            }
        }
    },
    "PublicRoute": {
        "Type": "AWS::EC2::Route",
        "Properties": {
            "DestinationCidrBlock": "0.0.0.0/0",
            "RouteTableId": {
                "Ref": "PublicRouteTable"
            },
            "GatewayId": {
                "Ref": "InternetGateway"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "93a62438-214b-4c30-906d-c45d73f0a288"
            }
        },
        "DependsOn": [
            "EC2VPCG4YNHI"
        ]
    },
    "EC2SRTA188EJ": {
        "Type": "AWS::EC2::SubnetRouteTableAssociation",
        "Properties": {
            "RouteTableId": {
                "Ref": "PublicRouteTable"
            },
            "SubnetId": {
                "Ref": "PublicSubnet"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "e9956922-4cfe-4d1a-a4c9-814613d2517e"
            }
        }
    },
    "WebServerFleet": {
        "Type": "AWS::AutoScaling::AutoScalingGroup",
        "Properties": {
            "MinSize": "1",
            "MaxSize": "10",
            "DesiredCapacity": {
                "Ref": "WebServerCount"
            },
            "VPCZoneIdentifier": [
                {
                    "Ref": "PublicSubnet"
                }
            ],
            "LoadBalancerNames": [
                {
                    "Ref": "PublicElasticLoadBalancer"
                }
            ],
            "LaunchConfigurationName": {
                "Ref": "WebServerLaunchConfig"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "e8849c27-1095-4667-906a-6f194c39e5bb"
            }
        },
        "DependsOn": [
            "PublicRoute"
        ]
    },
    "WebServerLaunchConfig": {
        "Type": "AWS::AutoScaling::LaunchConfiguration",
        "Properties": {
            "InstanceType": {
                "Ref": "InstanceType"
            },
            "ImageId": {
                "Fn::FindInMap": [
                    "AWSRegionArch2AMI",
                    {
                        "Ref": "AWS::Region"
                    },
                    {
                        "Fn::FindInMap": [
                            "AWSInstanceType2Arch",
                            {
                                "Ref": "InstanceType"
                            },
                            "Arch"
                        ]
                    }
                ]
            },
            "KeyName": {
                "Ref": "KeyName"
            },
            "AssociatePublicIpAddress": "true",
            "UserData": {
                "Fn::Base64": {
                    "Fn::Join": [
                        "",
                        [
                            "#!/bin/bash -xe\n",
                            "yum install -y aws-cfn-bootstrap\n",
                            "# Install the files and packages from the metadata\n",
                            "/opt/aws/bin/cfn-init -v ",
                            "         --stack ",
                            {
                                "Ref": "AWS::StackName"
                            },
                            "         --resource WebServerLaunchConfig ",
                            "         --configsets All ",
                            "         --region ",
                            {
                                "Ref": "AWS::Region"
                            },
                            "\n",
                            "#Install Redis\n",
                            "#rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm\n",
                            "rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm\n",
                            "yum --enablerepo=remi,remi-test -y install redis\n",
                            "chkconfig --add redis\n",
                            "chkconfig --level 345 redis on\n",
                            "service redis start\n",
                            "# Clone GitHub Repository\n",
                            "git clone -b master https://",
                            {
                                "Ref": "GitHubUsername"
                            },
                            ":",
                            {
                                "Ref": "GitHubPassword"
                            },
                            "@github.com/repo /var/www/html\n",
                            "# Update the httpd.conf file and restart the web server\n",
                            "mv -f /var/www/html/resources/conf/httpd.conf /etc/httpd/conf/httpd.conf \n",
                            "service httpd restart\n",
                            "# Install Composer\n",
                            "#chmod 755 -R /usr/local/bin\n",
                            "curl -sS https://getcomposer.org/installer > /tmp/installer\n",
                            "COMPOSER_HOME=/usr/local/composer php < /tmp/installer\n",
                            "mv composer.phar /usr/local/bin/composer.phar \n",
                            "groupadd www\n",
                            "usermod -a -G www ec2-user\n",
                            "usermod -a -G www apache\n",
                            "chown -R root:www /var/www\n",
                            "find /var/www -type d -exec chmod 2775 {} +\n",
                            "find /var/www -type f -exec chmod 0664 {} +\n",
                            "COMPOSER_HOME=/usr/local/composer /usr/local/bin/composer.phar install -d /var/www/html --no-plugins --no-scripts\n",
                            "mkdir /var/www/html/storage/framework\n",
                            "mkdir /var/www/html/storage/framework/views\n",
                            "mkdir /var/www/html/storage/framework/sessions\n",
                            "chmod -R 775 /var/www/html/storage\n",
                            "# Signal the status from cfn-init\n",
                            "/opt/aws/bin/cfn-signal -e $? ",
                            "         --stack ",
                            {
                                "Ref": "AWS::StackName"
                            },
                            "         --resource WebServerFleet ",
                            "         --region ",
                            {
                                "Ref": "AWS::Region"
                            },
                            "\n"
                        ]
                    ]
                }
            },
            "SecurityGroups": [
                {
                    "Ref": "WebServerSecurityGroup"
                }
            ]
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "9eb6713b-7a7f-46a2-8a53-76738c2069e7"
            },
            "AWS::CloudFormation::Init": {
                "configSets": {
                    "All": [
                        "installWebDependencies"
                    ]
                },
                "installWebDependencies": {
                    "packages": {
                        "yum": {
                            "httpd24": [],
                            "php56": [],
                            "php56-pdo": [],
                            "php56-mbstring": [],
                            "php56-mcrypt": [],
                            "php-ZendFramework-Db-Adapter-Pdo-Mysql.noarch": [],
                            "git": []
                        }
                    },
                    "services": {
                        "sysvinit": {
                            "httpd": {
                                "enabled": "true",
                                "ensureRunning": "true"
                            }
                        }
                    }
                }
            }
        }
    },
    "PublicElasticLoadBalancer": {
        "Type": "AWS::ElasticLoadBalancing::LoadBalancer",
        "Properties": {
            "Listeners": [
                {
                    "LoadBalancerPort": "80",
                    "InstancePort": "80",
                    "Protocol": "HTTP"
                }
            ],
            "HealthCheck": {
                "Target": "HTTP:80/",
                "HealthyThreshold": "3",
                "UnhealthyThreshold": "5",
                "Interval": "90",
                "Timeout": "60"
            },
            "Subnets": [
                {
                    "Ref": "PublicSubnet"
                }
            ],
            "SecurityGroups": [
                {
                    "Ref": "PublicLoadBalancerSecurityGroup"
                }
            ]
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "ee1affdf-e6fe-453f-b754-cf2de003f1d8"
            }
        }
    },
    "WebServerSecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupDescription": "Allow access from load balancer and SSH traffic",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "SourceSecurityGroupId": {
                        "Ref": "PublicLoadBalancerSecurityGroup"
                    }
                },
                {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "CidrIp": {
                        "Ref": "SSHLocation"
                    }
                },
                {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "CidrIp": "31.221.88.186/32"
                }
            ],
            "VpcId": {
                "Ref": "VPC"
            }
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "d4706141-a8d6-4d08-b092-cabedf2d351f"
            }
        }
    },
    "AnvilQueue": {
        "Type": "AWS::SQS::Queue",
        "Properties": {
            "QueueName": "phonehome-incident-anvil",
            "DelaySeconds": 0,
            "ReceiveMessageWaitTimeSeconds": 0,
            "VisibilityTimeout": 30,
            "MessageRetentionPeriod": 1209600
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "f2021951-c570-4233-8ccd-a05be043632c"
            }
        }
    },
    "ProcessQueue": {
        "Type": "AWS::SQS::Queue",
        "Properties": {
            "QueueName": "phonehome-incident-process",
            "DelaySeconds": 0,
            "ReceiveMessageWaitTimeSeconds": 0,
            "VisibilityTimeout": 30,
            "MessageRetentionPeriod": 1209600
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "c892fb7e-40c0-4391-be11-3e005ed16251"
            }
        }
    },
    "QueueDepthAlarm": {
        "Type": "AWS::CloudWatch::Alarm",
        "Properties": {
            "AlarmDescription": "Alarm if queue depth grows beyond 2000 messages",
            "Namespace": "AWS/SQS",
            "MetricName": "ApproximateNumberOfMessagesVisible",
            "Dimensions": [
                {
                    "Name": "AnvilQueue",
                    "Value": {
                        "Fn::GetAtt": [
                            "AnvilQueue",
                            "QueueName"
                        ]
                    }
                },
                {
                    "Name": "ProcessQueue",
                    "Value": {
                        "Fn::GetAtt": [
                            "ProcessQueue",
                            "QueueName"
                        ]
                    }
                }
            ],
            "Statistic": "Sum",
            "Period": "300",
            "EvaluationPeriods": "1",
            "Threshold": "2000",
            "ComparisonOperator": "GreaterThanThreshold",
            "AlarmActions": [
                {
                    "Ref": "AlarmTopic"
                }
            ],
            "InsufficientDataActions": [
                {
                    "Ref": "AlarmTopic"
                }
            ]
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "582ee47a-34d2-4faa-956f-72bbe48b5ca1"
            }
        }
    },
    "AlarmTopic": {
        "Type": "AWS::SNS::Topic",
        "Properties": {
            "Subscription": [
                {
                    "Endpoint": {
                        "Ref": "AlarmEmail"
                    },
                    "Protocol": "email"
                }
            ]
        },
        "Metadata": {
            "AWS::CloudFormation::Designer": {
                "id": "68ddbe84-8848-4920-8700-df090e65d755"
            }
        }
    }
},
"Parameters": {
    "AlarmEmail": {
        "Default": "[email protected]",
        "Description": "Email address to notify if operational problems arise",
        "Type": "String"
    },
    "InstanceType": {
        "Description": "WebServer EC2 instance type",
        "Type": "String",
        "Default": "t2.micro",
        "AllowedValues": [
            "t1.micro",
            "t2.micro",
            "t2.small",
            "t2.medium",
            "m1.small",
            "m1.medium",
            "m1.large"
        ],
        "ConstraintDescription": "must be a valid EC2 instance type."
    },
    "KeyName": {
        "Description": "Name of an EC2 KeyPair to enable SSH access to the instance.",
        "Type": "AWS::EC2::KeyPair::KeyName",
        "ConstraintDescription": "must be the name of an existing EC2 KeyPair."
    },
    "SSHLocation": {
        "Description": " The IP address range that can be used to access the web server using SSH.",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "0.0.0.0/0",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
    },
    "WebServerCount": {
        "Description": "Number of Amazon EC2 instances to launch for the WebServer server",
        "Type": "Number",
        "Default": "1"
    },
    "GitHubPassword": {
        "NoEcho": "true",
        "Description": "GitHub Password",
        "Type": "String"
    },
    "GitHubUsername": {
        "Description": "GitHub Username ",
        "Type": "String"
    }
},
"Mappings": {
    "AWSInstanceType2Arch": {
        "t1.micro": {
            "Arch": "PV64"
        },
        "t2.micro": {
            "Arch": "HVM64"
        },
        "t2.small": {
            "Arch": "HVM64"
        },
        "t2.medium": {
            "Arch": "HVM64"
        },
        "m1.small": {
            "Arch": "PV64"
        },
        "m1.medium": {
            "Arch": "PV64"
        },
        "m1.large": {
            "Arch": "PV64"
        }
    },
    "AWSRegionArch2AMI": {
        "eu-west-1": {
            "PV64": "ami-bf0897c8",
            "HVM64": "ami-a10897d6",
            "HVMG2": "ami-d5bc24a2"
        },
        "eu-west-2": {
            "PV64": "ami-896369ed",
            "HVM64": "ami-896369ed",
            "HVMG2": "ami-896369ed"
        },
        "eu-central-1": {
            "PV64": "ami-ac221fb1",
            "HVM64": "ami-a8221fb5",
            "HVMG2": "ami-7cd2ef61"
        }
    }
},
"Outputs": {
    "URL": {
        "Value": {
            "Fn::GetAtt": [
                "PublicElasticLoadBalancer",
                "DNSName"
            ]
        },
        "Description": "Newly created application URL"
    }
}
}

Note: some of the CF designer code has been excluded to stop stackoverflow complaining the file is too big. Any ideas what might be causing this issue?

like image 946
SamBremner Avatar asked Apr 12 '18 16:04

SamBremner


2 Answers

Seems like you are not waiting for your auto scaling group, so the stack creation completes before your ASG has any working instances. By the time the instance is up and executes cfn-signal, the stack has finished.

Try adding CreationPolicy:

https://aws.amazon.com/blogs/devops/use-a-creationpolicy-to-wait-for-on-instance-configurations/

and/or UpdatePolicy:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html

Something like the following, depending on the specifics of your case:

"WebServerFleet": {
  "CreationPolicy": {
    "ResourceSignal": {
      "Timeout": "PT15M",
      "Count": "1"
    }
  },
  "UpdatePolicy": {
    "AutoScalingScheduledAction": {
      "IgnoreUnmodifiedGroupSizeProperties": "true"
    },
    "AutoScalingRollingUpdate": {
      "MinInstancesInService": "1",
      "MaxBatchSize": "2",
      "WaitOnResourceSignals": "true",
      "PauseTime": "PT15M"
    }
  }
}

You may want to adjust the timeouts if your setup is longer or shorter. The batch size is also something to consider.

like image 70
kichik Avatar answered Oct 01 '22 23:10

kichik


I was having the same issue , CF stack was already reached to CREATE_COMPLETE so cfn-signal was of no use. Attached the CreationPolicy on EC2 resource to tell CloudFormation to wait for 1 success signal within 5 minutes before proceeding with stack creation and marking it CREATE_COMPLETE. So using cfn-signal along with CreationPolicy solved the issue. Below is the stack template

{
    "AWSTemplateFormatVersion" : "2010-09-09",

    "Description" : "AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",

    "Parameters" : {
      "KeyName": {
        "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
        "Type": "AWS::EC2::KeyPair::KeyName",
        "ConstraintDescription" : "Must be the name of an existing EC2 KeyPair."
      },

      "InstanceType" : {
        "Description" : "WebServer EC2 instance type",
        "Type" : "String",
        "Default" : "t2.micro",
        "AllowedValues" : [ "t1.micro", "t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large", "m1.small", "m1.medium",
                            "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.medium", "m3.large", "m3.xlarge", 
                            "m3.2xlarge", "m4.large", "m4.xlarge", "m4.2xlarge", "m4.4xlarge", "m4.10xlarge", "c1.medium", "c1.xlarge", 
                            "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c4.large", "c4.xlarge", "c4.2xlarge", "c4.4xlarge",
                            "c4.8xlarge", "g2.2xlarge", "g2.8xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge",
                            "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "d2.xlarge", "d2.2xlarge", "d2.4xlarge", "d2.8xlarge"]
  ,
        "ConstraintDescription" : "Must be a valid EC2 instance type."
      },

      "SSHLocation" : {
        "Description" : "The IP address range that can be used to SSH to the EC2 instances",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "0.0.0.0/0",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "Must be a valid IP CIDR range of the form x.x.x.x/x."
     }
    },

    "Mappings" : {
      "AWSInstanceType2Arch" : {
        "t1.micro"    : { "Arch" : "PV64"   }, "t2.nano"     : { "Arch" : "HVM64"  }, "t2.micro"    : { "Arch" : "HVM64"  },
        "t2.small"    : { "Arch" : "HVM64"  }, "t2.medium"   : { "Arch" : "HVM64"  }, "t2.large"    : { "Arch" : "HVM64"  },
        "m1.small"    : { "Arch" : "PV64"   }, "m1.medium"   : { "Arch" : "PV64"   }, "m1.large"    : { "Arch" : "PV64"   },
        "m1.xlarge"   : { "Arch" : "PV64"   }, "m2.xlarge"   : { "Arch" : "PV64"   }, "m2.2xlarge"  : { "Arch" : "PV64"   },
        "m2.4xlarge"  : { "Arch" : "PV64"   }, "m3.medium"   : { "Arch" : "HVM64"  }, "m3.large"    : { "Arch" : "HVM64"  },
        "m3.xlarge"   : { "Arch" : "HVM64"  }, "m3.2xlarge"  : { "Arch" : "HVM64"  }, "m4.large"    : { "Arch" : "HVM64"  },
        "m4.xlarge"   : { "Arch" : "HVM64"  }, "m4.2xlarge"  : { "Arch" : "HVM64"  }, "m4.4xlarge"  : { "Arch" : "HVM64"  },
        "m4.10xlarge" : { "Arch" : "HVM64"  }, "c1.medium"   : { "Arch" : "PV64"   }, "c1.xlarge"   : { "Arch" : "PV64"   },
        "c3.large"    : { "Arch" : "HVM64"  }, "c3.xlarge"   : { "Arch" : "HVM64"  }, "c3.2xlarge"  : { "Arch" : "HVM64"  },
        "c3.4xlarge"  : { "Arch" : "HVM64"  }, "c3.8xlarge"  : { "Arch" : "HVM64"  }, "c4.large"    : { "Arch" : "HVM64"  },
        "c4.xlarge"   : { "Arch" : "HVM64"  }, "c4.2xlarge"  : { "Arch" : "HVM64"  }, "c4.4xlarge"  : { "Arch" : "HVM64"  },
        "c4.8xlarge"  : { "Arch" : "HVM64"  }, "g2.2xlarge"  : { "Arch" : "HVMG2"  }, "g2.8xlarge"  : { "Arch" : "HVMG2"  },
        "r3.large"    : { "Arch" : "HVM64"  }, "r3.xlarge"   : { "Arch" : "HVM64"  }, "r3.2xlarge"  : { "Arch" : "HVM64"  },
        "r3.4xlarge"  : { "Arch" : "HVM64"  }, "r3.8xlarge"  : { "Arch" : "HVM64"  }, "i2.xlarge"   : { "Arch" : "HVM64"  },
        "i2.2xlarge"  : { "Arch" : "HVM64"  }, "i2.4xlarge"  : { "Arch" : "HVM64"  }, "i2.8xlarge"  : { "Arch" : "HVM64"  },
        "d2.xlarge"   : { "Arch" : "HVM64"  }, "d2.2xlarge"  : { "Arch" : "HVM64"  }, "d2.4xlarge"  : { "Arch" : "HVM64"  },
        "d2.8xlarge"  : { "Arch" : "HVM64"  }
      }
  ,
      "AWSRegionArch2AMI" : {
        "us-east-1"        : {"PV64" : "ami-2a69aa47", "HVM64" : "ami-97785bed", "HVMG2" : "ami-0a6e3770"},
        "us-west-2"        : {"PV64" : "ami-7f77b31f", "HVM64" : "ami-f2d3638a", "HVMG2" : "ami-ee15a196"},
        "us-west-1"        : {"PV64" : "ami-a2490dc2", "HVM64" : "ami-824c4ee2", "HVMG2" : "ami-0da4a46d"},
        "eu-west-1"        : {"PV64" : "ami-4cdd453f", "HVM64" : "ami-d834aba1", "HVMG2" : "ami-af8013d6"},
        "eu-west-2"        : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-403e2524", "HVMG2" : "NOT_SUPPORTED"},
        "eu-west-3"        : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-8ee056f3", "HVMG2" : "NOT_SUPPORTED"},
        "eu-central-1"     : {"PV64" : "ami-6527cf0a", "HVM64" : "ami-5652ce39", "HVMG2" : "ami-1d58ca72"},
        "ap-northeast-1"   : {"PV64" : "ami-3e42b65f", "HVM64" : "ami-ceafcba8", "HVMG2" : "ami-edfd658b"},
        "ap-northeast-2"   : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-863090e8", "HVMG2" : "NOT_SUPPORTED"},
        "ap-northeast-3"   : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-83444afe", "HVMG2" : "NOT_SUPPORTED"},
        "ap-southeast-1"   : {"PV64" : "ami-df9e4cbc", "HVM64" : "ami-68097514", "HVMG2" : "ami-c06013bc"},
        "ap-southeast-2"   : {"PV64" : "ami-63351d00", "HVM64" : "ami-942dd1f6", "HVMG2" : "ami-85ef12e7"},
        "ap-south-1"       : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-531a4c3c", "HVMG2" : "ami-411e492e"},
        "us-east-2"        : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-f63b1193", "HVMG2" : "NOT_SUPPORTED"},
        "ca-central-1"     : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-a954d1cd", "HVMG2" : "NOT_SUPPORTED"},
        "sa-east-1"        : {"PV64" : "ami-1ad34676", "HVM64" : "ami-84175ae8", "HVMG2" : "NOT_SUPPORTED"},
        "cn-north-1"       : {"PV64" : "ami-77559f1a", "HVM64" : "ami-cb19c4a6", "HVMG2" : "NOT_SUPPORTED"},
        "cn-northwest-1"   : {"PV64" : "ami-80707be2", "HVM64" : "ami-3e60745c", "HVMG2" : "NOT_SUPPORTED"}
      }

    },

    "Resources" : {
      "EC2Instance" : {
        "Type" : "AWS::EC2::Instance",
        "Metadata" : {
          "AWS::CloudFormation::Init" : {
              "configSets" : {
                  "InstallAndRun" : [ "config1", "config2" ]
              },
              "config1" : {
                  "packages" : {
                      "yum" : {
                          "httpd" : []
                      }
                  },
                  "files" : {
                      "/var/www/html/index.html" : {
                          "mode" : "000644", 
                          "owner" : "apache", 
                          "group" : "apache",
                          "source" : "https://s3.amazonaws.com/pcr-docs/index.html"
                      }
                  }
              },
              "config2" : {
                  "services" : {
                      "sysvinit" : {
                          "httpd" : {
                              "enabled" : "true",
                              "ensureRunning" : "true",
                              "packages" : { "yum" : ["httpd"] }
                          }
                      }
                  }
              }
          }
      },
        "Properties" : {
          "InstanceType" : { "Ref" : "InstanceType" },
          "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
          "KeyName" : { "Ref" : "KeyName" },
          "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
                            { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
          "UserData" : { "Fn::Base64" :
                              { "Fn::Join" : ["", [
                                 "#!/bin/bash -xe\n",
                                 "yum update -y aws-cfn-bootstrap\n",
                                 "# Install the files and packages from the metadata\n",
                                 "/opt/aws/bin/cfn-init -v ",
                                 "         --stack ", { "Ref" : "AWS::StackName" },
                                 "         --resource EC2Instance ",
                                 "         --configsets InstallAndRun ",
                                 "         --region ", { "Ref" : "AWS::Region" }, "\n",
                                 "# Signal the status from cfn-init\n",
                                 "/opt/aws/bin/cfn-signal -e $? ",
                                 "         --stack ", { "Ref" : "AWS::StackName" },
                                 "         --resource EC2Instance ",
                                 "         --region ", { "Ref" : "AWS::Region" }, "\n"
                              ]]}
                      }                  
        },
        "CreationPolicy": {
          "ResourceSignal": {
            "Count": "1",
            "Timeout": "PT5M"
          }
        }
      },

      "InstanceSecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties" : {
          "GroupDescription" : "Enable SSH access via port 22",
          "SecurityGroupIngress" : [ 
            { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : { "Ref" : "SSHLocation"} },
            { "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : { "Ref" : "SSHLocation"} }
           ]
        }
      }
    },

    "Outputs" : {
      "InstanceId" : {
        "Description" : "InstanceId of the newly created EC2 instance",
        "Value" : { "Ref" : "EC2Instance" }
      },
      "AZ" : {
        "Description" : "Availability Zone of the newly created EC2 instance",
        "Value" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ] }
      },
      "PublicDNS" : {
        "Description" : "Public DNSName of the newly created EC2 instance",
        "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicDnsName" ] }
      },
      "PublicIP" : {
        "Description" : "Public IP address of the newly created EC2 instance",
        "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicIp" ] }
      }
    }
  } 
like image 41
Mahtab Alam Avatar answered Oct 01 '22 21:10

Mahtab Alam