amazon-web-services - 将弹性IP分配给自动伸缩组使用cloudformation脚本创建的实例

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

我有一个 cloudformation 脚本,它创建了一个 EC2、RDS 堆栈,具有自动缩放功能、cloudwatch 警报以及 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?

最佳答案

您的问题有点破坏了自动缩放组的设计。 ASG 适用于来来去去的动态实例,而弹性 IP 适用于生命周期较长的实例。

话虽这么说,您应该编辑模板并添加关联公共(public) IP 地址 true。

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

关于amazon-web-services - 将弹性IP分配给自动伸缩组使用cloudformation脚本创建的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44601781/

相关文章:

mysql - 数据节点无法连接到 MySQL Cluster

mongodb - 为 Meteor 应用程序单独托管数据库

gradle - 如何确定是否已实例化Gradle任务(配置避免API)

apache - .htaccess 中的 DocumentRoot 导致错误

amazon-web-services - 维护 Jenkins 中多个 cloudformation 堆栈之间的部署顺序

amazon-web-services - 如何使用 Terraform 部署和重新部署应用程序?

postgresql - 如何通过 Amazon EC2 将 postgresql 数据移动到 Ubuntu 上的另一个目录?

java - Spring不调用setter

node.js - 使用 Node.js 在 DynamoDB 中分页?

javascript - AWS Cognito - 从浏览器应用程序邀请用户 (adminCreateUser)