我已经通过 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 实例而不是创建一个新实例?
下面是来自云形成模板的片段。当我更新模板时一切正常,但似乎仍然没有安装 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 -xen",
"sudo yum updaten",
"sudo yum install -y aws-cfn-bootstrapn", #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 -xen",
"sudo yum updaten",
"sudo yum install -y aws-cfn-bootstrapn", #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
部分。
这与你调用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
。
引用:
cfn-init
AWS::CloudFormation::Init