【问题标题】:AWS Cloudformation - Installing packages with cfn-initAWS Cloudformation - 使用 cfn-init 安装包
【发布时间】:2021-11-03 23:44:07
【问题描述】:

我已经通过 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 模板的 sn-p。当我更新模板时一切正常,但似乎仍未安装 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" ] ]

【问题讨论】:

    标签: bash postgresql amazon-web-services amazon-ec2 amazon-cloudformation


    【解决方案1】:

    如果有人遇到同样的问题,解决方案确实是您需要删除实例并从头开始重新创建。仅仅更新堆栈是行不通的。

    【讨论】:

      【解决方案2】:

      这里有点晚了(我发现这个正在寻找另一个问题),但是您可以使用您的 sn-p 中的此部分重新运行您的 CF 启动配置:

            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 部分。

      【讨论】:

        【解决方案3】:

        这与您使用未定义的 --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

        参考资料:

        cfn-init

        AWS::CloudFormation::Init

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-14
          • 2023-03-20
          • 2019-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-08
          相关资源
          最近更新 更多