Skip to content

Pre-build AMIs cause runners to have additional unused ebs when nothing is provided to block_device #1769

Closed

Description

@Hankier

So we are using ubuntu-focal
for our runners as we have ebs created from this part of code:

    device_name           = "/dev/sda1"
    volume_size           = "${var.root_volume_size_gb}"
    volume_type           = "gp3"
    delete_on_termination = "${var.ebs_delete_on_termination}"
  }

But this part cause images to have other ebs with default value of 30GB.

    for_each = [var.block_device_mappings]
    content {
      device_name = lookup(block_device_mappings.value, "device_name", "/dev/xvda")

      ebs {
        delete_on_termination = lookup(block_device_mappings.value, "delete_on_termination", true)
        volume_type           = lookup(block_device_mappings.value, "volume_type", "gp3")
        volume_size           = lookup(block_device_mappings.value, "volume_size", var.volume_size)
        encrypted             = lookup(block_device_mappings.value, "encrypted", true)
        iops                  = lookup(block_device_mappings.value, "iops", null)
      }
    }
}  

Screenshot 2022-02-21 at 16 00 13

Defaults are taken from:

  description = "The EC2 instance block device configuration. Takes the following keys: `device_name`, `delete_on_termination`, `volume_type`, `volume_size`, `encrypted`, `iops`"
  type        = map(string)
  default     = {}
}

variable "volume_size" {
  description = "Size of runner volume"
  type        = number
  default     = 30
}

What should we do about that? Should module accept null and not configure or ignore a empty {}?

Activity

  1. mahela-aws commented on Mar 3, 2022

    @mahela-aws

    @hunqer how did you managed to get it working with ubuntu-focal ? I have used the same packer template given here with few changes to suit our environment but runners are not getting registered with github. appreciate any help here

  2. toast-gear commented on Mar 7, 2022

    @toast-gear
    Contributor

    We haven't done anything beyond provide some non-conequential variable inputs, we've made 0 changes to the template. Are you sure your changes aren't impacting? Have you got a route out to the internet in the subnet you've depoyed the runners in?

  3. ScottGuymer commented on Mar 9, 2022

    @ScottGuymer
    Contributor

    I think this is one place that needs a bit of a re-write to make it simpler and more obvious.

    What is happening here is that the AMI you have specified has a default device mapping of /dev/sda1 so it will always get that when booted. By default the runners module modifies a device called /dev/xvda which isnt present on your AMI so this device will get added as an additional device. You now have two devices on your running instance.

    To get back to one you need to configure the following as part of the runners config

      block_device_mappings = {
        device_name = "/dev/sda1"
        volume_type = "gp2"
        volume_size = 30
      }
    

    This will have the effect of removing the config for /dev/xvda and modifying the default root device on your AMI which is /dev/sda1 (but it could be something else too)

  4. mahela-aws commented on Mar 9, 2022

    @mahela-aws

    We haven't done anything beyond provide some non-conequential variable inputs, we've made 0 changes to the template. Are you sure your changes aren't impacting? Have you got a route out to the internet in the subnet you've depoyed the runners in?

    yes they do have access to internet and also we have a working setup with amzn-linux-2 but we really wants to switch to ubuntu since there are few public actions that doesn't support amzn-linux-2.

    changes that I have made almost none, we copied over the scripts and changed the folder structure and inputs regions etc. we wanted to have our own setup for packer templates so that we can make changes as we wish. but unfortunately we are struggling a bit with ubuntu runners.

  5. toast-gear commented on Mar 9, 2022

    @toast-gear
    Contributor

    Our pipeline for building the runner is pretty basic so I can't imagine it's the packer template within this repository that is the problem for you, it's most likely a change made assuming your runner environment is valid. We build our AMI using this repository with 0 changes as we favour baking as little into the runner AMI as possible and instead opting for pipelines to use actions to setup the environment / run a tool e.g. actions/setup-python, hashicorp/packer-github-actions or projects / product teams to provide and own a container/s for their pipelines with dependencies included and use the container: workflow feature in their pipelines.

    AMIs and launch templates are fairly horrible to work with in general so we try to stay away from customising the AMI if possible and instead solve the problem in other ways.

    name: Publish runner
    
    on:
      workflow_dispatch:
        inputs:
          TestBuild:
            description: 'Is this a test build?'
            type: boolean
            default: "true"
    
    jobs:
      publish:
        runs-on: self-hosted
        strategy:
          matrix:
            include:
              - path: images/ubuntu-focal/github_agent.ubuntu.pkr.hcl
                os: ubuntu-focal
        steps:
          - name: Checkout
            uses: actions/checkout@v2
            with:
              repository: philips-labs/terraform-aws-github-runner
              ref: develop
    
          - name: Init
            uses: hashicorp/packer-github-actions@master
            with:
              command: init
              target: ${{ matrix.path }}
    
          - name: Validate
            uses: hashicorp/packer-github-actions@master
            with:
              command: validate
              target: ${{ matrix.path }}
    
          - name: Configure AWS credentials
            uses: aws-actions/configure-aws-credentials@v1
            with:
              role-to-assume: arn:aws:iam::$MY_ACCOUNT_ID:role/$MY_ROLE_NAME
              aws-region: $MY_REGION
              role-duration-seconds: 900
    
          - name: Get Versions
            id: versions
            run: |
              LATEST_RUNNER_VERSION=$(curl -s https://api.github.com/repos/actions/runner/tags | jq -r '.[0].name' | sed 's/v//')
              echo "Found latest runner version to be: $LATEST_RUNNER_VERSION"
              echo "::set-output name=runner_version::$LATEST_RUNNER_VERSION"
    
          - name: Build and Publish
            uses: hashicorp/packer-github-actions@master
            with:
              command: build
              arguments: |
                -on-error=abort
                -var security_group_id=$MY_SECURITY_GROUP
                -var region=$MY_REGION
                -var subnet_id=$MY_SUBNET
                -var instance_type=t3.medium
                -var root_volume_size_gb=50
                -var runner_version=${{ steps.versions.outputs.runner_version }}
              target: ${{ matrix.path }}
            env:
              PACKER_LOG: 1
    
          # https://github.com/hashicorp/packer-github-actions/issues/37
          - name: Tag
            run: |
              HEAD_COMMIT=$(git rev-parse HEAD)
              SHORT_HEAD_COMMIT=$(git rev-parse --short HEAD)
              AMI_ID=$(aws ec2 describe-images --owners '$MY_ACCOUNT_ID' --filters "Name=name,Values=github-runner-${{ matrix.os }}-*"  --query 'Images[*] | sort_by(@, &CreationDate)[-1].ImageId' --output text)
              if [[ "$AMI_ID" != "None" ]]; then
                echo "Tagging $AMI_ID with:"
                echo "TestBuild=${{ github.event.inputs.TestBuild }}"
                echo "Repository=$GITHUB_REPOSITORY"
                echo "Commit=$HEAD_COMMIT"
                echo "CommitShort=$SHORT_HEAD_COMMIT"
                echo "Product=actions"
                aws ec2 create-tags --resources "$AMI_ID" --tags \
                  Key=TestBuild,Value=${{ github.event.inputs.TestBuild }} \
                  Key=Commit,Value="$HEAD_COMMIT" \
                  Key=CommitShort,Value="$SHORT_HEAD_COMMIT" \
                  Key=Repository,Value="$GITHUB_REPOSITORY" \
                  Key=Product,Value="actions" \
                  Key=RunnerVersion,Value="${{ steps.versions.outputs.runner_version }}"
              else
                echo "::error title=Failed to find a AMI with specified filter and query::$AMI_ID"
                exit 1
              fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions