将弹性IP分配给由自动扩展组使用cloudformation脚本创建的实例

时间:2017-06-17 07:05:55

标签: amazon-web-services amazon-ec2 configuration amazon-cloudformation launch

我有一个cloudformation脚本,可以创建一个EC2,RDS堆栈,带有自动调节和云观察警报以及DNS记录。

这是脚本

LaunchConfiguration:
Type: "AWS::AutoScaling::LaunchConfiguration"
Metadata:
  "AWS::CloudFormation::Init":
    config:
      packages:
        yum:
          "php": []
          "php-mysql": []
          "mysql": []
          "httpd": []
      sources: {"/var/www/html": "https://wordpress.org/latest.tar.gz"}
      files:
        "/root/config.sh":
          content:
            "Fn::Join":
              - ""
              - [
                  "#!/bin/bash -ex\n",
                  "cp wp-config-sample.php wp-config.php\n",
                  "sed -i \"s/'database_name_here'/'devwordpress'/g\" wp-config.php\n",
                  "sed -i \"s/'username_here'/'devuser'/g\" wp-config.php\n",
                  "sed -i \"s/'password_here'/'devpassword'/g\" wp-config.php\n",
                  "sed -i \"s/'localhost'/'", {"Fn::GetAtt": ["Database", "Endpoint.Address"]}, "'/g\" wp-config.php\n",
                  "echo \"define( 'DISALLOW_FILE_MODS', true ); \" >> wp-config.php \n",
                  "echo \"define( 'WP_AUTO_UPDATE_CORE', false ); \" >> wp-config.php \n",
                  "chmod -R 777 wp-content/ \n",
                  "curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \n",
                  "php wp-cli.phar core install --url=\"", "wordpress.devops-tech.in", "\" --title=\"", {"Ref": "BlogTitle"}, "\" --admin_user=\"", {"Ref": "BlogAdminUsername"}, "\" --admin_password=\"", {"Ref": "BlogAdminPassword"}, "\" --admin_email=\"", {"Ref": "BlogAdminEMail"}, "\" \n",
                  "php wp-cli.phar plugin install --activate amazon-web-services \n",
                  "php wp-cli.phar plugin install --activate amazon-s3-and-cloudfront \n",
                  "CHARCOUNT=`printf \"", "wordpress", "\" | wc -c` \n",
                  "php wp-cli.phar db query \"DELETE FROM wp_options WHERE option_name = 'tantan_devwordpress_s3'; INSERT INTO wp_options (option_name, option_value, autoload) VALUES('tantan_prodwordpress_s3', 'a:15:{s:17:\\\"post_meta_version\\\";i:1;s:6:\\\"bucket\\\";s:", "$CHARCOUNT", ":\\\"", "wordpress","\\\";s:6:\\\"region\\\";s:0:\\\"\\\";s:6:\\\"domain\\\";s:9:\\\"subdomain\\\";s:7:\\\"expires\\\";s:1:\\\"0\\\";s:10:\\\"cloudfront\\\";s:0:\\\"\\\";s:13:\\\"object-prefix\\\";s:19:\\\"wp-content/uploads/\\\";s:10:\\\"copy-to-s3\\\";s:1:\\\"1\\\";s:13:\\\"serve-from-s3\\\";s:1:\\\"1\\\";s:17:\\\"remove-local-file\\\";s:1:\\\"0\\\";s:3:\\\"ssl\\\";s:7:\\\"request\\\";s:12:\\\"hidpi-images\\\";s:1:\\\"0\\\";s:17:\\\"object-versioning\\\";s:1:\\\"0\\\";s:21:\\\"use-yearmonth-folders\\\";s:1:\\\"1\\\";s:20:\\\"enable-object-prefix\\\";s:1:\\\"1\\\";}', 'yes');\" \n"
                ]
          mode: "000500"
          owner: "root"
          group: "root"

      commands:
        01_mv:
          command: "mv * ../"
          cwd: "/var/www/html/wordpress"
        02_config:
          command: "/root/config.sh"
          cwd: "/var/www/html"

      services:
        sysvinit:
          httpd:
            enabled: "true"
            ensureRunning: "true"

Properties:
  ImageId: {"Fn::FindInMap": ["EC2RegionMap", {"Ref": "AWS::Region"}, "AmazonLinuxAMIHVMEBSBacked64bit"]}
  InstanceType: {"Ref": "WebServerInstanceType"}
  SecurityGroups: [{"Ref": "WebServerSecurityGroup"}]
  KeyName: {"Ref": "WebServerKeyName"}
  AssociatePublicIpAddress: "true"
  UserData:
    "Fn::Base64":
      "Fn::Join":
        - ""
        - [
            "#!/bin/bash -ex\n",
            "yum update -y aws-cfn-bootstrap\n",
            "/opt/aws/bin/cfn-init -v --stack ", {"Ref": "AWS::StackName"}, " --resource LaunchConfiguration --region ", {"Ref": "AWS::Region"}, "\n",
            "/opt/aws/bin/cfn-signal -e $? --stack ", {"Ref": "AWS::StackName"}, " --resource AutoScalingGroup --region ", {"Ref": "AWS::Region"}, "\n"
          ]
  InstanceMonitoring: "true"

AutoScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
  LoadBalancerNames: [{"Ref": "LoadBalancer"}]
  LaunchConfigurationName: {"Ref": "LaunchConfiguration"}
  MinSize: "1"
  MaxSize: "2"
  DesiredCapacity: "1"
  Cooldown: "300"
  HealthCheckGracePeriod: "300"
  HealthCheckType: "ELB"
  VPCZoneIdentifier: [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
  Tags:
    - PropagateAtLaunch: "true"
      Value: "dev-instance-wordpress"
      Key: "Name"

现在我想将弹性ip附加到将通过自动缩放创建的实例。 如何在创建堆栈时附加弹性ip?

1 个答案:

答案 0 :(得分:2)

你的问题有点打破了自动缩放组的设计。 ASG用于进出的动态实例,而弹性IP用于具有更长生命周期的事物。

话虽这么说,您应该编辑模板并添加真实的关联公共IP地址。

请参阅http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-launchconfig.html#cfn-as-launchconfig-blockdevicemappings