I am looking to launch an AWS instance by deploying a script. However, I do not fully understand what this means. What should be in the script in order to launch it and how do I approach this in order to meet the following requirements?
User specifies AWS credentials in a separate key file;
User invokes termination script and pass the instance ID from
command line;
Termination script shuts down AWS instance.
Upon completion, the termination script returns message indicating
whether the termination process has been completed successfully
I would appreciate some help in understanding what exactly a deployment script it and what language I should write it in. I have been coding thus far in Python and have created a script that creates an instance. But I am not sure how this is different from deploying an instance.
The usage of the expressions "create an instance" and "deploy an instance" can mean the same thing or different things. Depends on the engineer's viewpoint.
Basically creating an EC2 instance means the AWS definition of launching an EC2 instance. Deploying an EC2 instance may include additional configuration details such as patching the OS, installing software and applications, etc. It is up to you to decide which is which and how each should be done.
When deploying an EC2 instance, I prefer to configure a machine exactly the way that I want with OS patches, software and my applications. Then I create an AMI. When I then launch a new EC2 instance, I use my hand created AMI. Then the new EC2 instance is exactly what I want. No long deployment phase.
Best practices when writing scripts. Do not store your Amazon credentials in your scripts, source code, random files, etc. Install the Amazon CLI (Command Line Internface) tool and then configure the CLI with your credentials. Now your credentials are stored in a well defined location with the added benefit that Amazon SDKs, scripts, etc. will know how to find the credentials and will automatically load and use them.
The easiest way of writing scripts to manage AWS services is to use the AWS CLI. Just about anything that you can do in the Amazon Management Console, you can do with the CLI. The CLI works on Windows, Linux and Mac OS.
AWS Command Line Interface
Here is a CLI example that will terminate an EC2 instance. Replace with your instance ID:
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Writing your scripts in Python is another good idea. Managing AWS services with Python is very easy; there are lots of examples available on the Internet; and Python is just so easy and quick to develop Amazon apps. Use the Boto3 library and not the older Boto library. I use Python 3.x for all new development, but be aware that there is a lot of already created work on the Internet for AWS that runs under Python 2.x.
CLI EC2 Commands
Related
I have Python script that is supposed to run once every few days to annotate some data on a remote database.
Which PaaS services (GAE, Heroku, etc.) allows for a stand-alone Python script to be deployed and executed via some sort of cron scheduler?
GAE has a module called cron jobs and Heroku has Heroku Scheduler. Both are fairly easy to use and configure. You can check the documentation of both. As I do not have any other information on what you want to do I don’t know if one would be more suitable to you than the other.
I'm looking to create an AWS system with one master EC2 instance which can create other instances.
For now, I managed to create python files with boto able to create ec2 instances.
The script works fine in my computer environment but when I try to deploy it using Amazon BeanStalk with Django (Python 3.4 included) the script doesn't work. I can't configure aws cli (and so Boto) through SSL because the only user I can access is ec2-user and the web server uses another user.
I could simply handwrite my access ID key and password on the python file but that would not be secure. What can I do to solve this problem?
I also discovered AWS cloudformation today, is it a better idea to create new instances with that rather than with the boto function run?
This sounds like an AWS credentials question, not specifically a "create ec2 instances" question. The answer is to assign the appropriate AWS permissions to the EC2 instance via an IAM role. Then your boto/boto3 code and/or the AWS CLI running on that instance will have permissions to make the necessary AWS API calls without having an access key and secret key stored in your code.
I'm hosting a website on AWS. Its a web interface with a SQL database. The website will be used to:
1. View results of query from Database
2. Insert data into database
3. View the data and update it where needed.
The codes and connections works file when I run the application on localhost (Apache on my C drive). But we want to host it on AWS so that people around me can use it.
So, In AWS I uploaded the code on EC2 and installed apache on it, all the html links are working but the python file is simply displaying the code.
I'm guessing it has something to do with the shebang. Currently my code has the following shebang:
#!C:\Python27\python.exe
Can someone guide me if its the shebang or if there is something else i need to do.
I have installed boto, but not sure what to do next. The AWS website and most of the forums talk about using Elastic Beanstalk. I want to host a fully functioning Python webApp on AWS without using Elastic Beanstalk.
When apache displays code, that is a clear sign that Apache is not configured properly to execute python. You should look to see if mod_python is installed and configured correctly.
Also, #! is generally used with Linux not windows. If apache/mod_python is installed and configured correctly I can't imagine what code you'd have that would need #! since the .py extension would be enough.
IF your EC2 instance is indeed running Linux, and your code does indeed need #! try:
#!/bin/python
OR
#!/usr/local/bin/python
(Depends on where the python binary is, and those are the most common locations.)
If your EC2 instance is running Windows then "Unless you are using cygwin, windows has no shebang support"
Hi have you logged into your EC2 instance through the endpoint and then run your script, from the command line. I have some experience with EC2 running apache2 only my application was written in Java, having previously used python scripts I was able to run them by logging into my EC2 instance, you can do this from AWS management console. hope this helps you somewhat.
Say I have a file "main.py" and I just want it to run at 10 minute intervals, but not on my computer. The only external libraries the file uses are mysql.connector and pip requests.
Things I've tried:
PythonAnywhere - free tier is too limiting (need to connect to external DB)
AWS Lambda - Only supports up to Python 2.7, converted my code but still had issues
Google Cloud Platform + Heroku - can only find tutorials covering deploying applications, I think these could do what I'm looking for but I can't figure out how.
Thanks!
I'd start by taking a look at this question/answer that I asked previously on unix.stackexchange - I went with an AWS redhat installation and it was free to use.
Once you've decided on your VM, you can add SSH onto your server using any SSH client and upload your Python script. A personal preference is this application.
If you need to update the Python version on the server, you can do this by installing the required Python RPMs. A quick google should return the yum [or whichever RPM management system you're using] repository for the required RPMs.
Once you've installed the version of Python that you need, I'd suggest looking into the 'crontab' which can be used to schedule jobs. You can set a cronjob to run every 10minutes which will call your script.
See this site for more information on how to use the crontab
This sounds like a perfect use case for AWS Lambda which supports Python. You can invoke your Lambda on a schedule using Scheduled Events.
I see that you tried Lambda and it didn't work out for you which is too bad as that seems like the easiest route. You could also launch an EC2 instance and use userdata to schedule a cron when the instance starts.
Another option would be an Elastic Beanstalk worker with a cron.yml that defines your schedule. Elastic Beanstalk supports Python 3.4.
Update: AWS does now support Python 3.6. Just select Python 3.6 from the runtime environments when configuring.
I am attempting to write a python code that would few of my manual steps in logging into the AWS platform.
In Ubuntu terminal , I used to write the command
ssh -A ec2-user#<ip-address>
and then again log into another instance using
ssh ec2-user#<ip.address>
Now I am looking at python code that would be able to automate this logging in process. I have written the following code till now.
import boto3
ec2 = boto3.resource('ec2')
There are 2 ways mostly to configure the boto3 library.
You need to configure it first on your system and use the same configuration everywhere. You can use AWS CLI for this by running aws configure on your terminal.
Set the environment variables and call the boto3 configuration via process.env.ENV_KEY and then use it like :
client = boto3.client(
'ec2',
aws_access_key_id=process.env.ACCESS_KEY,
aws_secret_access_key=process.env.SECRET_KEY,
aws_session_token=process.env.SESSION_TOKEN,
)
If you want to perform actions on a running instance, boto3 is not what you're looking for. What you're asking about is more in the realm of what's called configuration management.
While you could write something yourself using an SSH library like Paramiko, you may want to look at a more purpose-built software package like Fabric. It's built on-top of the aforementioned Paramiko, with added functionality tailored to running commands on remote servers. For a more full-featured, open source configuration management solution, I recommend looking into Ansible.
AWS also has a native service for configuring EC2 instances called EC2 Run Command.