2017-08-10 20 views
0

ubuntu 16.04をデプロイするためのクラウドフォーメーションテンプレートをセットアップしようとしています。テンプレートはうまく展開され、私はSSHでサーバーにアクセスできますが、ApacheとPHPモジュールのように私が定義した追加のパッケージはインストールされません。私はそれがなぜあるのか分からないようですか?私はオンラインでいくつかのコードスニペットを見ましたが、それが適切に動作するように見えることはできません。ここで間違っていることを教えてくれる人もいますか?ubuntu 16.04クラウドフォーメーションテンプレート

コード:

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

答えて

2

は、単純な例の作業コードが

{ 
    "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" 
     } 
    } 
} 
....であります
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ファイルを有効に確認してください。

希望すると助かります!ここ