Last active
November 18, 2020 20:42
-
-
Save stewmi/0c1dc7e84da41e54eb28f8bd869e7e05 to your computer and use it in GitHub Desktop.
a Python script to update Launch Template in EC2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import argparse | |
ec2 = boto3.client('ec2') | |
DEFAULT_INSTANCE_PROFILE = "" | |
DEFAULT_LT_NAME = "" | |
def create_lt_version(lt_name, iam_instance_profile): | |
''' | |
:param lt_name: the Launch Template Name that needs updated. | |
:param iam_instance_profile: the IAM instance profile to associate with the LT version. | |
:return: the latest version number | |
''' | |
response = ec2.create_launch_template_version( | |
LaunchTemplateName=lt_name, | |
LaunchTemplateData={ | |
'IamInstanceProfile': { | |
'Name': iam_instance_profile | |
}, | |
'CpuOptions': { | |
'CoreCount': 48, | |
'ThreadsPerCore': 1 | |
} | |
} | |
) | |
return response['LaunchTemplateVersion']['VersionNumber'] | |
def set_version(lt_name, version_number): | |
ec2.modify_launch_template( | |
LaunchTemplateName=lt_name, | |
DefaultVersion=str(version_number) | |
) | |
def main(): | |
parser = argparse.ArgumentParser(description="A script to update AWS EC2 Launch Template.") | |
parser.add_argument( | |
'--template', | |
help='the name of the EC2 Launch Template.', | |
action='store', | |
dest="template", | |
default=DEFAULT_LT_NAME) | |
parser.add_argument( | |
'--instance-profile', | |
help='the name of the IAM Instance Profile to use within the template.', | |
action='store', | |
dest="instance_profile", | |
default=DEFAULT_INSTANCE_PROFILE) | |
args = parser.parse_args() | |
print(args.template) | |
version = create_lt_version(args.template, args.instance_profile) | |
set_version(args.template, version) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment