Home > Enterprise >  How to add region in packer ami data source
How to add region in packer ami data source

Time:12-17

We have a number of aws ami's, now in my pipeline i would like to use the latest ami but its failing on the following error

   Error: Datasource.Execute failed: Error querying AMI: MissingRegion: could not find region configuration

I have given the region in the source block but when i specify region in data source block its not working. Can you please suggest how can i accomplish this

packer {
   required_plugins {
     amazon = {
       version = ">= 0.0.1"
       source  = "github.com/hashicorp/amazon"
     }
   }
}

data "amazon-ami" "my_ami_ds" {
  filters = {
    virtualization-type = "hvm"
    name                = "my-test-ami*"
    root-device-type    = "ebs"
  }
  owners      = ["${var.aws_account_id}"]
  most_recent = true
  region      = "${var.aws_region}"
}

Workflow yaml

      name: packer

      on:
        workflow_run:
        workflows: [msbuild]
        types:
  - completed

jobs:
  packer:
  runs-on: my-runner
  strategy:
   matrix:
    environment: ['dev','test','prod']

steps:
  - name: Ensure previous build succeeded
    if: ${{ github.event.workflow_run.conclusion != 'success' }}
    run: exit 1

  - uses: actions/[email protected]

  - name: Setup AWS configuration
    run: scripts/actions-setup-aws-config

  - name: Setup Packer
    uses: hashicorp-contrib/setup-packer@v1
    with:
      packer-version: 1.8.2

  - name: Get release info
    id: release_info
    uses: actions/github-script@v6
    with:
      script: |
        const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
           owner: context.repo.owner,
           repo: context.repo.repo,
           run_id: context.payload.workflow_run.id,
        });
        return {
          artifactId: artifact.id,
          version: artifact.name.replace(artifactBaseName, "")
        };
  - name: Set release info
    id: release_output
    run: |
      ARTIFACT_ID=$(echo '${{ steps.release_info.outputs.result }}' | jq '.artifactId')
      ARTIFACT_URL=https://api.github.com/repos/${{ github.repository }}/actions/artifacts/$ARTIFACT_ID/zip
      VERSION=$(echo '${{ steps.release_info.outputs.result }}' | jq '.version')
      echo "::set-output name=artifact_url::$ARTIFACT_URL"
      echo "::set-output name=version::$VERSION"
  - name: Packer init
    working-directory: ./work_dir
    run: packer init .

  - name: Packer build
    working-directory: ./work_dir
    run: |
      packer build -timestamp-ui \
        --var-file "variables/${{ matrix.environment }}.pkrvars.hcl" \
        --var build_version=${{ steps.release_output.outputs.version }} \
        --var artifacts_url=${{ steps.release_output.outputs.artifact_url }} \
        --var artifacts_token=${{ secrets.GITHUB_TOKEN }} .
  - name: Get AMI ID
    working-directory: ./work_dir
    run: |
      AMI_ID=$(jq -r '.builds[-1].artifact_id' manifest.json | cut -d ":" -f2)
      echo "Created AMI with ID $AMI_ID" >> $GITHUB_STEP_SUMMARY
     service-company-api-pipeline/packer.yml at master · 
     HavenEngineering/service-company-api-pipeline

aws config script

      #!/bin/bash

      set -o errexit
      set -o pipefail
      set -o nounset



     source "$(dirname "${0}")/common"

     #ensure_invoked_from_repo
     #ensure_invoked_in_github_actions

     repo_root_path="$(git rev-parse --show-toplevel)"
     aws_config_path="${repo_root_path}/.aws_config"

     rm -f "${aws_config_path}"
     touch "${aws_config_path}"

     source "$(dirname "${0}")/aws-account-ids"

    for env in "${!aws_account_ids[@]}"; do
       cat <<EOF >>"${aws_config_path}"
       [profile company-${env}]
       region            = eu-west-1
       role_arn          = arn:aws:iam::${aws_account_ids[${env}]}:role/ciadmin
      credential_source = Ec2InstanceMetadata
      EOF
      done

      echo "AWS_CONFIG_FILE=${aws_config_path}" >> "${GITHUB_ENV}"

New packer build command

   - name: Packer build
    working-directory: ./work_dir
    run: |
      packer build -timestamp-ui \
        --var AWS_DEFAULT_REGION=eu-west-1

Added as env variable in workflow file still same

  name: packer
  env:
      AWS_REGION: eu-west-1
  on:
    workflow_run:
    workflows: [msbuild]
    types:
        - completed

CodePudding user response:

The error indicates that var.aws_region is undefined.

You have two options:

  1. You pass the region as a variable from the pipeline:

    packer build -timestamp-ui \
      ...
      -var "aws_region=..."
      ...
    
  2. You export the region as an environment variable in your pipeline:

    export AWS_REGION=...
    

    and then reference it in the packer file:

    variable "aws_region" {
      default = env("AWS_REGION")
    }
    

CodePudding user response:

The region in the data does not propagate to the entire Packer configs and templates. Also, this error is thrown as part of the authentication with the go-aws SDK utilized by Packer prior to command execution, and as such an input value for the variable declaration in Packer will not fix this either. You need to supply the region as an environment variable in your GH actions config to propagate to the AWS SDK for Go:

env:
  AWS_REGION: eu-west-1

However, you likely want this to be consistent with your variable declaration for aws_region. You can easily ensure this by default with:

variable "aws_region" {
  default = env("AWS_REGION")
}

although this can be overwritten with variable inputs.

  • Related