Home > Enterprise >  Using Boto3 for EC2 Launch Templates
Using Boto3 for EC2 Launch Templates

Time:08-19

So, I am trying to leverage my created launch template via python

import boto3

ec2 = boto3.resource('ec2')

lt = {
    'LaunchTemplateId': 'lt-abcd',
    'LaunchTemplateName': 'My_Launch_Template',
    'Version': '1'
}


def handler(event, context):

    instances = ec2.create_instances(
        LaunchTemplate=lt,
        MinCount=1,
        MaxCount=1
        # UserData=user_data
    )

But I keep getting this weird error message every time

  "errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'",
  "errorType": "Runtime.HandlerNotFound",

My IAM role has the EC2 policy set to full access.

CodePudding user response:

Your lambda function has lambda_function.lambda_handler configured as the handler, therefore either you rename your function:

def lambda_handler(event, context):

    instances = ec2.create_instances(
        LaunchTemplate=lt,
        MinCount=1,
        MaxCount=1
        # UserData=user_data
    )

or you change the handler to lambda_function.handler (you can change this in the lambda settings).

You can find more information on lambda handler here: https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html

  • Related