使用 cloudformation 脚本将弹性 IP 分配给由自动缩放组创建的实例
Assign an Elastic IP to an Instance which was created by auto-scaling group using cloudformation script
我有一个 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 地址。
我有一个 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 地址。