Reputation: 2253
I want to host a website on a small AWS instance (e.g. t2.micro
) that launches a larger instance (e.g. m5.2xlarge
) whenever a user uploads data to be processed. I also want the larger instance to shut down when the processing is finished. Is there an AWS service or some other workaround that can do this? The larger instance would only be triggered every few days.
Upvotes: 0
Views: 121
Reputation: 2253
You can also point the instance to the correct S3 bucket during launch. Just provide a script to the UserData
argument:
# Point to bucket.
parent = 'my-awesome-s3-bucket'
startup_script = '''#!/bin/bash
bucket=<PARENT_NAME>
aws s3 cp s3://$bucket /home/ubuntu --recursive
'''
startup_script = startup_script.replace('<PARENT_NAME>', parent)
# Launch instance.
try:
response = ec2.run_instances(
InstanceType='m4.2xlarge',
KeyName='aws3',
SecurityGroups=['launch-wizard-12'],
MinCount=1,
MaxCount=1,
UserData=startup_script
)
Upvotes: 0
Reputation: 269490
The easiest way to stop an instance when it has finished a task is to issue a shutdown
command to the Operating System. The default behaviour will be to Stop the EC2 instance.
The easiest way to start the instance is to call the StartInstances()
command, either via command-line or via an SDK. The tricky part is telling the instance what data should be processed. Also, consider what should happen if another data file is uploaded while the first file is being processed -- should it shutdown, or should it work on the new file?
I would recommend:
StartInstances()
to start the bigger instanceSee: Auto-Stop EC2 instances when they finish a task - DEV
Upvotes: 1