我正在尝试创建一个简单的Web部署模板,该模板引用另一个模板中的资源。实例创建成功,但因信号失败错误而终止。我是CloudFormation的新手,似乎在这里遇到了障碍-
我能够启动Linux EC2实例,安装httpd并创建一个index.html,但是代码无法执行似乎帮助程序的脚本
AWSTemplateFormatVersion: 2010-09-09
Description: This template will create a web instance by refering an existing VPC created by another template.
Parameters:
NetworkStackName:
Description: >-
Name of an active CloudFormation stack that contains the networking
resources, such as the subnet and security group, that will be used in
this stack.
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
Default: AR3Resource
InstanceType:
Description: 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
- hi1.4xlarge
- hs1.8xlarge
- cr1.8xlarge
- cc2.8xlarge
- cg1.4xlarge
ConstraintDescription: Must be a valid Instance type.
KeyName:
Description: Existing KeyPair name
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Must be an existing KeyPair from the region where instance is being created.
# IPCidrRange:
# 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: HVM64
t2.nano:
Arch: HVM64
t2.micro:
Arch: HVM64
t2.small:
Arch: HVM64
t2.medium:
Arch: HVM64
t2.large:
Arch: HVM64
m1.small:
Arch: HVM64
m1.medium:
Arch: HVM64
m1.large:
Arch: HVM64
m1.xlarge:
Arch: HVM64
m2.xlarge:
Arch: HVM64
m2.2xlarge:
Arch: HVM64
m2.4xlarge:
Arch: HVM64
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: HVM64
c1.xlarge:
Arch: HVM64
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
hi1.4xlarge:
Arch: HVM64
hs1.8xlarge:
Arch: HVM64
cr1.8xlarge:
Arch: HVM64
cc2.8xlarge:
Arch: HVM64
AWSInstanceType2NATArch:
t1.micro:
Arch: NATHVM64
t2.nano:
Arch: NATHVM64
t2.micro:
Arch: NATHVM64
t2.small:
Arch: NATHVM64
t2.medium:
Arch: NATHVM64
t2.large:
Arch: NATHVM64
m1.small:
Arch: NATHVM64
m1.medium:
Arch: NATHVM64
m1.large:
Arch: NATHVM64
m1.xlarge:
Arch: NATHVM64
m2.xlarge:
Arch: NATHVM64
m2.2xlarge:
Arch: NATHVM64
m2.4xlarge:
Arch: NATHVM64
m3.medium:
Arch: NATHVM64
m3.large:
Arch: NATHVM64
m3.xlarge:
Arch: NATHVM64
m3.2xlarge:
Arch: NATHVM64
m4.large:
Arch: NATHVM64
m4.xlarge:
Arch: NATHVM64
m4.2xlarge:
Arch: NATHVM64
m4.4xlarge:
Arch: NATHVM64
m4.10xlarge:
Arch: NATHVM64
c1.medium:
Arch: NATHVM64
c1.xlarge:
Arch: NATHVM64
c3.large:
Arch: NATHVM64
c3.xlarge:
Arch: NATHVM64
c3.2xlarge:
Arch: NATHVM64
c3.4xlarge:
Arch: NATHVM64
c3.8xlarge:
Arch: NATHVM64
c4.large:
Arch: NATHVM64
c4.xlarge:
Arch: NATHVM64
c4.2xlarge:
Arch: NATHVM64
c4.4xlarge:
Arch: NATHVM64
c4.8xlarge:
Arch: NATHVM64
g2.2xlarge:
Arch: NATHVMG2
g2.8xlarge:
Arch: NATHVMG2
r3.large:
Arch: NATHVM64
r3.xlarge:
Arch: NATHVM64
r3.2xlarge:
Arch: NATHVM64
r3.4xlarge:
Arch: NATHVM64
r3.8xlarge:
Arch: NATHVM64
i2.xlarge:
Arch: NATHVM64
i2.2xlarge:
Arch: NATHVM64
i2.4xlarge:
Arch: NATHVM64
i2.8xlarge:
Arch: NATHVM64
d2.xlarge:
Arch: NATHVM64
d2.2xlarge:
Arch: NATHVM64
d2.4xlarge:
Arch: NATHVM64
d2.8xlarge:
Arch: NATHVM64
hi1.4xlarge:
Arch: NATHVM64
hs1.8xlarge:
Arch: NATHVM64
cr1.8xlarge:
Arch: NATHVM64
cc2.8xlarge:
Arch: NATHVM64
AWSRegionArch2AMI:
us-east-1:
PV64: ami-8ff710e2
HVM64: ami-f5f41398
HVMG2: ami-4afd1d27
us-west-2:
PV64: ami-eff1028f
HVM64: ami-d0f506b0
HVMG2: ami-ee897b8e
us-west-1:
PV64: ami-ac85fbcc
HVM64: ami-6e84fa0e
HVMG2: ami-69106909
eu-west-1:
PV64: ami-23ab2250
HVM64: ami-b0ac25c3
HVMG2: ami-936de5e0
eu-central-1:
PV64: ami-27c12348
HVM64: ami-d3c022bc
HVMG2: ami-8e7092e1
ap-northeast-1:
PV64: ami-26160d48
HVM64: ami-29160d47
HVMG2: ami-91809aff
ap-northeast-2:
PV64: NOT_SUPPORTED
HVM64: ami-cf32faa1
HVMG2: NOT_SUPPORTED
ap-southeast-1:
PV64: ami-f3dd0a90
HVM64: ami-1ddc0b7e
HVMG2: ami-3c30e75f
ap-southeast-2:
PV64: ami-8f94b9ec
HVM64: ami-0c95b86f
HVMG2: ami-543d1137
sa-east-1:
PV64: ami-e188018d
HVM64: ami-fb890097
HVMG2: NOT_SUPPORTED
cn-north-1:
PV64: ami-77a46e1a
HVM64: ami-05a66c68
HVMG2: NOT_SUPPORTED
Resources:
AR3Webserver:
Type: AWS::EC2::Instance
Metadata:
'AWS::CloudFormation::Init':
configSets:
All:
- ConfigureApp
ConfigureApp:
Packages:
yum:
httpd: []
files:
/var/www/html/index.html:
content: !Join
- |+
- - >-
<http>
<h1>This is a AR3 Test Website</h1>
</http>
mode: '000644'
owner: root
group: root
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
Properties:
InstanceType: !Ref InstanceType
ImageId: !FindInMap [AWSRegionArch2AMI, !Ref 'AWS::Region', HVM64]
KeyName: !Ref KeyName
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
NetworkInterfaces:
- GroupSet:
- !ImportValue
'Fn::Sub': '${NetworkStackName}-WebSecurityGroup'
AssociatePublicIpAddress: 'true'
DeviceIndex: '0'
DeleteOnTermination: 'true'
SubnetId: !ImportValue
'Fn::Sub': '${NetworkStackName}-PubSubnetID1'
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- |
yum update -y aws-cfn-bootstrap
- |
# Install the files and packages from the metadata
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource AR3Webserver '
- ' --configsets All '
- ' --region '
- !Ref 'AWS::Region'
- |+
- |
# Signal the status from cfn-init
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource AR3Webserver '
- ' --region '
- !Ref 'AWS::Region'
- |+
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Outputs:
URL:
Value: !Join
- ''
- - 'http://'
- !GetAtt
- AR3Webserver
- PublicDnsName
Description: AR3 Website URL
现在部分正常工作
AWSTemplateFormatVersion: 2010-09-09
Description: This template will create a web instance by refering an existing VPC created by another template.
Parameters:
NetworkStackName:
Description: >-
Name of an active CloudFormation stack that contains the networking
resources, such as the subnet and security group, that will be used in
this stack.
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
Default: AR3Resource
InstanceType:
Description: 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
- hi1.4xlarge
- hs1.8xlarge
- cr1.8xlarge
- cc2.8xlarge
- cg1.4xlarge
ConstraintDescription: Must be a valid Instance type.
KeyName:
Description: Existing KeyPair name
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Must be an existing KeyPair from the region where instance is being created.
# IPCidrRange:
# 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: HVM64
t2.nano:
Arch: HVM64
t2.micro:
Arch: HVM64
t2.small:
Arch: HVM64
t2.medium:
Arch: HVM64
t2.large:
Arch: HVM64
m1.small:
Arch: HVM64
m1.medium:
Arch: HVM64
m1.large:
Arch: HVM64
m1.xlarge:
Arch: HVM64
m2.xlarge:
Arch: HVM64
m2.2xlarge:
Arch: HVM64
m2.4xlarge:
Arch: HVM64
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: HVM64
c1.xlarge:
Arch: HVM64
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
hi1.4xlarge:
Arch: HVM64
hs1.8xlarge:
Arch: HVM64
cr1.8xlarge:
Arch: HVM64
cc2.8xlarge:
Arch: HVM64
AWSInstanceType2NATArch:
t1.micro:
Arch: NATHVM64
t2.nano:
Arch: NATHVM64
t2.micro:
Arch: NATHVM64
t2.small:
Arch: NATHVM64
t2.medium:
Arch: NATHVM64
t2.large:
Arch: NATHVM64
m1.small:
Arch: NATHVM64
m1.medium:
Arch: NATHVM64
m1.large:
Arch: NATHVM64
m1.xlarge:
Arch: NATHVM64
m2.xlarge:
Arch: NATHVM64
m2.2xlarge:
Arch: NATHVM64
m2.4xlarge:
Arch: NATHVM64
m3.medium:
Arch: NATHVM64
m3.large:
Arch: NATHVM64
m3.xlarge:
Arch: NATHVM64
m3.2xlarge:
Arch: NATHVM64
m4.large:
Arch: NATHVM64
m4.xlarge:
Arch: NATHVM64
m4.2xlarge:
Arch: NATHVM64
m4.4xlarge:
Arch: NATHVM64
m4.10xlarge:
Arch: NATHVM64
c1.medium:
Arch: NATHVM64
c1.xlarge:
Arch: NATHVM64
c3.large:
Arch: NATHVM64
c3.xlarge:
Arch: NATHVM64
c3.2xlarge:
Arch: NATHVM64
c3.4xlarge:
Arch: NATHVM64
c3.8xlarge:
Arch: NATHVM64
c4.large:
Arch: NATHVM64
c4.xlarge:
Arch: NATHVM64
c4.2xlarge:
Arch: NATHVM64
c4.4xlarge:
Arch: NATHVM64
c4.8xlarge:
Arch: NATHVM64
g2.2xlarge:
Arch: NATHVMG2
g2.8xlarge:
Arch: NATHVMG2
r3.large:
Arch: NATHVM64
r3.xlarge:
Arch: NATHVM64
r3.2xlarge:
Arch: NATHVM64
r3.4xlarge:
Arch: NATHVM64
r3.8xlarge:
Arch: NATHVM64
i2.xlarge:
Arch: NATHVM64
i2.2xlarge:
Arch: NATHVM64
i2.4xlarge:
Arch: NATHVM64
i2.8xlarge:
Arch: NATHVM64
d2.xlarge:
Arch: NATHVM64
d2.2xlarge:
Arch: NATHVM64
d2.4xlarge:
Arch: NATHVM64
d2.8xlarge:
Arch: NATHVM64
hi1.4xlarge:
Arch: NATHVM64
hs1.8xlarge:
Arch: NATHVM64
cr1.8xlarge:
Arch: NATHVM64
cc2.8xlarge:
Arch: NATHVM64
AWSRegionArch2AMI:
us-east-1:
PV64: ami-8ff710e2
HVM64: ami-f5f41398
HVMG2: ami-4afd1d27
us-west-2:
PV64: ami-eff1028f
HVM64: ami-d0f506b0
HVMG2: ami-ee897b8e
us-west-1:
PV64: ami-ac85fbcc
HVM64: ami-6e84fa0e
HVMG2: ami-69106909
eu-west-1:
PV64: ami-23ab2250
HVM64: ami-b0ac25c3
HVMG2: ami-936de5e0
eu-central-1:
PV64: ami-27c12348
HVM64: ami-d3c022bc
HVMG2: ami-8e7092e1
ap-northeast-1:
PV64: ami-26160d48
HVM64: ami-29160d47
HVMG2: ami-91809aff
ap-northeast-2:
PV64: NOT_SUPPORTED
HVM64: ami-cf32faa1
HVMG2: NOT_SUPPORTED
ap-southeast-1:
PV64: ami-f3dd0a90
HVM64: ami-1ddc0b7e
HVMG2: ami-3c30e75f
ap-southeast-2:
PV64: ami-8f94b9ec
HVM64: ami-0c95b86f
HVMG2: ami-543d1137
sa-east-1:
PV64: ami-e188018d
HVM64: ami-fb890097
HVMG2: NOT_SUPPORTED
cn-north-1:
PV64: ami-77a46e1a
HVM64: ami-05a66c68
HVMG2: NOT_SUPPORTED
Resources:
AR3Webserver:
Type: AWS::EC2::Instance
Metadata:
'AWS::CloudFormation::Init':
config:
Packages:
yum:
httpd: []
files:
/var/www/html/index.html:
content: |
<html>
<body>
<h1>AR3 Web </h1>
<h2>This is a test web page!!</h2>
</body
</html>
mode: '000644'
owner: root
group: root
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
trigger=post.update
path=Resources.AR3Webserver.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource AR3Webserver --region ${AWS::Region}
runas=root
mode: '000400'
owner: root
group: root
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Properties:
InstanceType: !Ref InstanceType
ImageId: !FindInMap [AWSRegionArch2AMI, !Ref 'AWS::Region', HVM64]
KeyName: !Ref KeyName
Tags:
- Key: Application
Value: !Ref 'AWS::StackId'
NetworkInterfaces:
- GroupSet:
- !ImportValue
'Fn::Sub': '${NetworkStackName}-WebSecurityGroup'
AssociatePublicIpAddress: 'true'
DeviceIndex: '0'
DeleteOnTermination: 'true'
SubnetId: !ImportValue
'Fn::Sub': '${NetworkStackName}-PubSubnetID1'
UserData:
'Fn::Base64': !Sub |
#!/bin/bash -ex
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource AR3Webserver --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource AR3Webserver --region ${AWS::Region}
Outputs:
URL:
Value: !Join
- ''
- - 'http://'
- !GetAtt
- AR3Webserver
- PublicDnsName
Description: AR3 Website URL
我遇到的错误是
+ /opt/aws/bin/cfn-init -v --stack AR3Web --resource AR3Webserver --region us-east-1
+ Error occurred during build: Could not enable service httpd (return code 1)
+ Feb 16 12:09:53 cloud-init[2775]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
+ Feb 16 12:09:53 cloud-init[2775]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
+ Feb 16 12:09:53 cloud-init[2775]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
+ Cloud-init v. 0.7.6 finished at Sat, 16 Feb 2019 12:09:53 +0000. Datasource DataSourceEc2. Up 34.94 seconds
答案 0 :(得分:1)
首先,我要提到的模板是 WAY 版本。它使用的是2016年以来的AMI,所有这些映射都可以替换为基于Query for the latest Amazon Linux AMI IDs using AWS Systems Manager Parameter Store | AWS Compute Blog的简单参数。
无论如何,您收到的错误表明EC2实例上运行的启动脚本未能表明成功。
EC2实例上的/var/log/cloud-init-output.log
文件说:
Error occurred during build: Could not enable service httpd (return code 1)
因此,这些配置行似乎有问题:
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
答案 1 :(得分:0)
我看到了一些问题。
直接的问题是AWS::CloudFormation::Init
节中的错字,其中Packages
的首字母为P,而docs则应将packages
都排在下面案件。这将导致未安装httpd软件包,进而导致您在services
部分尝试启动httpd时看到错误消息。
这随后导致UserData
脚本出错,因此cfn-signal
没有发送,并且看到错误消息:
在指定的持续时间内未能接收到1个资源信号
此外,(我的意思是在模板的第二版中),还有其他错别字,其中mode
,owner
和group
的缩进均不正确(也为一个级别)深)。这将导致所有该配置被误解为文件内容的一部分。
如果您解决了所有问题,那就没问题了。