我编写了以下 CloudFormation 模板:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "company - LaunchConfiguration and AutoScalingGroup Template",
"Parameters" : {
"Vpc" : {
"Type" : "AWS::EC2::VPC::Id"
},
"SecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup::Id",
"Description": "choose sg"
},
"Environment" : {
"Type" : "String",
"Description" : "Choose environment",
"AllowedValues" : [ "dev", "stg", "prd" ],
"Default" : "stg"
},
"VPCtoPeerto" : {
"Type" : "String",
"Description" : "Choose VPC to peer to",
"AllowedValues" : [ "xsiteprd", "boostprd", "booststg", "xsitestg"],
"Default" : "boostprd"
},
"ParentStackName": {
"Description": "Name of an active CloudFormation stack that contains the networking resources, such as the subnet and security group, that will be used in this stack.",
"Type": "String",
"MinLength" : 1,
"MaxLength" : 255,
"AllowedPattern" : "^[a-zA-Z][-a-zA-Z0-9]*$",
"Default" : "test"
}
},
"Mappings" : {
"AMIMap" : {
"us-east-1" : { "amiid" : "ami-759bc50a" },
"us-east-2" : { "amiid" : "ami-5e8bb23b" },
"us-west-1" : { "amiid" : "ami-4aa04129" },
"us-west-2" : { "amiid" : "ami-ba602bc2" },
"eu-west-1" : { "amiid" : "ami-4aa04129" },
"eu-central-1" : { "amiid" : "ami-de8fb135" },
"eu-west-2" : { "amiid" : "ami-6b3fd60c" },
"ap-northeast-1" : { "amiid" : "ami-98cfaa75" },
"ap-southeast-1" : { "amiid" : "ami-1c106cf6" },
"sa-east-1" : { "amiid" : "ami-8eecc9e2" }
},
"KeyName" : {
"us-east-1" : { "key" : "va-ni" },
"eu-west-2" : { "key" : "ni-uk" },
"ap-southeast-1" : { "key" : "ni-au" },
"sa-east-1" : { "key" : "ni-sp" },
"eu-central-1" : { "key" : "ni-fr" }
},
"AsgSize" : {
"dev" : { "MinSize" : 1 , "MaxSize" : 2 },
"stg" : { "MinSize" : 1 , "MaxSize" : 2 },
"prd" : { "MinSize" : 1 , "MaxSize" : 2 }
},
"PeeringVpcId" : {
"xsiteprd" : { "VAVpcId" : "vpc-71e55e15" },
"boostprd" : { "VAVpcId" : "vpc-1f0e197d" },
"booststg" : { "VAVpcId" : "vpc-de5b15b9" },
"xsitestg" : { "VAVpcId" : "vpc-25f59042" }
}
},
"Resources" : {
"AutoScalingGroup" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"VPCZoneIdentifier" : [ { "Fn::ImportValue" : { "Fn::Sub" : "${ParentStackName}-SubnetOne" } }, { "Fn::ImportValue" : { "Fn::Sub" : "${ParentStackName}-SubnetTwo" } }, { "Fn::ImportValue" : { "Fn::Sub" : "${ParentStackName}-SubnetThree" } } ],
"LaunchConfigurationName" : { "Ref" : "LaunchConfiguration" },
"MinSize" : { "Fn::FindInMap" : [ "AsgSize", { "Ref" : "Environment" }, "MinSize" ] },
"MaxSize" : { "Fn::FindInMap" : [ "AsgSize", { "Ref" : "Environment" }, "MaxSize" ] },
"TerminationPolicies" : [ "NewestInstance" ],
"MetricsCollection": [
{
"Granularity": "1Minute",
"Metrics": []
}
],
"Tags" : [
{ "Key" : "Name", "Value" : { "Fn::Join" : [ "-", [ { "Ref" : "AWS::StackName" }, { "Fn::ImportValue" : { "Fn::Sub" : "${ParentStackName}-Environment" } } ] ] }, "PropagateAtLaunch" : "true" }
]
}
},
"LaunchConfiguration" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Properties" : {
"ImageId" : { "Fn::FindInMap" : [ "AMIMap", { "Ref" : "AWS::Region" }, "amiid" ]},
"InstanceType" : "t2.micro",
"KeyName" : { "Fn::FindInMap" : [ "KeyName", { "Ref" : "AWS::Region" }, "key" ]},
"AssociatePublicIpAddress" : "true",
"SecurityGroups" : [ { "Fn::ImportValue" : { "Fn::Sub" : "${ParentStackName}-SG" } } ],
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"/usr/bin/cloud-init-per once apt-update apt-get update\n",
"/usr/bin/cloud-init-per once bootstrap-deps1 apt-get install python-pip jq python-setuptools heat-cfntools -y\n",
"/usr/bin/cloud-init-per once bootstrap-deps2 pip install awscli botocore boto \n",
"export LC_ALL=C\n",
"export vpcid=",{ "Ref" : "Vpc" }, "\n",
"export vpctopeer=",{ "Fn::FindInMap" : [ "PeeringVpcId", { "Ref" : "VPCtoPeerto" }, "VAVpcId" ] }, "\n",
"mkdir aws-cfn-bootstrap-latest\n",
"curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1\n",
"easy_install aws-cfn-bootstrap-latest\n",
"/usr/local/bin/cfn-init --stack ", { "Ref" : "AWS::StackName" }, " --resource ", { "Ref" : "AutoScalingGroup" } , " --region ", { "Ref": "AWS::Region" }, "\n",
]]}}
}
},
"ServerProfile" : {
"Type" : "AWS::IAM::InstanceProfile",
"Properties" : {
"Path" : "/",
"Roles" : ["BackendServer"]
}
}
}
}
请忽略看似缩进的问题,这里的代码框弄乱了它,但缩进是正确的。
我希望在自动扩展组中启动的实例在环境变量中填充对等 vpc 的 vpcid 和 vpcid。
当我检查时/var/log/cloud-init.log
,我看到了这一点:
+ export LC_ALL=C
+ LC_ALL=C
+ export vpcid=vpc-aff8a6c4
+ vpcid=vpc-aff8a6c4
+ export vpctopeer=vpc-1f0e197d
+ vpctopeer=vpc-1f0e197d
但是变量没有正确导出,因为当我通过 ssh 进入服务器时这些变量没有设置。
我知道我可以呼应这些变量,/etc/environment
但我觉得有更好的方法可以做到这一点。
实现这一目标的最佳方法是什么?
答案1
当您在“用户数据”脚本中导出变量时,这些变量将在脚本完成时被“销毁”。这与您在 shell 中导出变量然后注销时的情况相同。
我相信将变量存储在 /etc/environment 中是让这些变量在新虚拟机上可用的最简单方法。
答案2
您可以使用用户数据脚本将数据回显到磁盘上的 .sh 文件,然后创建一个 cron 来执行该 cron - 即使您决定不长期使用它,我也建议将其作为测试步骤来确认用户数据正在导出正确的数据(即,您可以 cat .sh 文件以确认 .sh 文件中存在正确的值)