正确的安装方式&使用AWS Cloud Formation调试软件包的安装

时间:2016-06-08 09:31:56

标签: amazon-web-services amazon-ec2 amazon-cloudformation

我想通过云形成来设置我的基础架构。据我了解,有两种方法可以安装软件包。

选项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) 我没有看到Debug 1中提到的任何失败消息,如何重写模板,这些消息在事件中正确显示。

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

问题3) 我在Mac和Mac上安装了aws为我的root帐户运行aws configure。如何从那里访问日志。因为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"
        ]
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

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