[英]Difference between UserData and cfn-helper with CloudFormation AWS
[英]AWS Cloudformation - Installing packages with cfn-init
我已經通過 cloudformation 創建了一個 EC2 實例,我試圖讓它直接通過 cloudformation 在實例上安裝 postgres。 但是,當我通過 SSH 連接到我的實例並嘗試通過命令行運行psql
,我不斷收到:
bash: psql: command not found
我試過手動執行,使用以下命令安裝 postgres,它工作正常。
sudo yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs
可能是因為我只是更新堆棧,從而更新 ec2 實例而不是創建一個新實例?
下面是來自 cloudformation 模板的片段。 當我更新模板時一切正常,但似乎仍未安裝 postgres ......
DbWrapper:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
packages:
yum:
postgresql: []
postgresql-server: []
postgresql-devel: []
postgresql-contrib: []
postgresql-docs: []
Properties:
ImageId: ami-f976839e #AMI aws linux 2
InstanceType: t2.micro
AvailabilityZone: eu-west-2a
SecurityGroupIds:
- !Ref Ec2SecurityGroup
SubnetId: !Ref SubnetA
KeyName: !Ref KeyPairName
UserData:
Fn::Base64:
!Join [ "", [
"#!/bin/bash -xe\n",
"sudo yum update\n",
"sudo yum install -y aws-cfn-bootstrap\n", #download aws helper scripts
"sudo /opt/aws/bin/cfn-init -v ", #use cfn-init to install packages in cloudformation init
!Sub "--stack ${AWS::StackName} ",
"--resource DbWrapper ",
"--configsets Install ",
!Sub "--region ${AWS::Region} ",
"\n" ] ]
如果有人遇到相同的問題,則解決方案的確是您需要刪除實例並從頭開始重新創建。 只是更新堆棧是行不通的。
到這里有點晚了(我發現這是在尋找另一個問題),但是您可以使用片段中的這段代碼重新運行CF Launch Config:
UserData:
Fn::Base64:
!Join [ "", [
"#!/bin/bash -xe\n",
"sudo yum update\n",
"sudo yum install -y aws-cfn-bootstrap\n", #download aws helper scripts
"sudo /opt/aws/bin/cfn-init -v ", #use cfn-init to install packages in cloudformation init
!Sub "--stack ${AWS::StackName} ",
"--resource DbWrapper ",
"--configsets Install ",
!Sub "--region ${AWS::Region} ",
"\n" ] ]
/opt/aws/bin/cfn-init
命令是從指定的啟動配置(定義包的位置)中運行元數據配置的內容。
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html
刪除實例並重新創建實例的原因是,它重新運行了上面EC2部分中的UserData
。
這與您使用尚未定義的--configsets
值調用cfn-init
有關。 您需要將下面的configSets部分添加到您的元數據部分:
Metadata:
AWS::CloudFormation::Init:
configSets:
Install:
- "config"
config:
packages:
yum:
postgresql: []
postgresql-server: []
postgresql-devel: []
postgresql-contrib: []
postgresql-docs: []
否則從您的cfn-init
原始調用中取出--configset
。
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.