简体   繁体   English

AWS Cloudformation - 使用 cfn-init 安装软件包

[英]AWS Cloudformation - Installing packages with cfn-init

I have created an EC2 instance via cloudformation and I am trying to get it to install postgres on the instance directly via cloudformation.我已经通过 cloudformation 创建了一个 EC2 实例,我试图让它直接通过 cloudformation 在实例上安装 postgres。 However, when I SSH into my instance and try to run psql via the command line I keep getting:但是,当我通过 SSH 连接到我的实例并尝试通过命令行运行psql ,我不断收到:

bash: psql: command not found

I have tried doing it manually, installing postgres with the below command and it works fine.我试过手动执行,使用以下命令安装 postgres,它工作正常。

sudo yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs

Could it be that it is because I'm just updating the stack and thus the ec2 instance rather than creating a new one?可能是因为我只是更新堆栈,从而更新 ec2 实例而不是创建一个新实例?

Below is a snippet from the cloudformation template.下面是来自 cloudformation 模板的片段。 Everything works when I update the template but it seems that postgres still isn't installed...当我更新模板时一切正常,但似乎仍未安装 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" ] ]

If anyone is encountering the same problem, the solution was indeed that you need to delete the instance and recreate from scratch. 如果有人遇到相同的问题,则解决方案的确是您需要删除实例并从头开始重新创建。 Just updating the stack won't work. 只是更新堆栈是行不通的。

A bit late here (I found this searching for another issue), but you can re-run your CF Launch Config with this piece from your snippet: 到这里有点晚了(我发现这是在寻找另一个问题),但是您可以使用片段中的这段代码重新运行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" ] ]

the /opt/aws/bin/cfn-init command is what's running the metadata config from the launch config you've specified, which is where your packages are defined. /opt/aws/bin/cfn-init命令是从指定的启动配置(定义包的位置)中运行元数据配置的内容。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html

The reason deleting the instance and recreating it works is that it re-runs the UserData piece from the EC2 section above. 删除实例并重新创建实例的原因是,它重新运行了上面EC2部分中的UserData

This is related to you calling cfn-init with --configsets value that you do not have defined.这与您使用尚未定义的--configsets值调用cfn-init有关。 You would need to add the configSets section below to your metadata section:您需要将下面的configSets部分添加到您的元数据部分:

Metadata:
  AWS::CloudFormation::Init:
    configSets:
      Install:
        - "config"
    config: 
      packages: 
        yum:
          postgresql: []
          postgresql-server: []
          postgresql-devel: []
          postgresql-contrib: []
          postgresql-docs: []

Otherwise take out --configset from your cfn-init original call.否则从您的cfn-init原始调用中取出--configset

References:参考:

cfn-init cfn-init

AWS::CloudFormation::Init AWS::CloudFormation::Init

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM