github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/docs/lambda/getting-started.md (about) 1 # Introduction 2 3 This guide will walk you through creating and testing a simple Lambda function. 4 5 We need the the `fn` tool for the rest of this guide. You can install it 6 by following [these instructions](https://github.com/iron-io/function/fn). 7 8 *For this getting started we are assuming you already have working lambda function code available, if not head to the [import instructions] (import.md) and skip the next section.* 9 10 ## Creating the function 11 12 Let's convert the `hello_world` AWS Lambda example to Docker. 13 14 ```python 15 def my_handler(event, context): 16 message = 'Hello {} {}!'.format(event['first_name'], 17 event['last_name']) 18 return { 19 'message' : message 20 } 21 ``` 22 23 Create an empty directory for your project and save this code in a file called 24 `hello_world.py`. 25 26 Now let's use `fn`'s Lambda functionality to create a Docker image. We can 27 then run the Docker image with a payload to execute the Lambda function. 28 29 ```sh 30 $ fn lambda create-function irontest/hello_world:1 python2.7 hello_world.my_handler hello_world.py 31 Creating directory: irontest/hello_world:1 ... OK 32 Creating Dockerfile: irontest/hello_world:1/Dockerfile ... OK 33 Copying file: irontest/hello_world/hello_world:1.py ... OK 34 Creating function.yaml ... OK 35 ``` 36 37 As you can see, this is very similar to creating a Lambda function using the 38 `aws` CLI tool. We name the function as we would name other Docker images. The 39 `1` indicates the version. You can use any string. This way you can configure 40 your deployment environment to use different versions. The handler is 41 the name of the function to run, in the form that python expects 42 (`module.function`). Where you would package the files into a `.zip` to upload 43 to Lambda, we just pass the list of files to `fn`. 44 45 ## Deploying the function to IronFunctions 46 47 Next we want to deploy the function to our IronFunctions 48 ```sh 49 $ fn deploy -v -d ./irontest irontest 50 deploying irontest/hello_world:1/function.yaml 51 Sending build context to Docker daemon 4.096 kB 52 Step 1 : FROM iron/lambda-python2.7 53 latest: Pulling from iron/lambda-python2.7 54 c52e3ed763ff: Pull complete 55 789cf808491a: Pull complete 56 d1b635efed57: Pull complete 57 fe23c3dbcfa8: Pull complete 58 63c874a9687e: Pull complete 59 a6d462dae1df: Pull complete 60 Digest: sha256:c5dde3bf3be776c0f6b909d4ad87255a0af9b6696831fbe17c5f659655a0494a 61 Status: Downloaded newer image for iron/lambda-python2.7:latest 62 ---> 66d3adf47835 63 Step 2 : ADD hello_world.py ./hello_world:1.py 64 ---> 91a592e0dfa9 65 Removing intermediate container 1a1ef40ff0dd 66 Step 3 : CMD hello_world.my_handler 67 ---> Running in 318da1bba060 68 ---> db9b9644168e 69 Removing intermediate container 318da1bba060 70 Successfully built db9b9644168e 71 The push refers to a repository [docker.io/irontest/hello_world:1] 72 5d9d142e21b2: Pushed 73 11d8145d6038: Layer already exists 74 23885f85dbd0: Layer already exists 75 6a350a8d14ee: Layer already exists 76 e67f7ef625c5: Layer already exists 77 321db514ef85: Layer already exists 78 6102f0d2ad33: Layer already exists 79 latest: digest: sha256:5926ff413f134fa353e4b42f2d4a0d2d4f5b3a39489cfdf6dd5b4a63c4e40dee size: 1784 80 updating API with appName: irontest route: /hello_world:1 image: irontest/hello_world:1 81 path result 82 irontest/hello_world:1/function.yaml done 83 ``` 84 85 This will deploy the generated function under the app `irontest` with `hello_world` as a route, e.g: 86 `http://<hostname>/r/irontest/hello_world:1`, 87 88 You should also now see the generated Docker image. 89 90 ```sh 91 $ docker images 92 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 93 irontest/hello_world:1 latest db9b9644168e About a minute ago 108.4 MB 94 ... 95 ``` 96 97 ## Testing the function 98 99 The `test-function` subcommand can launch the Dockerized function with the 100 right parameters. 101 102 ```sh 103 $ fn lambda test-function irontest/hello_world:1 --payload '{ "first_name": "Jon", "last_name": "Snow" }' 104 {"message": "Hello Jon Snow!"} 105 ``` 106 107 You should see the output. 108 109 ## Calling the function from IronFunctions 110 111 The `fn call` command can call the deployed version with a given payload. 112 113 ```sh 114 $ echo '{ "first_name": "Jon", "last_name": "Snow" }' | ./fn call irontest /hello_world:1 115 {"message": "Hello Jon Snow!"} 116 ``` 117 118 You should see the output. 119 120 121 ## Commands documentation 122 * [create-function](create.md) 123 * [test-function](test.md) 124 * [aws-import](import.md) 125 126 ## More documentation 127 * [env](environment.md) 128 * [aws](aws.md)