Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overrides for tags and run_tags #303

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Overrides for tags and run_tags #303

wants to merge 3 commits into from

Conversation

guikcd
Copy link

@guikcd guikcd commented Aug 22, 2024

Summary

Support tags and/or run_tags in overrides.auto.pkrvars.hcl:

tags = {
  owner = "myteam"
  environment= "test"
}

run_tags = {
  owner = "packer"
  environment= "build"
}

Before this PR, users need to identity AMI and manually tags the resulted AMI.

Closes: #31

Implementation details

Define a variable for tags and run_tags for overrides

Then for each variant:

  • tags are moved into locals as default_tags
  • merge the default + user provided tags and define tags
  • define run_tags

Testing

Building AMIs without overriding these tags are OK
Building AMIs with these tags are OK:

ALL=$(ls al*|sed "s/.pkr.hcl//")

for variant in $ALL
do
        rm -f manifest.json
        REGION=eu-west-1 make ${variant}
        ami=$(jq -r ".builds[0].artifact_id" manifest.json | awk -F':' '{print $NF}')
        # tags are set when AMI is available, not during creation
        echo "wait AMI"
        aws ec2 wait image-available --image-ids $ami
        snap=$(aws ec2 describe-images --region eu-west-1 --image-ids $ami --query "Images[0].BlockDeviceMappings[0].Ebs.SnapshotId" --output text)
        echo "check tags"
        aws ec2 describe-images --region eu-west-1 --image-ids $ami --query "Images[0].Tags[]" --output text | grep --quiet "myteam"
        if [ $? -ne 0 ]
        then
                echo "WARNING: tags are not set! for $variant / $ami"
        fi
        echo "unregister"
        aws ec2 deregister-image --image-id $ami
        echo "delete snap"
        aws ec2 delete-snapshot --snapshot-id $snap
done

New tests cover the changes: yes

Description for the changelog

Feature: Support user provided tags and run_tags.

Licensing

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@guikcd guikcd requested a review from a team as a code owner August 22, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow custom tags
1 participant