簡體   English   中英

使用AWS Cloud Formation安裝和調試軟件包的正確安裝方式

[英]Correct Way to Install & Debug Installation of Packages With 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並為我的根帳戶運行aws configure 我如何從那里訪問日志。 因為EC2實例尚未運行,所以我不能只使用它。

完整模板:

    {
  "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"
        ]
      }
    }
  }
}

我認為第二季度的答案是正在執行的CF堆棧的資源選項卡。 在“資源”選項卡上,您將看到已創建哪些資源,哪些正在進行中以及哪些失敗。 使用“ DependsOn”參數,我們可以強制執行創建資源的順序。 因此,您可以設置模板以按順序創建資源,並且可以查明故障(如果有)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM