【问题标题】:ubuntu 16.04 cloud formation templateubuntu 16.04 云形成模板
【发布时间】:2017-09-06 23:54:00
【问题描述】:

我正在尝试设置云形成模板来部署 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"
         ]
       ]
     }
   }
 }
  }
 }
}

【问题讨论】:

    标签: templates ubuntu-16.04 amazon-cloudformation


    【解决方案1】:

    这是一个简单示例的工作代码......

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

    【讨论】:

    • 该解决方案将获取软件包以安装并运行命令,但在重新启动后不会继续。缺少 cfn-hup 的配置以及对 cfn-signal 的调用以指示脚本有效。
    【解决方案2】:

    创建一个 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 文件。

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 2019-08-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 2016-10-27
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多