hashicorp / packer-plugin-amazon

Packer plugin for Amazon AMI Builder

Home Page:https://www.packer.io/docs/builders/amazon

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support deprecated sources by adding a `IncludeDeprecated` flag to `source_ami_filter`

Ckarles opened this issue Β· comments

Community Note

Please vote on this issue by adding a πŸ‘ reaction to the original issue to help the community and maintainers prioritize this request.
Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request.
If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Description

A call to AWS API DescribeImages can contain a boolean parameter IncludeDeprecated. Sending a DescribeImages with this flag to True while using Filters to find an image will add in the response non-private images which have been marked as deprecated.

I think this parameter is useful and should be supported by packer-plugin-amazon.

Use Case(s)

Using deprecated images as source.
Issue I'm facing right now: AMI was deprecated and we can't build from this images anymore. I'd like to only acknowledge and use IncludeDeprecated instead of the alternative (manually calling DescribeImage before the call to packer and find the ami-id to pass to packer).

Potential References

DescribeImage AWS API doc: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImages.html
DescribeImage golang sdk: https://github.com/aws/aws-sdk-go-v2/blob/38e10609d10442e497844d3add2e4b9c6d2889f1/service/ec2/api_op_DescribeImages.go#L128C3-L128C20

Implementation:
AmiFilterOptions

type AmiFilterOptions struct {

DescribeImage parameters set:
if len(d.Filters) > 0 {
amiFilters, err := buildEc2Filters(d.Filters)
if err != nil {
err := fmt.Errorf("Couldn't parse ami filters: %s", err)
return nil, err
}
params.Filters = amiFilters
}
if len(d.Owners) > 0 {
params.Owners = d.GetOwners()
}

DescribeImage request:
req, resp := ec2conn.DescribeImagesRequest(&ec2.DescribeImagesInput{
Owners: []*string{aws.String("self")},
Filters: []*ec2.Filter{{
Name: aws.String("name"),
Values: []*string{aws.String(s.DestAmiName)},
}}})

Hi @Ckarles,

Good idea! I've opened a PR for this, if this is merged, this will be part of the next plugin release.

Thanks for the suggestion!

Out of curiosity, and since it may help me write an acceptance test for this feature, could I ask you which image you are depending on if this is publicly available? I can find a deprecated AMI to run a build on I would think, but this would save me some search time if you can share yours :)

Hi,

thanks for the quick patch!

This image for example (on eu-west-1):

{
            "Architecture": "x86_64",
            "CreationDate": "2021-07-07T06:40:13.000Z",
            "ImageId": "ami-058b1b7fe545997ae",
            "ImageLocation": "amazon/amzn2-ami-hvm-2.0.20210701.0-x86_64-gp2",
            "ImageType": "machine",
            "Public": true,
            "OwnerId": "137112412989",
            "PlatformDetails": "Linux/UNIX",
            "UsageOperation": "RunInstances",
            "State": "available",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "SnapshotId": "snap-049de0aefa0006007",
                        "VolumeSize": 8,
                        "VolumeType": "gp2",
                        "Encrypted": false
                    }
                }
            ],
            "Description": "Amazon Linux 2 AMI 2.0.20210701.0 x86_64 HVM gp2",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "Name": "amzn2-ami-hvm-2.0.20210701.0-x86_64-gp2",
            "RootDeviceName": "/dev/xvda",
            "RootDeviceType": "ebs",
            "SriovNetSupport": "simple",
            "VirtualizationType": "hvm",
            "DeprecationTime": "2023-07-07T06:40:13.000Z"
        }