From 40cb525803ada41620aa09d8ea137457af93b472 Mon Sep 17 00:00:00 2001 From: Scottpedia Date: Sun, 23 Aug 2020 17:36:47 -0400 Subject: [PATCH] Add the sorting function --- aws/cloudformation-template-ipsec | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/aws/cloudformation-template-ipsec b/aws/cloudformation-template-ipsec index acf4bd2..71038ba 100644 --- a/aws/cloudformation-template-ipsec +++ b/aws/cloudformation-template-ipsec @@ -594,6 +594,9 @@ [ "import boto3", "import cfnresponse", + "def creation_date(e):", + " return e['CreationDate']", + "", "def handler(event, context):", " try:", " regionName = event['ResourceProperties']['Region']", @@ -601,13 +604,15 @@ " ec2 = boto3.client('ec2',regionName)", " IAMName = ''", " if distribution == 'Ubuntu16.04':", - " IAMName = 'ami-ubuntu-16.04-1.12.0-00-1538138520'", + " IAMName = 'ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*'", " elif distribution == 'Ubuntu18.04':", - " IAMName = 'ami-ubuntu-18.04-1.16.2-00-1571834140'", + " IAMName = 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*'", " elif distribution == 'Debian9':", - " IAMName = 'debian-stretch-hvm-x86_64-gp2-2020-07-20-58035'", + " IAMName = 'debian-stretch-hvm-x86_64-gp2-*'", " response = ec2.describe_images(Filters=[{'Name':'name', 'Values':[IAMName]}])", - " AMIId = response['Images'][0]['ImageId']", + " images = response['Images']", + " images.sort(key=creation_date,reverse=True)", + " AMIId = images[0]['ImageId']", " cfnresponse.send(event, context, cfnresponse.SUCCESS, {'AMIId':AMIId}, 'AMIInfo')", " except Exception:", " cfnresponse.send(event, context, cfnresponse.FAILED, {})"