-1

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/checkout@v2.4.2

  - 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
srk786
  • 551
  • 3
  • 10
  • 19
  • How are you invoking the packer build? – Paolo Dec 16 '22 at 11:35
  • we are running github actions to run the pipeline, i am not too familiar with pipeline but i noticed this in the workflow file - name: Packer build – srk786 Dec 16 '22 at 11:38
  • If i run packer locally then i can specify the region using aws configure command and my packer works file, but i dont know how to specify region in pipeline for the data source to work, We do have aws_region variable which we use in the source block and it works, but when i try to use the same in data source block, its not supported, also i am thinking is there a way to specify aws_region i i do locally in pipeline – srk786 Dec 16 '22 at 11:40
  • Show us the github actions yml file – Paolo Dec 16 '22 at 11:42
  • i have just noticed that in our workflow file, we are specifying aws_region in the aws configuration, so i dont know why data source still asks for region – srk786 Dec 16 '22 at 11:42
  • i just added the workflow file in main question, i am also adding the referenced script which sets the aws config – srk786 Dec 16 '22 at 11:50
  • Nowhere in the yml file do I see the AWS region being set – Paolo Dec 16 '22 at 11:52
  • if you check the 1st work flow file, it has reference to run: scripts/actions-setup-aws-config and the 2nd script shows region specified – srk786 Dec 16 '22 at 11:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250471/discussion-between-srk786-and-paolo). – srk786 Dec 16 '22 at 11:57

1 Answers1

0

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")
    }
    
Paolo
  • 21,270
  • 6
  • 38
  • 69
  • var.aws_region is set to default eu-west-1 and this is the variable i am using in the source block which works, if i take this variable off from the data source block, the error is what i specified in the question at the top. so it looks like somehow i need to force the region as env variable – srk786 Dec 16 '22 at 12:04
  • i have now added var in workflow file, still getting same error – srk786 Dec 16 '22 at 12:12
  • run: | packer build -timestamp-ui \ --var AWS_REGION=eu-west-1 --var-file "variables/${{ matrix.environment }}.pkrvars.hcl" \ – srk786 Dec 16 '22 at 12:12
  • @srk786 update the question with an "EDIT" section to show the new packer build command – Paolo Dec 16 '22 at 12:13
  • edited the question, tried both with AWS_REGION and AWS_DEFAULT_REGION according to https://github.com/hashicorp/packer/issues/9237 – srk786 Dec 16 '22 at 12:17
  • @srk786 you didn't follow my answer properly. You need to pass `--var aws_region=eu-west-1` , and not `--var AWS_DEFAULT_REGION...` – Paolo Dec 16 '22 at 12:19
  • even adding --var aws_region=eu-west-1 is not working still same issue – srk786 Dec 16 '22 at 16:00