JaviOverflow
JaviOverflow

Reputation: 1480

How can one add a yum repository in AWS::CloudFormation::Init

I'm trying to install docker in a Centos instance using cloudformation and the blocks of AWS::CloudFormation::Init

One of the installation steps is to add a certain repository to yum by running:

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

How can I enable that repository in a cloudformation template. Ideally, I would like to use a single config block if that's possible.

This is what I got so far

Resources:
  ec2:
    Type: AWS::EC2::Instance
    Metadata:
      AWS::CloudFormation::Init:
        config:
          packages:
            yum:
              yum-utils: []
              device-mapper-persistent-data: []
              lvm2: []
    Properties:
      ...

Upvotes: 1

Views: 1714

Answers (1)

Alex Harvey
Alex Harvey

Reputation: 15472

You should be able to do that by adding a commands block:

Resources:
  ec2:
    Type: AWS::EC2::Instance
    Metadata:
      AWS::CloudFormation::Init:
        configSets:
          config:
            - yum_config_manager
            - yum_packages
        yum_config_manager:
          commands:
            yum_config_manager:
              command: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        yum_packages:
          packages:
            yum:
              yum-utils: []
              device-mapper-persistent-data: []
              lvm2: []

And in your UserData you will have:

cfn-init -c config -s ${AWS::StackId} --resource ec2 --region ${AWS::Region}

Further explanation:

  • Note that the commands and packages blocks have each been wrapped in a configSet.

  • Note also that the -c config tells cfn-init which of the configSets you want to run.

After it boots up you should be able to inspect successful operation by looking in /var/log/cfn-init-cmd.log.

More info in the docs especially the sections on configSets and commands.

Upvotes: 1

Related Questions