我想通過雲形成來設置我的基礎架構。據我瞭解,有兩種安裝軟件包的方法。正確的安裝和調試方法使用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"
]
}
}
}
}
你確定你不能ssh到實例嗎?當CF堆棧創建失敗時,您是否因失敗而停用回滾,以便您的實例不會被銷燬? –
我不知道這是可能的。我是否必須在LinuxEC2Instance之後添加'DisableRollback:false':{'或開頭的右邊例如在'「Parameters」之後:{' –
它不在CloudFormation模板中。這是您創建堆棧時的一個選項。如果您使用AWS控制檯,它位於「高級」選項(第3個屏幕)上。如果您使用AWS CLI,則有一個選項:http://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html –