2016-06-08 103 views
0

我想通过云形成来设置我的基础架构。据我了解,有两种安装软件包的方法。正确的安装和调试方法使用AWS Cloud Formation安装软件包

选项1)

 "packages": { 
      "apt-get": { 
      "epel-release": [], 
      "gcc-c++": [], 
      "make": [], 
      "git": [] 
      } 
     } 

选项2)随着等待条件

"UserData": { 
    "Fn::Base64": { 
    "Fn::Join": [ 
     "", 
     [ 
     "#!/bin/bash -ex\n", 
     "apt-get update -y aws-cfn-bootstrap\n", 
     "apt-get install -y aws-cli\n", 
     ... 

现在我等待条件超时安装包时。我读到目前为止,有三个选项:

调试1)发送一个错误,当安装失败

"npm install pm2 -g || error_exit 'Failed to install pm2.'\n", 

调试2)成功步骤后发送积极的信号:

"/opt/aws/bin/cfn-signal -e 0 -r \"AWS installed node & npm.\" '", 

调试3)检出日志 http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html

问题

问题1) 我没有看到调试1中提到的任何失败消息,我如何重写模板,以确保这些消息在事件中正确显示。

问题2) 也许是因为它已经在第一步失败了。但是,我可以看到/使其可见,哪些子步骤已通过?

问题3) 我在我的Mac &我的根帐户运行aws configure安装aws。我如何从那里访问日志。由于EC2实例尚未运行,因此我不能只是ssh。

完整的模板:

