如何在Packer AMI数据源中配置地域以解决MissingRegion错误
在CI/CD流水线中使用Packer调用最新AWS AMI时触发错误:
Error: Datasource.Execute failed: Error querying AMI: MissingRegion: could not find region configuration
已在Packer的amazon-ami数据源块中指定region参数,但未生效;尝试在Packer构建命令中传递地域变量、在GitHub Workflow中设置环境变量后问题仍存在。相关配置如下:
Packer配置
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}" }
GitHub Workflow代码
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
AWS配置脚本
#!/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}"
已尝试的解决方法
- 修改Packer构建命令,直接传递
AWS_DEFAULT_REGION变量- name: Packer build working-directory: ./work_dir run: | packer build -timestamp-ui \ --var AWS_DEFAULT_REGION=eu-west-1 - 在Workflow顶层添加
AWS_REGION环境变量name: packer env: AWS_REGION: eu-west-1 on: workflow_run: workflows: [msbuild] types: - completed
1. 确认aws_region变量是否正确传入
检查variables/${{ matrix.environment }}.pkrvars.hcl文件中是否定义了aws_region变量,且值为eu-west-1(或对应环境的地域)。如果该变量未定义,Packer数据源中的region = "${var.aws_region}"会为空,直接导致地域缺失。
2. 明确指定AWS_PROFILE环境变量
你的AWS配置脚本生成了带环境后缀的profile(如company-dev),但Workflow中未指定使用哪个profile。在Packer构建步骤前添加环境变量:
- name: Set AWS Profile run: echo "AWS_PROFILE=company-${{ matrix.environment }}" >> $GITHUB_ENV
这样Packer会自动读取对应profile中的地域配置,无需额外传递参数。
3. 直接在Packer构建命令中强制传递aws_region变量
确保aws_region变量被明确传入,修改构建命令:
- name: Packer build working-directory: ./work_dir run: | packer build -timestamp-ui \ --var-file "variables/${{ matrix.environment }}.pkrvars.hcl" \ --var aws_region=eu-west-1 \ --var build_version=${{ steps.release_output.outputs.version }} \ --var artifacts_url=${{ steps.release_output.outputs.artifact_url }} \ --var artifacts_token=${{ secrets.GITHUB_TOKEN }} .
4. 验证AWS配置文件是否生效
在Setup AWS配置步骤后添加验证命令,确认配置文件生成正确:
- name: Verify AWS Config run: cat $AWS_CONFIG_FILE
检查输出中是否包含对应环境的profile,且region字段正确设置为eu-west-1。
5. 升级Packer版本
你使用的Packer 1.8.2版本较旧,可能存在数据源地域解析的bug。尝试升级到最新稳定版:
- name: Setup Packer uses: hashicorp-contrib/setup-packer@v1 with: packer-version: "latest"
内容的提问来源于stack exchange,提问作者srk786

