Use Product Code to filter AMIs used as source images by packer

Add documentation for this process

Change-Id: I5d548ceefb93602d751b5c88230e7dba0a88be2a
diff --git a/README.md b/README.md
index c6aa3ed..600674f 100644
--- a/README.md
+++ b/README.md
@@ -169,6 +169,52 @@
 > The new AMI ID can be found near the end of the logs of the run of
 > [ci-management-packer-merge-<ostype>-basebuild](https://jenkins.opencord.org/job/ci-management-packer-merge-ubuntu-16.04-basebuild/).
 
+### Finding source AMI images
+
+Source OS images published by upstream projects like Ubuntu and CentOS need to
+be well specified, so that the correct images are used.  Anyone can list in the
+marketplace, so care should be taken to use the correct images.
+
+This is done in Packer using
+[source_ami_filter](https://packer.io/docs/builders/amazon-ebs.html#source_ami_filter)
+with is parameterized on the image `name`, `owner`, and `product-code` within
+the `packer/vars/<os_name>.json` files that define the source images.
+
+Upstream docs that specify AMIs:
+
+- [CentOS](https://wiki.centos.org/Cloud/AWS)
+- [Ubuntu](https://cloud-images.ubuntu.com/locator/ec2/)
+
+Unfortunately these filter parameters have conflicts - images with the official
+Ubuntu owner (`099720109477`) doesn't specify a `product-code` field.
+
+As an alternative, `aws-marketplace` owner is used, which also has the same
+images. To find the `product-code`, go to the [AWS
+Marketplace](https://aws.amazon.com/marketplace) and find the image you want,
+then click the button to launch the image. In the URL there will be a
+`productId` UUID parameter - find this, and then use it search for a product
+code using the [aws command
+line](https://docs.aws.amazon.com/cli/latest/index.html):
+
+    aws ec2 describe-images \
+        --owners aws-marketplace \
+        --filters "Name=name,Values=*d83d0782-cb94-46d7-8993-f4ce15d1a484*"
+
+Then look at the output for the `ProductCodeId` - this is what should go in the
+OS json file in the `source_ami_filter_product_code` field.
+
+Once you've determined the correct settings, the Packer filter can be tested
+with this command:
+
+    aws ec2 describe-images \
+        --owners aws-marketplace \
+        --filters "Name=name,Values=*ubuntu*16.04*" \
+                  "Name=product-code,Values=csv6h7oyg29b7epjzg7qdr7no" \
+                  "Name=architecture,Values=x86_64" \
+                  "Name=virtualization-type,Values=hvm" \
+                  "Name=root-device-type,Values=ebs"
+
+
 ### Adding additional EC2 instance types
 
 If you create a new cloud instance type, make sure to set both the `Security
diff --git a/packer/templates/basebuild.json b/packer/templates/basebuild.json
index 92f5090..e19bf61 100644
--- a/packer/templates/basebuild.json
+++ b/packer/templates/basebuild.json
@@ -1,10 +1,12 @@
 {
   "variables": {
+    "instance_type": "t2.medium",
     "aws_access_key": null,
     "aws_security_key": null,
     "security_group_id": null,
     "source_ami_filter_name": null,
     "source_ami_filter_owner": null,
+    "source_ami_filter_product_code": "",
     "subnet_id": null,
     "ssh_user": null,
     "distro": null,
@@ -15,13 +17,14 @@
     {
       "access_key": "{{user `aws_access_key`}}",
       "ami_name": "{{user `distro`}} - basebuild - {{user `arch`}} - {{isotime \"20060102-1504\"}}",
-      "instance_type": "t2.micro",
+      "instance_type": "{{user `instance_type`}}",
       "region": "us-west-2",
       "secret_key": "{{user `aws_security_key`}}",
       "security_group_id": "{{user `security_group_id`}}",
       "source_ami_filter": {
         "filters": {
           "name": "{{user `source_ami_filter_name`}}",
+          "product-code": "{{user `source_ami_filter_product_code`}}",
           "architecture": "{{user `arch`}}",
           "root-device-type": "ebs",
           "virtualization-type": "hvm"
diff --git a/packer/vars/centos-7.json b/packer/vars/centos-7.json
index aa629a9..340f545 100644
--- a/packer/vars/centos-7.json
+++ b/packer/vars/centos-7.json
@@ -1,6 +1,8 @@
 {
   "source_ami_filter_name": "*CentOS Linux 7*HVM*",
-  "source_ami_filter_owner": "679593333241",
+  "source_ami_filter_owner": "aws-marketplace",
+  "source_ami_filter_product_code": "aw0evgkw8e5c1q413zgy5pjce",
+
   "ssh_user": "centos",
 
   "distro": "CentOS 7",
diff --git a/packer/vars/ubuntu-16.04.json b/packer/vars/ubuntu-16.04.json
index 972ff75..4f7d473 100644
--- a/packer/vars/ubuntu-16.04.json
+++ b/packer/vars/ubuntu-16.04.json
@@ -1,6 +1,7 @@
 {
   "source_ami_filter_name": "*ubuntu*16.04*",
-  "source_ami_filter_owner": "099720109477",
+  "source_ami_filter_owner": "aws-marketplace",
+  "source_ami_filter_product_code": "csv6h7oyg29b7epjzg7qdr7no",
 
   "ssh_user": "ubuntu",