{ 
    "Description": "Create instances ready for CodeDeploy: Create up to 3 Amazon EC2 instances with an associated instance profile and install the AWS CodeDeploy Agent. **WARNING** This template creates one or more Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template. Copyright [2012-2014] Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.", 
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Parameters": { 
    "TagKey": { 
     "Description": "The EC2 tag key that identifies this as a target for deployments.", 
     "Type": "String", 
     "Default": "Name", 
     "AllowedPattern": "[\\x20-\\x7E]*", 
     "ConstraintDescription": "Can contain only ASCII characters." 
    }, 
    "TagValue": { 
     "Description": "The EC2 tag value that identifies this as a target for deployments.", 
     "Type": "String", 
     "Default": "golden-cow", 
     "AllowedPattern": "[\\x20-\\x7E]*", 
     "ConstraintDescription": "Can contain only ASCII characters." 
    }, 
    "KeyPairName": { 
     "Description": "Name of an existing Amazon EC2 key pair to enable SSH or RDP access to the instances.", 
     "Type": "String", 
     "MinLength": "1", 
     "MaxLength": "255", 
     "AllowedPattern": "[\\x20-\\x7E]*", 
     "Default": "AWS Golden Cow", 
     "ConstraintDescription": "KeyPairName is a required Field and can contain only ASCII characters." 
    }, 
    "InstanceType": { 
     "Description": "Amazon EC2 instance type.", 
     "Type": "String", 
     "Default": "t2.micro", 
     "ConstraintDescription": "Must be a valid Amazon EC2 instance type." 
    }, 
    "InstanceCount": { 
     "Description": "Number of Amazon EC2 instances (Must be a number between 1 and 1).", 
     "Type": "Number", 
     "Default": "1", 
     "ConstraintDescription": "Must be a number between 1 and 3.", 
     "MinValue": "1", 
     "MaxValue": "1" 
    }, 
    "OperatingSystem": { 
     "Description": "Amazon EC2 operating system type (Linux or Windows).", 
     "Type": "String", 
     "Default": "Linux", 
     "ConstraintDescription": "Linux", 
     "AllowedValues": [ 
     "Linux" 
     ] 
    }, 
    "SSHLocation": { 
     "Description": "The IP address range that can be used to connect using SSH or RDP to the Amazon 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": { 
    "RegionOS2AMI": { 
     "eu-central-1": { 
     "Linux": "ami-87564feb" 
     } 
    }, 
    "OS2SSHPort": { 
     "Linux": { 
     "SSHPort": "22" 
     } 
    } 
    }, 
    "Conditions": { 
    "LaunchInstance1": { 
     "Fn::Equals": [ 
     "1", 
     { 
      "Ref": "InstanceCount" 
     } 
     ] 
    } 
    }, 
    "Resources": { 
    "LinuxEC2Instance": { 
     "Type": "AWS::EC2::Instance", 
     "Condition": "LaunchInstance1", 
     "Metadata": { 
     "AWS::CloudFormation::Init": { 
      "config": { 
      "services": { 
       "sysvint": { 
       "codedeploy-agent": { 
        "enabled": "true", 
        "ensureRunning": "true" 
       } 
       } 
      }, 
      "packages": { 
       "apt-get": { 
       "epel-release": [], 
       "gcc-c++": [], 
       "make": [], 
       "git": [] 
       } 
      } 
      } 
     }, 
     "AWS::CloudFormation::Designer": { 
      "id": "df094acb-0425-4ae5-bfc1-18c94c3d90c1" 
     } 
     }, 
     "Properties": { 
     "ImageId": { 
      "Fn::FindInMap": [ 
      "RegionOS2AMI", 
      { 
       "Ref": "AWS::Region" 
      }, 
      { 
       "Ref": "OperatingSystem" 
      } 
      ] 
     }, 
     "InstanceType": { 
      "Ref": "InstanceType" 
     }, 
     "SecurityGroups": [ 
      { 
      "Ref": "SecurityGroup" 
      } 
     ], 
     "UserData": { 
      "Fn::Base64": { 
      "Fn::Join": [ 
       "", 
       [ 
       "#!/bin/bash -ex\n", 
       "apt-get update -y aws-cfn-bootstrap\n", 
       "apt-get install -y aws-cli\n", 
       "/opt/aws/bin/cfn-signal -e 0 -r \"AWS installed CLI.\" '", 
       "# Helper function.\n", 
       "function error_exit\n", 
       "{\n", 
       " /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", 
       { 
        "Ref": "WaitHandle" 
       }, 
       "'\n", 
       " exit 1\n", 
       "}\n", 
       "#Install nodejs, npm\n", 
       "curl -sL https://deb.nodesource.com/setup_6.x\n", 
       "apt-get install -y nodejs npm || error_exit 'Failed to install nodejs.'\n", 
       "npm install pm2 -g || error_exit 'Failed to install pm2.'\n", 
       "/opt/aws/bin/cfn-signal -e 0 -r \"AWS installed node & npm.\" '", 
       "# Install the AWS CodeDeploy Agent.\n", 
       "cd /home/ec2-user/\n", 
       "aws s3 cp 's3://aws-codedeploy-eu-central-1/latest/codedeploy-agent_all.deb' . || error_exit 'Failed to download AWS CodeDeploy Agent.'\n", 
       "apt-get -y install codedeploy-agent_all.deb || error_exit 'Failed to install AWS CodeDeploy Agent.' \n", 
       "/opt/aws/bin/cfn-init -s ", 
       { 
        "Ref": "AWS::StackId" 
       }, 
       " -r LinuxEC2Instance --region ", 
       { 
        "Ref": "AWS::Region" 
       }, 
       " || error_exit 'Failed to run cfn-init.'\n", 
       "# All is well, so signal success.\n", 
       "/opt/aws/bin/cfn-signal -e 0 -r \"AWS CodeDeploy Agent setup complete.\" '", 
       { 
        "Ref": "WaitHandle" 
       }, 
       "'\n" 
       ] 
      ] 
      } 
     }, 
     "KeyName": { 
      "Ref": "KeyPairName" 
     }, 
     "Tags": [ 
      { 
      "Key": { 
       "Ref": "TagKey" 
      }, 
      "Value": { 
       "Ref": "TagValue" 
      } 
      } 
     ], 
     "IamInstanceProfile": { 
      "Ref": "InstanceRoleInstanceProfile" 
     } 
     } 
    }, 
    "WaitHandle": { 
     "Type": "AWS::CloudFormation::WaitConditionHandle", 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "761ddc9a-7c3b-41ca-9fa1-21429046b271" 
     } 
     } 
    }, 
    "WaitCondition": { 
     "Type": "AWS::CloudFormation::WaitCondition", 
     "Properties": { 
     "Count": 3, 
     "Handle": { 
      "Ref": "WaitHandle" 
     }, 
     "Timeout": "900" 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "a2c91b03-2c8c-4bd5-9c44-efdb89cf5375" 
     } 
     } 
    }, 
    "SecurityGroup": { 
     "Type": "AWS::EC2::SecurityGroup", 
     "Properties": { 
     "GroupDescription": "Enable HTTP access via port 80 and SSH access.", 
     "SecurityGroupIngress": [ 
      { 
      "IpProtocol": "tcp", 
      "FromPort": "80", 
      "ToPort": "80", 
      "CidrIp": "0.0.0.0/0" 
      }, 
      { 
      "IpProtocol": "tcp", 
      "FromPort": { 
       "Fn::FindInMap": [ 
       "OS2SSHPort", 
       { 
        "Ref": "OperatingSystem" 
       }, 
       "SSHPort" 
       ] 
      }, 
      "ToPort": { 
       "Fn::FindInMap": [ 
       "OS2SSHPort", 
       { 
        "Ref": "OperatingSystem" 
       }, 
       "SSHPort" 
       ] 
      }, 
      "CidrIp": { 
       "Ref": "SSHLocation" 
      } 
      } 
     ] 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "841b5a35-52f2-4887-bd4b-7b9a7dd90dc3" 
     } 
     } 
    }, 
    "CodeDeployTrustRole": { 
     "Type": "AWS::IAM::Role", 
     "Properties": { 
     "AssumeRolePolicyDocument": { 
      "Statement": [ 
      { 
       "Sid": "1", 
       "Effect": "Allow", 
       "Principal": { 
       "Service": [ 
        "codedeploy.us-east-1.amazonaws.com", 
        "codedeploy.us-west-2.amazonaws.com", 
        "codedeploy.eu-west-1.amazonaws.com", 
        "codedeploy.eu-central-1.amazonaws.com" 
       ] 
       }, 
       "Action": "sts:AssumeRole" 
      } 
      ] 
     }, 
     "Path": "/" 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "47a08921-1b89-46f7-8874-7d41e8e3595f" 
     } 
     } 
    }, 
    "CodeDeployRolePolicies": { 
     "Type": "AWS::IAM::Policy", 
     "Properties": { 
     "PolicyName": "CodeDeployPolicy", 
     "PolicyDocument": { 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Resource": [ 
       "*" 
       ], 
       "Action": [ 
       "ec2:Describe*" 
       ] 
      }, 
      { 
       "Effect": "Allow", 
       "Resource": [ 
       "*" 
       ], 
       "Action": [ 
       "autoscaling:CompleteLifecycleAction", 
       "autoscaling:DeleteLifecycleHook", 
       "autoscaling:DescribeLifecycleHooks", 
       "autoscaling:DescribeAutoScalingGroups", 
       "autoscaling:PutLifecycleHook", 
       "autoscaling:RecordLifecycleActionHeartbeat" 
       ] 
      } 
      ] 
     }, 
     "Roles": [ 
      { 
      "Ref": "CodeDeployTrustRole" 
      } 
     ] 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "6b960024-669a-49c2-be54-ac96c32be1f7" 
     } 
     } 
    }, 
    "InstanceRole": { 
     "Type": "AWS::IAM::Role", 
     "Properties": { 
     "AssumeRolePolicyDocument": { 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Principal": { 
       "Service": [ 
        "ec2.amazonaws.com" 
       ] 
       }, 
       "Action": [ 
       "sts:AssumeRole" 
       ] 
      } 
      ] 
     }, 
     "Path": "/" 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "7d432a6d-0c26-4570-8043-36dab502b408" 
     } 
     } 
    }, 
    "InstanceRolePolicies": { 
     "Type": "AWS::IAM::Policy", 
     "Properties": { 
     "PolicyName": "InstanceRole", 
     "PolicyDocument": { 
      "Statement": [ 
      { 
       "Effect": "Allow", 
       "Action": [ 
       "autoscaling:Describe*", 
       "cloudformation:Describe*", 
       "cloudformation:GetTemplate", 
       "s3:Get*" 
       ], 
       "Resource": "*" 
      } 
      ] 
     }, 
     "Roles": [ 
      { 
      "Ref": "InstanceRole" 
      } 
     ] 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "729a7797-a918-420b-a063-eed7adcda437" 
     } 
     } 
    }, 
    "InstanceRoleInstanceProfile": { 
     "Type": "AWS::IAM::InstanceProfile", 
     "Properties": { 
     "Path": "/", 
     "Roles": [ 
      { 
      "Ref": "InstanceRole" 
      } 
     ] 
     }, 
     "Metadata": { 
     "AWS::CloudFormation::Designer": { 
      "id": "8088e2e8-777e-4b49-9fe7-a36c8d76f6ee" 
     } 
     } 
    } 
    }, 
    "Outputs": { 
    "CodeDeployTrustRoleARN": { 
     "Value": { 
     "Fn::GetAtt": [ 
      "CodeDeployTrustRole", 
      "Arn" 
     ] 
     } 
    } 
    }, 
    "Metadata": { 
    "AWS::CloudFormation::Designer": { 
     "7d432a6d-0c26-4570-8043-36dab502b408": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 60, 
      "y": 90 
     }, 
     "z": 1, 
     "embeds": [] 
     }, 
     "8088e2e8-777e-4b49-9fe7-a36c8d76f6ee": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 180, 
      "y": 90 
     }, 
     "z": 1, 
     "embeds": [], 
     "isassociatedwith": [ 
      "7d432a6d-0c26-4570-8043-36dab502b408" 
     ] 
     }, 
     "729a7797-a918-420b-a063-eed7adcda437": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 60, 
      "y": 210 
     }, 
     "z": 1, 
     "embeds": [], 
     "isassociatedwith": [ 
      "7d432a6d-0c26-4570-8043-36dab502b408" 
     ] 
     }, 
     "47a08921-1b89-46f7-8874-7d41e8e3595f": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 180, 
      "y": 210 
     }, 
     "z": 1, 
     "embeds": [] 
     }, 
     "6b960024-669a-49c2-be54-ac96c32be1f7": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 300, 
      "y": 90 
     }, 
     "z": 1, 
     "embeds": [], 
     "isassociatedwith": [ 
      "47a08921-1b89-46f7-8874-7d41e8e3595f" 
     ] 
     }, 
     "841b5a35-52f2-4887-bd4b-7b9a7dd90dc3": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 300, 
      "y": 210 
     }, 
     "z": 1, 
     "embeds": [] 
     }, 
     "761ddc9a-7c3b-41ca-9fa1-21429046b271": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 60, 
      "y": 330 
     }, 
     "z": 1, 
     "embeds": [] 
     }, 
     "044d2ef7-e348-4e63-8d56-40c4c22cf6a3": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 180, 
      "y": 330 
     }, 
     "z": 1, 
     "embeds": [], 
     "ismemberof": [ 
      "841b5a35-52f2-4887-bd4b-7b9a7dd90dc3" 
     ], 
     "isrelatedto": [ 
      "761ddc9a-7c3b-41ca-9fa1-21429046b271", 
      "8088e2e8-777e-4b49-9fe7-a36c8d76f6ee" 
     ] 
     }, 
     "90b878c4-8d68-4e73-a523-d0abc821a603": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 300, 
      "y": 330 
     }, 
     "z": 1, 
     "embeds": [], 
     "ismemberof": [ 
      "841b5a35-52f2-4887-bd4b-7b9a7dd90dc3" 
     ], 
     "isrelatedto": [ 
      "761ddc9a-7c3b-41ca-9fa1-21429046b271", 
      "8088e2e8-777e-4b49-9fe7-a36c8d76f6ee" 
     ] 
     }, 
     "a2c91b03-2c8c-4bd5-9c44-efdb89cf5375": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 420, 
      "y": 90 
     }, 
     "z": 1, 
     "embeds": [], 
     "references": [ 
      "761ddc9a-7c3b-41ca-9fa1-21429046b271" 
     ] 
     }, 
     "df094acb-0425-4ae5-bfc1-18c94c3d90c1": { 
     "size": { 
      "width": 60, 
      "height": 60 
     }, 
     "position": { 
      "x": 420, 
      "y": 210 
     }, 
     "z": 1, 
     "embeds": [], 
     "ismemberof": [ 
      "841b5a35-52f2-4887-bd4b-7b9a7dd90dc3" 
     ], 
     "isrelatedto": [ 
      "761ddc9a-7c3b-41ca-9fa1-21429046b271", 
      "8088e2e8-777e-4b49-9fe7-a36c8d76f6ee" 
     ] 
     } 
    } 
    } 
} 
+0

你确定你不能ssh到实例吗?当CF堆栈创建失败时,您是否因失败而停用回滚,以便您的实例不会被销毁? –

+0

我不知道这是可能的。我是否必须在LinuxEC2Instance之后添加'DisableRollback:false':{'或开头的右边例如在'“Parameters”之后:{' –

+0

它不在CloudFormation模板中。这是您创建堆栈时的一个选项。如果您使用AWS控制台,它位于“高级”选项(第3个屏幕)上。如果您使用AWS CLI,则有一个选项:http://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html –

回答

1

我认为答案Q2是执行CF堆栈的资源标签。在资源选项卡上,您会看到哪些资源已创建,哪些资源正在进行中,哪些资源已失败。使用“DependsOn”参数,我们可以执行创建资源的顺序。因此,您可以将您的模板设置为按顺序创建资源,并且可以查明故障(如果有的话)。