Answer the question
In order to leave comments, you need to log in
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
)
[email protected]:~# ssh -i "EC2keys.pem" [email protected]
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question