data-science-ipython-notebooks/aws/aws.ipynb

1074 lines
28 KiB
Plaintext
Raw Normal View History

{
2015-04-16 02:54:26 +08:00
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks).</i></small>"
]
},
2015-04-16 02:54:26 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Amazon Web Services (AWS)\n",
2015-04-16 02:54:26 +08:00
"\n",
"* SSH to EC2\n",
"* Boto\n",
2015-04-16 02:54:26 +08:00
"* S3cmd\n",
"* s3-parallel-put\n",
"* S3DistCp\n",
"* Redshift\n",
"* Kinesis\n",
"* Lambda"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ssh-to-ec2\">SSH to EC2</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Connect to an Ubuntu EC2 instance through SSH with the given key:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!ssh -i key.pem ubuntu@ipaddress"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Connect to an Amazon Linux EC2 instance through SSH with the given key:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!ssh -i key.pem ec2-user@ipaddress"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Boto\n",
"\n",
"[Boto](https://github.com/boto/boto) is the official AWS SDK for Python.\n",
"\n",
"Install Boto:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"!pip install Boto"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Configure boto by creating a ~/.boto file with the following:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"aws_access_key_id = YOURACCESSKEY\n",
"aws_secret_access_key = YOURSECRETKEY"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Work with S3:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import boto\n",
"s3 = boto.connect_s3()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Work with EC2:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import boto.ec2\n",
"ec2 = boto.ec2.connect_to_region('us-east-1')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a bucket and put an object in that bucket:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import boto\n",
"import time\n",
"s3 = boto.connect_s3()\n",
"\n",
"# Create a new bucket. Buckets must have a globally unique name (not just\n",
"# unique to your account).\n",
"bucket = s3.create_bucket('boto-demo-%s' % int(time.time()))\n",
"\n",
"# Create a new key/value pair.\n",
"key = bucket.new_key('mykey')\n",
"key.set_contents_from_string(\"Hello World!\")\n",
"\n",
"# Sleep to ensure the data is eventually there.\n",
"# This is often referred to as \"S3 eventual consistency\".\n",
"time.sleep(2)\n",
"\n",
"# Retrieve the contents of ``mykey``.\n",
"print key.get_contents_as_string()\n",
"\n",
"# Delete the key.\n",
"key.delete()\n",
"\n",
"# Delete the bucket.\n",
"bucket.delete()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each service supports a different set of commands. Refer to the following for more details:\n",
"* [AWS Docs](https://aws.amazon.com/documentation/)\n",
"* [Boto Docs](http://boto.readthedocs.org/en/latest/index.html)"
]
},
2015-04-16 02:54:26 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"s3cmd\">S3cmd</h2>\n",
"\n",
"Before I discovered [S3cmd](http://s3tools.org/s3cmd), I had been using the [S3 console](http://aws.amazon.com/console/) to do basic operations and [boto](https://boto.readthedocs.org/en/latest/) to do more of the heavy lifting. However, sometimes I just want to hack away at a command line to do my work.\n",
"\n",
"I've found S3cmd to be a great command line tool for interacting with S3 on AWS. S3cmd is written in Python, is open source, and is free even for commercial use. It offers more advanced features than those found in the [AWS CLI](http://aws.amazon.com/cli/)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install s3cmd:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!sudo apt-get install s3cmd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Running the following command will prompt you to enter your AWS access and AWS secret keys. To follow security best practices, make sure you are using an IAM account as opposed to using the root account.\n",
"\n",
"I also suggest enabling GPG encryption which will encrypt your data at rest, and enabling HTTPS to encrypt your data in transit. Note this might impact performance."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!s3cmd --configure"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Frequently used S3cmds:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# List all buckets\n",
"!s3cmd ls\n",
"\n",
"# List the contents of the bucket\n",
"!s3cmd ls s3://my-bucket-name\n",
"\n",
"# Upload a file into the bucket (private)\n",
"!s3cmd put myfile.txt s3://my-bucket-name/myfile.txt\n",
"\n",
"# Upload a file into the bucket (public)\n",
"!s3cmd put --acl-public --guess-mime-type myfile.txt s3://my-bucket-name/myfile.txt\n",
"\n",
"# Recursively upload a directory to s3\n",
"!s3cmd put --recursive my-local-folder-path/ s3://my-bucket-name/mydir/\n",
"\n",
"# Download a file\n",
"!s3cmd get s3://my-bucket-name/myfile.txt myfile.txt\n",
"\n",
"# Recursively download files that start with myfile\n",
"!s3cmd --recursive get s3://my-bucket-name/myfile\n",
"\n",
"# Delete a file\n",
"!s3cmd del s3://my-bucket-name/myfile.txt\n",
"\n",
"# Delete a bucket\n",
"!s3cmd del --recursive s3://my-bucket-name/\n",
"\n",
"# Create a bucket\n",
"!s3cmd mb s3://my-bucket-name\n",
"\n",
"# List bucket disk usage (human readable)\n",
"!s3cmd du -H s3://my-bucket-name/\n",
"\n",
"# Sync local (source) to s3 bucket (destination)\n",
"!s3cmd sync my-local-folder-path/ s3://my-bucket-name/\n",
"\n",
"# Sync s3 bucket (source) to local (destination)\n",
"!s3cmd sync s3://my-bucket-name/ my-local-folder-path/\n",
"\n",
"# Do a dry-run (do not perform actual sync, but get information about what would happen)\n",
"!s3cmd --dry-run sync s3://my-bucket-name/ my-local-folder-path/\n",
"\n",
"# Apply a standard shell wildcard include to sync s3 bucket (source) to local (destination)\n",
"!s3cmd --include '2014-05-01*' sync s3://my-bucket-name/ my-local-folder-path/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"s3-parallel-put\">s3-parallel-put</h2>\n",
"\n",
"[s3-parallel-put](https://github.com/twpayne/s3-parallel-put.git) is a great tool for uploading multiple files to S3 in parallel."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install package dependencies:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!sudo apt-get install boto\n",
"!sudo apt-get install git"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clone the s3-parallel-put repo:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!git clone https://github.com/twpayne/s3-parallel-put.git"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setup AWS keys for s3-parallel-put:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!export AWS_ACCESS_KEY_ID=XXX\n",
"!export AWS_SECRET_ACCESS_KEY=XXX"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sample usage:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!s3-parallel-put --bucket=bucket --prefix=PREFIX SOURCE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dry run of putting files in the current directory on S3 with the given S3 prefix, do not check first if they exist:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!s3-parallel-put --bucket=bucket --host=s3.amazonaws.com --put=stupid --dry-run --prefix=prefix/ ./"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"s3distcp\">S3DistCp</h2>\n",
"\n",
"[S3DistCp](http://docs.aws.amazon.com/ElasticMapReduce/latest/DeveloperGuide/UsingEMR_s3distcp.html) is an extension of DistCp that is optimized to work with Amazon S3. S3DistCp is useful for combining smaller files and aggregate them together, taking in a pattern and target file to combine smaller input files to larger ones. S3DistCp can also be used to transfer large volumes of data from S3 to your Hadoop cluster."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To run S3DistCp with the EMR command line, ensure you are using the proper version of Ruby:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!rvm --default ruby-1.8.7-p374"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The EMR command line below executes the following:\n",
"* Create a master node and slave nodes of type m1.small\n",
"* Runs S3DistCp on the source bucket location and concatenates files that match the date regular expression, resulting in files that are roughly 1024 MB or 1 GB\n",
"* Places the results in the destination bucket"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!./elastic-mapreduce --create --instance-group master --instance-count 1 \\\n",
"--instance-type m1.small --instance-group core --instance-count 4 \\\n",
"--instance-type m1.small --jar /home/hadoop/lib/emr-s3distcp-1.0.jar \\\n",
"--args \"--src,s3://my-bucket-source/,--groupBy,.*([0-9]{4}-01).*,\\\n",
"--dest,s3://my-bucket-dest/,--targetSize,1024\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For further optimization, compression can be helpful to save on AWS storage and bandwidth costs, to speed up the S3 to/from EMR transfer, and to reduce disk I/O. Note that compressed files are not easy to split for Hadoop. For example, Hadoop uses a single mapper per GZIP file, as it does not know about file boundaries.\n",
"\n",
"What type of compression should you use?\n",
"\n",
"* Time sensitive job: Snappy or LZO\n",
"* Large amounts of data: GZIP\n",
"* General purpose: GZIP, as its supported by most platforms\n",
"\n",
"You can specify the compression codec (gzip, lzo, snappy, or none) to use for copied files with S3DistCp with outputCodec. If no value is specified, files are copied with no compression change. The code below sets the compression to lzo:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"--outputCodec,lzo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"redshift\">Redshift</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copy values from the given S3 location containing CSV files to a Redshift cluster:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"copy table_name from 's3://source/part'\n",
"credentials 'aws_access_key_id=XXX;aws_secret_access_key=XXX'\n",
"csv;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copy values from the given location containing TSV files to a Redshift cluster:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"copy table_name from 's3://source/part'\n",
"credentials 'aws_access_key_id=XXX;aws_secret_access_key=XXX'\n",
"csv delimiter '\\t';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"View Redshift errors:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"select * from stl_load_errors;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vacuum Redshift in full:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"VACUUM FULL;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Analyze the compression of a table:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"analyze compression table_name;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cancel the query with the specified id:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cancel 18764;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The CANCEL command will not abort a transaction. To abort or roll back a transaction, you must use the ABORT or ROLLBACK command. To cancel a query associated with a transaction, first cancel the query then abort the transaction.\n",
"\n",
"If the query that you canceled is associated with a transaction, use the ABORT or ROLLBACK. command to cancel the transaction and discard any changes made to the data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"abort;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reference table creation and setup:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![alt text](http://docs.aws.amazon.com/redshift/latest/dg/images/tutorial-optimize-tables-ssb-data-model.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"CREATE TABLE part (\n",
" p_partkey integer not null sortkey distkey,\n",
" p_name varchar(22) not null,\n",
" p_mfgr varchar(6) not null,\n",
" p_category varchar(7) not null,\n",
" p_brand1 varchar(9) not null,\n",
" p_color varchar(11) not null,\n",
" p_type varchar(25) not null,\n",
" p_size integer not null,\n",
" p_container varchar(10) not null\n",
");\n",
"\n",
"CREATE TABLE supplier (\n",
" s_suppkey integer not null sortkey,\n",
" s_name varchar(25) not null,\n",
" s_address varchar(25) not null,\n",
" s_city varchar(10) not null,\n",
" s_nation varchar(15) not null,\n",
" s_region varchar(12) not null,\n",
" s_phone varchar(15) not null)\n",
"diststyle all;\n",
"\n",
"CREATE TABLE customer (\n",
" c_custkey integer not null sortkey,\n",
" c_name varchar(25) not null,\n",
" c_address varchar(25) not null,\n",
" c_city varchar(10) not null,\n",
" c_nation varchar(15) not null,\n",
" c_region varchar(12) not null,\n",
" c_phone varchar(15) not null,\n",
" c_mktsegment varchar(10) not null)\n",
"diststyle all;\n",
"\n",
"CREATE TABLE dwdate (\n",
" d_datekey integer not null sortkey,\n",
" d_date varchar(19) not null,\n",
" d_dayofweek varchar(10) not null,\n",
" d_month varchar(10) not null,\n",
" d_year integer not null,\n",
" d_yearmonthnum integer not null,\n",
" d_yearmonth varchar(8) not null,\n",
" d_daynuminweek integer not null,\n",
" d_daynuminmonth integer not null,\n",
" d_daynuminyear integer not null,\n",
" d_monthnuminyear integer not null,\n",
" d_weeknuminyear integer not null,\n",
" d_sellingseason varchar(13) not null,\n",
" d_lastdayinweekfl varchar(1) not null,\n",
" d_lastdayinmonthfl varchar(1) not null,\n",
" d_holidayfl varchar(1) not null,\n",
" d_weekdayfl varchar(1) not null)\n",
"diststyle all;\n",
"\n",
"CREATE TABLE lineorder (\n",
" lo_orderkey integer not null,\n",
" lo_linenumber integer not null,\n",
" lo_custkey integer not null,\n",
" lo_partkey integer not null distkey,\n",
" lo_suppkey integer not null,\n",
" lo_orderdate integer not null sortkey,\n",
" lo_orderpriority varchar(15) not null,\n",
" lo_shippriority varchar(1) not null,\n",
" lo_quantity integer not null,\n",
" lo_extendedprice integer not null,\n",
" lo_ordertotalprice integer not null,\n",
" lo_discount integer not null,\n",
" lo_revenue integer not null,\n",
" lo_supplycost integer not null,\n",
" lo_tax integer not null,\n",
" lo_commitdate integer not null,\n",
" lo_shipmode varchar(10) not null\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Table name | Sort Key | Distribution Style |\n",
"|------------|--------------|--------------------|\n",
"| LINEORDER | lo_orderdate | lo_partkey |\n",
"| PART | p_partkey | p_partkey |\n",
"| CUSTOMER | c_custkey | ALL |\n",
"| SUPPLIER | s_suppkey | ALL |\n",
"| DWDATE | d_datekey | ALL |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Sort Keys](http://docs.aws.amazon.com/redshift/latest/dg/tutorial-tuning-tables-sort-keys.html)\n",
"\n",
"When you create a table, you can specify one or more columns as the sort key. Amazon Redshift stores your data on disk in sorted order according to the sort key. How your data is sorted has an important effect on disk I/O, columnar compression, and query performance.\n",
"\n",
"Choose sort keys for based on these best practices:\n",
"\n",
"If recent data is queried most frequently, specify the timestamp column as the leading column for the sort key.\n",
"\n",
"If you do frequent range filtering or equality filtering on one column, specify that column as the sort key.\n",
"\n",
"If you frequently join a (dimension) table, specify the join column as the sort key."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Distribution Styles](http://docs.aws.amazon.com/redshift/latest/dg/c_choosing_dist_sort.html)\n",
"\n",
"When you create a table, you designate one of three distribution styles: KEY, ALL, or EVEN.\n",
"\n",
"**KEY distribution**\n",
"\n",
"The rows are distributed according to the values in one column. The leader node will attempt to place matching values on the same node slice. If you distribute a pair of tables on the joining keys, the leader node collocates the rows on the slices according to the values in the joining columns so that matching values from the common columns are physically stored together.\n",
"\n",
"**ALL distribution**\n",
"\n",
"A copy of the entire table is distributed to every node. Where EVEN distribution or KEY distribution place only a portion of a table's rows on each node, ALL distribution ensures that every row is collocated for every join that the table participates in.\n",
"\n",
"**EVEN distribution**\n",
"\n",
"The rows are distributed across the slices in a round-robin fashion, regardless of the values in any particular column. EVEN distribution is appropriate when a table does not participate in joins or when there is not a clear choice between KEY distribution and ALL distribution. EVEN distribution is the default distribution style."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"kinesis\">Kinesis</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a stream:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws kinesis create-stream --stream-name Foo --shard-count 1 --profile adminuser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List all streams:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws kinesis list-streams --profile adminuser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get info about the stream:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws kinesis describe-stream --stream-name Foo --profile adminuser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Put a record to the stream:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws kinesis put-record --stream-name Foo --data \"SGVsbG8sIHRoaXMgaXMgYSB0ZXN0IDEyMy4=\" --partition-key shardId-000000000000 --region us-east-1 --profile adminuser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get records from a given shard:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!SHARD_ITERATOR=$(aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name Foo --query 'ShardIterator' --profile adminuser)\n",
"aws kinesis get-records --shard-iterator $SHARD_ITERATOR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Delete a stream:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws kinesis delete-stream --stream-name Foo --profile adminuser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"lambda\">Lambda</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List lambda functions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda list-functions \\\n",
" --region us-east-1 \\\n",
" --max-items 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Upload a lambda function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda upload-function \\\n",
" --region us-east-1 \\\n",
" --function-name foo \\\n",
" --function-zip file-path/foo.zip \\\n",
" --role IAM-role-ARN \\\n",
" --mode event \\\n",
" --handler foo.handler \\\n",
" --runtime nodejs \\\n",
" --debug"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Invoke a lambda function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda invoke-async \\\n",
" --function-name foo \\\n",
" --region us-east-1 \\\n",
" --invoke-args foo.txt \\\n",
" --debug"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Return metadata for a specific function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda get-function-configuration \\\n",
" --function-name helloworld \\\n",
" --region us-east-1 \\\n",
" --debug"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Return metadata for a specific function along with a presigned URL that you can use to download the function's .zip file that you uploaded:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda get-function \\\n",
" --function-name helloworld \\\n",
" --region us-east-1 \\\n",
" --debug"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add an event source:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda add-event-source \\\n",
" --region us-east-1 \\\n",
" --function-name ProcessKinesisRecords \\\n",
" --role invocation-role-arn \\\n",
" --event-source kinesis-stream-arn \\\n",
" --batch-size 100 \\\n",
" --profile adminuser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Delete a lambda function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"!aws lambda delete-function \\\n",
" --function-name helloworld \\\n",
" --region us-east-1 \\\n",
" --debug"
]
}
],
"metadata": {
2015-04-16 02:54:26 +08:00
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
2015-04-16 02:54:26 +08:00
},
"nbformat": 4,
"nbformat_minor": 0
}