From 968868c2f546fd169116705d2d5a368cd0cb0f0b Mon Sep 17 00:00:00 2001 From: Scottpedia Date: Sat, 20 Mar 2021 01:14:59 -0400 Subject: [PATCH] use python dictionary to store AMI info instead of using conditioning --- aws/cloudformation-template-ipsec.json | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/aws/cloudformation-template-ipsec.json b/aws/cloudformation-template-ipsec.json index 5c1284e..1332983 100644 --- a/aws/cloudformation-template-ipsec.json +++ b/aws/cloudformation-template-ipsec.json @@ -651,21 +651,15 @@ " regionName = event['ResourceProperties']['Region']", " distribution = event['ResourceProperties']['Distribution']", " ec2 = boto3.client('ec2',regionName)", - " AMIName = ''", - " if distribution == 'Ubuntu1604':", - " AMIName = 'ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*'", - " elif distribution == 'Ubuntu1804':", - " AMIName = 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*'", - " elif distribution == 'Ubuntu2004':", - " AMIName = 'ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*'", - " elif distribution == 'Debian9':", - " AMIName = 'debian-stretch-hvm-x86_64-gp2-*'", - " elif distribution == 'CentOS7':", - " AMIName = 'CentOS 7.9.2009 x86_64'", - " elif distribution == 'CentOS8':", - " AMIName = 'CentOS 8.3.2011 x86_64'", - " elif distribution == 'AmazonLinux2':", - " AMIName = 'amzn2-ami-hvm-*.*-x86_64-gp2'", + " AMIName = {", + " 'Ubuntu1604': 'ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*',", + " 'Ubuntu1804': 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*',", + " 'Ubuntu2004': 'ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*',", + " 'Debian9': 'debian-stretch-hvm-x86_64-gp2-*',", + " 'CentOS7': 'CentOS 7.9.2009 x86_64',", + " 'CentOS8': 'CentOS 8.3.2011 x86_64',", + " 'AmazonLinux2': 'amzn2-ami-hvm-*.*-x86_64-gp2',", + " }[distribution]", " response = ec2.describe_images(Filters=[{'Name':'name', 'Values':[AMIName]}], Owners=['099720109477', '379101102735', '125523088429', 'amazon'])", " images = response['Images']", " images.sort(key=creation_date,reverse=True)",