简体   繁体   中英

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. However, when I SSH into my instance and try to run psql via the command line I keep getting:

bash: psql: command not found

I have tried doing it manually, installing postgres with the below command and it works fine.

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?

Below is a snippet from the cloudformation template. Everything works when I update the template but it seems that postgres still isn't installed...

  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:

      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.

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.

This is related to you calling cfn-init with --configsets value that you do not have defined. You would need to add the configSets section below to your metadata section:

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.

References:

cfn-init

AWS::CloudFormation::Init

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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