So I have two different launch templates I am spinning up into 2 AZs. The code runs successfully, but only the first launch template "lt" runs successfully. Why is that?
import boto3
# I have called the EC2 resource
ec2 = boto3.resource('ec2')
# I have defined my launch template
lt = {
'LaunchTemplateName': 'My_Launch_Template',
'Version': '1'
}
lt_a = {
'LaunchTemplateName': 'My_Launch_Template_2',
'Version': '1'
}
# I have defined a function to execute
def lambda_handler(event, context):
instances = ec2.create_instances(
LaunchTemplate=lt,
MinCount=1,
MaxCount=1
)
def lambda_handler(event, context):
instances = ec2.create_instances(
LaunchTemplate=lt_a,
MinCount=1,
MaxCount=1
)
CodePudding user response:
Your code isn't valid Python code. I suggest you review the basics of the language before you start implementing the function on AWS lambda. Get all of your code working on your local machine and once you're happy, move it to AWS.
Anyway, I would rewrite your code to have a helper function. This way you maximize code reuse (and you make your life easier):
import boto3
ec2 = boto3.resource('ec2')
def create_instance(launch_template_name, launch_template_id):
lt = {
'LaunchTemplateName': launch_template_name,
'Version': launch_template_id
}
return ec2.create_instanceS(
LaunchTemplate=lt,
MinCount=1,
MaxCount=1
)
def lambda_handler(event, context):
first_instance = create_instance('My_Launch_Template', '1')
second_instance = create_instance('My_Launch_Template_2', '1')
...