1

My first python script is a file that creates an EC2 instance, and i am attempting have this script call a function from another python file i imported, the code is shown here:

import boto3
import amif2.py

session = boto3.Session(region_name='us-east-1')

ec2 = session.client('ec2')

response = ec2.describe_images(
    Filters=[
        {
            'Name': 'name',
            'Values': ['temp']
        }
    ]
)

for each in response['Images']:
    image_id = each['ImageId']

ec2_response = ec2.run_instances(
    ImageId=image_id,
    InstanceType="---",
    SubnetId=---,
    SecurityGroupIds=[
        -
    ],

    IamInstanceProfile={
        'Arn': '---'
    },
    KeyName='ec2-creation-dev',
    MaxCount=1,
    MinCount=1,
    TagSpecifications=[
        {
            'ResourceType': 'instance',
            'Tags': [
                {
                    'Key': 'Name',
                    'Value': 'temp'
                },
            ]
        },
        {
            'ResourceType': 'volume',
            'Tags': [
                {
                    'Key': 'Name',
                    'Value': 'temp'
                },
            ]
        },
    ]
)

print(ec2_response)

amif2.py.amifunc2()

inside this amif2.py is this function:

def amifunc2():

        touch bashfile.txt
        open("pythoncommandfile.txt","w+")

I tried both commmands, because my first script launches an EC2 instance, and inside this instance, the commands are in bash hence the touch file command, but i also wanted to test if it were the python command that creates the file in the instance. however after testing i get this error:

Traceback (most recent call last):
  File "amibake7.py", line 2, in <module>
    import amif2.py
ImportError: No module named 'amif2.py'; 'amif2' is not a package

2 Answers 2

1

As you're using AWS (and Boto3) I would recommend using the built in tools that allow remote execution of scripts. The service is Systems Manager Run Command.

The instance would need to be created with an IAM role that supports this to happen.

Assuming this is done, you could update your function to support taking the argument of the instance ID. Then run the remote command by taking benefit of the existing AWS-RunShellScript document or create your own document.

In Boto3 the execution would look like the below

client = boto3.client('ssm')

response = client.send_command(
    InstanceIds=[
        'i-123456', #Replace this with your instance ID
    ],
    DocumentName='AWS-RunShellScript',
    Parameters={
        'commands': [
            'touch bashfile.txt',
        ]
    }
)
Sign up to request clarification or add additional context in comments.

Comments

1

just do

import amif2

you don't need to put the extension in and make sure amif2.py is in the same directory as the python script

1 Comment

it is, but i want this file to be created inside the ec2 instance being made by the script. this file just creates inside the instance i ran the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.