简体   繁体   English

如何在packer ami数据源中添加区域

[英]How to add region in packer ami data source

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我们有很多 aws ami,现在在我的管道中我想使用最新的 ami 但它因以下错误而失败

   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工作流 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 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}"

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在工作流文件中添加为 env 变量仍然相同

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

The error indicates that var.aws_region is undefined.该错误表明var.aws_region未定义。

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") }

The region in the data does not propagate to the entire Packer configs and templates. data中的region不会传播到整个 Packer 配置和模板。 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.此外,此错误作为 Packer 在命令执行之前使用的 go-aws SDK 进行身份验证的一部分抛出,因此 Packer 中变量声明的输入值也不会修复此问题。 You need to supply the region as an environment variable in your GH actions config to propagate to the AWS SDK for Go:您需要在 GH 操作配置中提供该区域作为环境变量,以传播到适用于 Go 的 AWS 开发工具包:

env:
  AWS_REGION: eu-west-1

However, you likely want this to be consistent with your variable declaration for aws_region .但是,您可能希望这与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.尽管这可以用可变输入覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为数据源指定特定地域的ACM证书? - How to specify a ACM certificate in a specific region for a data source? 将 AMI 从一个区域复制到另一个区域,然后无法通过 ssh 连接到新实例 - Copy AMI from one region to another then not able to ssh to new instance 如何将区域端点添加到 pub sub 模拟器 - How can I add region endpoints to pub sub emulator 如何在 ec2 ami 中安装 byobu - How do I install byobu in ec2 ami 如何按区域过滤数组中的项目? - How to filter items in array by region? Data studio - 不能向混合数据源添加 10 个以上的维度 - Data studio - cannot add more than 10 dimensions to a blended data source 如何在 Flutter 的 SfCalendar 数据源中显示来自 Firestore 的约会? - How to display appointment from Firestore in SfCalendar's data source in Flutter? 如何更改 Google Cloud 中的区域/区域? - How to change Region / Zone in Google Cloud? 如何使用 terraform 从 Ami(具有多个卷 -3+)旋转 ec2 - How to spin the ec2 from Ami(with multiple volumes -3+) using terraform 如何在 AWS Marketplace 中查找 CentOS 7 映像的 AMI ID? - How to find AMI ID of CentOS 7 image in AWS Marketplace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM