Skip to content

Instantly share code, notes, and snippets.

@delagoya
Created November 10, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delagoya/4e357dbb3592b914e944618f1f9c9a52 to your computer and use it in GitHub Desktop.
Save delagoya/4e357dbb3592b914e944618f1f9c9a52 to your computer and use it in GitHub Desktop.

Amazon Web Services

Implementing a simple AWS Lambda function

In this lab, we will use the chalice Python serverless web application framework for AWS to create a REST API endpoint to reverse complement a DNA string using AWS Lambda and Amazon API Gateway.

# install chalice
$ pip install chalice

# create a new chalice project
$ chalice new-project revcomp
$ cd revcomp

Inside the new directory you will see two files:

$ tree -a .
.
├── .chalice
│   └── config.json
├── app.py
└── requirements.txt

We can ignore requirements.txt for the moment and concentrate on app.py:

from chalice import Chalice

app = Chalice(app_name='revcomp')


@app.route('/')
def index():
    return {'hello': 'world'}

# ...

First let's add a simple private function to reverse complement a DNA sequence. Here we will assume good input from the user.

def __revcomp(dna):
    complement_dict = {
        "A":"T",
        "T":"A",
        "G":"C",
        "C":"G"
    }
    dna = reversed(dna.upper())
    result = [complement_dict[base] for base in dna]
    return ''.join(result)

Next we will add a new URL with a parameter that takes in a dna sequence and returns the reverse complement using the private function we defined:

@app.route('/revcomp/{dna}')
def revcomp(dna):
    return {"orig": dna, "reverse_complement":__revcomp(dna)}

Let's deploy this app. Make sure you're in the helloworld directory and run chalice deploy:

$ chalice deploy

chalice deploy
Updating lambda function...
Regen deployment package...
Sending changes to lambda.
Lambda deploy done.
API Gateway rest API already found.
Deleting root resource id
Done deleting existing resources.
Deploying to: dev
https://**********.execute-api.us-east-1.amazonaws.com/dev/

TIP: If you get an error from IAM permission please refer to the troubleshooting section below.

Next you can test the API endpoints using cURL

$ curl https://fkvz6mngm8.execute-api.us-east-1.amazonaws.com/dev/
{"hello": "world"
$ curl https://*******.execute-api.us-east-1.amazonaws.com/dev/revcomp/ATGCGCGTATGCGGACGGTA
{"reverse_complement": "TACCGTCCGCATACGCGCAT", "orig": "ATGCGCGTATGCGGACGGTA"}

Cleaning up

You will need to manually delete the create resources, such as the API Gateway and Lambda function(s). Here is a run through using the AWS CLI.

$ aws apigateway get-rest-apis
--------------------------------------------
|                GetRestApis               |
+------------------------------------------+
||                  items                 ||
|+-------------+--------------+-----------+|
|| createdDate |     id       |   name    ||
|+-------------+--------------+-----------+|
||  1474528141 |  abcd2fghj3  |  revcomp  ||
|+-------------+--------------+-----------+|
$ aws apigateway delete-rest-api --rest-api-id abcd2fghj3

$aws lambda list-functions --output json
{
    "Functions": [
        {
            "Version": "$LATEST",
            "CodeSha256": "MCw2Rhh9xafczkt5WfCAj2hSUrj8wKVw1d7htnFPFxc=",
            "FunctionName": "revcomp",
            "MemorySize": 128,
            "CodeSize": 3225199,
            "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:revcomp",
            "Handler": "app.app",
            "Role": "arn:aws:iam::123456789012:role/chalice-logs",
            "Timeout": 60,
            "LastModified": "2016-09-22T07:35:27.271+0000",
            "Runtime": "python2.7",
            "Description": ""
        }
    ]
}
$ aws lambda delete-function --function-name revcomp

Be sure to review your IAM Roles to assess whether they need to also be deleted.

Troubleshooting

By default, chalice assumes your IAM user is able to create API Gateway and Lambda resources, as well as have the ability to list and create a IAM role to use with the deployed application resource, such as the AWS Lambda function. If you are not able to do this, then you will need two items:

  1. an IAM Role like the following:
# arn:aws:iam::123456789012:role/chalice-logs
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*",
      "Effect": "Allow"
    }
  ]
}
  1. the ability to pass this IAM role, among other permissions, such as the PowerUserAccess and IAMReadOnlyAccess managed policies.
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1474528038000",
      "Effect": "Allow",
      "Action": [
        "iam:PassRole"
      ],
      "Resource": [
        "arn:aws:iam::123456789012:role/chalice-logs"
      ]
    }
  ]
}

Once the above are created, you will need to update your chalice configuration file to add in the manage_iam_role and iam_role_arn configuration options:

{
  "manage_iam_role": false,
  "iam_role_arn": "arn:aws:iam::123456789012:role/chalice-logs",
  "app_name": "revcomp",
  "stage": "dev"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment