[英]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:你有两个选择:
You pass the region as a variable from the pipeline:您将该区域作为管道中的变量传递:
packer build -timestamp-ui \... -var "aws_region=..."...
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.