-1

I am trying to launch instance using aws lambda using python, but I cannot pass my base64 encoded userdata script. The script looks like this:

import os
import boto3

AMI = "ami-052efd3df9dad4825"
INSTANCE_TYPE = "c6a.32xlarge"

ec2 = boto3.resource('ec2')
                
def lambda_handler(event, context):
    instance = ec2.create_instances(
        ImageId=AMI,
        InstanceType=INSTANCE_TYPE,
        MaxCount=1,
        MinCount=1,
        UserData=*my script here*,
    )

    print("New instance created:", instance[0].id)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Cicada
  • 1
  • 3
    "I cannot pass my base64 encoded userdata script. " - why? What does it mean? Can you provide any information explaining what is your issue? Any errors? – Marcin Aug 25 '22 at 10:37
  • You've left out any the user data script, and any error messages or unexpected behavior this code generates. There is not enough information to help you here. – Mark B Aug 25 '22 at 13:11

1 Answers1

0

You should be able to specify it without needing to base64 encode it, such as:

user_data = '''
#!/bin/bash
echo 'test' > /home/ec2/test.txt
'''

Make sure the first line starts with a #!.

See also: EC2 User Data not working via python boto command (It's old, but shows examples.)

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470