我正在尝试设置云形成模板来部署ubuntu 16.04。模板部署很好,我可以SSH到服务器。但我定义的额外的包,如apache和php模块将无法安装。我似乎无法弄清楚为什么会这样?我在线查看了几个代码片段,但似乎无法使其正常工作。有人可以告诉我这里我做错了什么吗?
守则:
{
"Description" : "Staging single instance",
"Outputs": {
"InstanceID": {
"Description": "The WWW instance id",
"Value": { "Ref": "StageInstance" }
}
},
"Parameters": {
"AMI": {
"Description": "The Amazon Ubuntu AMI",
"Type": "String",
"Default": "ami-996372fd"
},
"EBSVolumeSize": {
"Description": "The size of the EBS volume for the transcoder",
"Type": "String",
"Default": "20"
},
"InstanceType": {
"AllowedValues": [
"t2.micro",
"t2.small",
"t2.medium",
"t2.large",
"c4.large",
"c4.xlarge",
"c4.2xlarge",
"c4.4xlarge",
"c4.8xlarge"
],
"ConstraintDescription": "must be a valid EC2 instance type",
"Default": "t2.micro",
"Description": "EC2 instance type",
"Type": "String"
},
"KeyName": {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to NAT instances.",
"Type": "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription" : "Must be the name of an existing EC2 KeyPair." },
"Subnet": {
"Description": "The subnet to place the instance in...",
"Type": "AWS::EC2::Subnet::Id",
"ConstraintDescription" : "Must be a valid Subnet."
},
"VPC": {
"Description": "The VPC to deploy to...",
"Type": "AWS::EC2::VPC::Id",
"ConstraintDescription" : "Must be a valid VPC."
}
},
"Resources": {
"StageSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": {"Ref": "VPC"},
"GroupDescription": "Allow SSH, HTTP, and HTTPS access",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": "443",
"ToPort": "443",
"CidrIp": "0.0.0.0/0"
}
]
}
},
"StageInstance": {
"Type" : "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"packages": {
"apt": {
"awscli": [ ],
"apache2": [ ],
"php7.0": [ ],
"libapache2-mod-php7.0": [ ],
"php7.0-cli": [ ],
"php7.0-common": [ ],
"php7.0-mbstring": [ ],
"php7.0-gd": [ ],
"php7.0-intl": [ ],
"php7.0-xml": [ ],
"php7.0-mysql": [ ],
"php7.0-mcrypt": [ ],
"php7.0-zip": [ ]
}
}
}
}
},
"Properties": {
"BlockDeviceMappings" : [{
"DeviceName" : "/dev/sda1",
"Ebs" : {"VolumeSize" : {"Ref" : "EBSVolumeSize"}}
}],
"SecurityGroupIds": [{"Ref": "StageSecurityGroup"}],
"KeyName": {"Ref": "KeyName" },
"ImageId": {"Ref": "AMI"},
"SubnetId": {"Ref": "Subnet"},
"InstanceType": {"Ref": "InstanceType"},
"Tags": [
{"Key" : "Name", "Value" : "Ldn_Svr_Stage_Web"}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -x\n",
"apt-get update\n",
"apt-get install -y python-pip\n",
"pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
"cfn-init -v –resource StageInstance",
" –stack ",
{
"Ref": "AWS::StackName"
},
" –region ",
{
"Ref": "AWS::Region"
},
"\n",
"cfn-signal -e $? –resource StageInstance",
" –stack ",
{
"Ref": "AWS::StackName"
},
" –region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
}
}
}
}
答案 0 :(得分:4)
以下是一个简单示例的工作代码....
{
"Description": "stack descripton.....",
"Parameters": {
"AMI": {
"Default": "ami-996372fd",
"Description": "The Amazon Ubuntu AMI",
"Type": "String"
},
"EBSVolumeSize": {
"Default": "20",
"Description": "The size of the EBS volume",
"Type": "String"
},
"InstanceName": {
"Default": "My instance name",
"Description": "EC2 Instance Name",
"Type": "String"
},
"InstanceType": {
"AllowedValues": [
"t2.micro",
"t2.small",
"t2.medium",
"t2.large",
"c4.large",
"c4.xlarge",
"c4.2xlarge",
"c4.4xlarge",
"c4.8xlarge"
],
"ConstraintDescription": "must be a valid EC2 instance type",
"Default": "t2.micro",
"Description": "EC2 instance type",
"Type": "String"
},
"KeyName": {
"ConstraintDescription": "Must be the name of an existing EC2 KeyPair.",
"Description": "Name of an existing EC2 KeyPair to enable SSH access to NAT instances.",
"Type": "AWS::EC2::KeyPair::KeyName"
},
"Subnet": {
"ConstraintDescription": "Must be a valid Subnet.",
"Description": "The subnet to place the instance in...",
"Type": "AWS::EC2::Subnet::Id"
},
"VPC": {
"ConstraintDescription": "Must be a valid VPC.",
"Description": "The VPC to deploy to...",
"Type": "AWS::EC2::VPC::Id"
}
},
"Resources": {
"Instance": {
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"packages": {
"apt": {
"awscli": [],
"ruby": [],
"unzip": []
}
},
"files": {
"/tmp/sample.conf": {
"content": {
"Fn::Join": [
"",
[
"write a sample file to OS and give permissions.... \n"
]
]
},
"mode": "000644",
"owner": "root",
"group": "root"
}
},
"commands": {
"01update_pkgs": {
"command": "apt-get update && apt-get upgrade -y"
},
"02next_command": {
"command": "echo the numbering here has to be in order... 01, 02, 03 and so on... last item is not comma'd"
}
}
}
}
},
"Properties": {
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"VolumeSize": {
"Ref": "EBSVolumeSize"
}
}
}
],
"ImageId": {
"Ref": "AMI"
},
"InstanceType": {
"Ref": "InstanceType"
},
"KeyName": {
"Ref": "KeyName"
},
"SecurityGroupIds": [
{
"Ref": "SecurityGroup"
}
],
"SubnetId": {
"Ref": "Subnet"
},
"Tags": [
{
"Key": "Name",
"Value": {
"Ref": "InstanceName"
}
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -xe\n",
"apt-get install -y python-setuptools\n",
"mkdir -p /opt/aws/bin\n",
"wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
"easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-latest.tar.gz\n",
" apt-get update\n",
"/opt/aws/bin/cfn-init --region ",
{
"Ref": "AWS::Region"
},
" -s ",
{
"Ref": "AWS::StackName"
},
" -r Instance\n"
]
]
}
}
},
"Type": "AWS::EC2::Instance"
},
"EIPAddress": {
"Type": "AWS::EC2::EIP"
},
"IPAssoc": {
"Type": "AWS::EC2::EIPAssociation",
"Properties": {
"InstanceId": {
"Ref": "Instance"
},
"EIP": {
"Ref": "EIPAddress"
}
}
},
"SecurityGroup": {
"Properties": {
"GroupDescription": "Allow SSH, HTTP, and HTTPS access",
"SecurityGroupIngress": [
{
"CidrIp": "0.0.0.0/0",
"FromPort": "22",
"IpProtocol": "tcp",
"ToPort": "22"
},
{
"CidrIp": "0.0.0.0/0",
"FromPort": "443",
"IpProtocol": "tcp",
"ToPort": "443"
}
],
"VpcId": {
"Ref": "VPC"
}
},
"Type": "AWS::EC2::SecurityGroup"
}
}
}
答案 1 :(得分:0)
创建一个apache.json文件:
{
"Resources" :
{
"WebServer":{
"Type": "AWS::EC2::Instance",
"Properties":{
"ImageId" : "ami-9a7724c8",
"KeyName" : "demo",
"InstanceType":"t1.micro",
"SecurityGroups" : [ "demo" ],
"UserData" : {"Fn::Base64" : {"Fn::Join" : ["", [
"#!/bin/bash\n",
"# Launching Instance",
"apt-get -y install apache2 \n",
"apt-get -y install php php5-mysql \n"
] ] } }
}
}
},
"Outputs":{
"WebsiteURL" : {
"Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [
"WebServer", "PublicDnsName" ]}]] },
"Description" : "URL for newly created apache server "
}
}
}
然后使用此json文件创建云形成堆栈。
请在保存之前验证json文件。
希望它会有所帮助!