A
A
alex_devPY2018-11-05 19:23:21
Python
alex_devPY, 2018-11-05 19:23:21

How do I execute a script on an Amazon instance after it has been created?

Hello.
How do I execute a Boto3 script on an Amazon instance after it is created or during creation?

import boto3
import random

key = ''
secret = ''
ec2 = boto3.resource(
                        'ec2', 
                        aws_access_key_id=key, 
                        aws_secret_access_key=secret, 
                        region_name='eu-central-1'
                    )
try:
    key_pair = ec2.create_key_pair(KeyName='EC2keys')
except Exception as e:
    print e
    
script = '''
#!/bin/bash
apt-get install -y apache2
'''

# Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type - ami-0233214e13e500f77
droplet = ec2.create_instances(
                                ImageId='ami-0233214e13e500f77', 
                                MinCount=1, 
                                MaxCount=1, 
                                InstanceType='t2.nano', 
                                KeyName='EC2keys', 
                                UserData = script
                               )

The instance itself is created. Status: running
But I don't see Apache installed on it. I can't login with a key either.
[email protected]:~# ssh -i "EC2keys.pem" [email protected]

and hangs here (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2018-11-05
@alex_devPY

If you can’t log in using the key, then either the EC2 instance is not accessible from the outside (look towards security groups to begin with) and in general, where is the specific indication of which subnet the instance will be located in?
And Apache doesn’t get up for you, most likely because there is no traffic to the instance (again, look at the infrastructure)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question