├── .editorconfig ├── .gitattributes ├── .gitignore ├── .mailmap ├── LICENSE ├── NOTICE ├── README.md ├── _asset ├── arrow-up.png ├── created-by-eawsy.png ├── eawsy-context.png ├── eawsy-errors-output.png ├── eawsy-errors-result.png ├── eawsy-handler.png ├── eawsy-helloworld-access.png ├── eawsy-helloworld-created.png ├── eawsy-helloworld-package.png ├── eawsy-helloworld-results.png ├── eawsy-helloworld-runtime.png ├── eawsy-logo.png ├── eawsy-logs-fatal.png ├── eawsy-logs-panic.png ├── eawsy-logs-standard.png ├── eawsy-preview.png └── powered-by-aws.png ├── _docker ├── Dockerfile └── entrypoint.go └── service └── lambda └── runtime ├── context.go ├── doc.go ├── handler.go ├── proxy.c └── proxy.go /.editorconfig: -------------------------------------------------------------------------------- 1 | root=true 2 | 3 | [*] 4 | charset=utf-8 5 | indent_style=space 6 | indent_size=2 7 | end_of_line=lf 8 | insert_final_newline=true 9 | trim_trailing_whitespace=true 10 | max_line_length=120 11 | 12 | [*.md] 13 | trim_trailing_whitespace=false 14 | 15 | [Makefile] 16 | indent_style=tab 17 | 18 | [*.go] 19 | indent_style=tab -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | 3 | *.md text 4 | *.c text 5 | *.go text 6 | 7 | *.png binary 8 | 9 | .editorconfig text 10 | .gitattributes text 11 | .gitignore text 12 | .mailmap text 13 | Dockerfile text 14 | Makefile text 15 | LICENSE text 16 | NOTICE text 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.so 2 | *.zip 3 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | Farzad Senart 2 | Lionel Suss 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2016 Alsanium, SAS. or its affiliates. All rights reserved. 2 | 3 | This product is licensed to you under the Apache License, Version 2.0 4 | (the "License"); you may not use this product except in compliance with the 5 | License. 6 | 7 | This product may include a number of subcomponents with separate copyright 8 | notices and license terms. Your use of these subcomponents is subject to the 9 | terms and conditions of the subcomponent's license, as noted in the LICENSE 10 | file. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |

THIS PROJECT IS DEPRECATED IN FAVOR OF ITS SUCCESSOR
eawsy/aws-lambda-go-shim



4 | 5 | [Powered by Amazon Web Services][aws-home] 6 | [Created by eawsy][eawsy-home] 7 | 8 | # eawsy/aws-lambda-go 9 | 10 | > A fast and clean way to execute Go on AWS Lambda. 11 | 12 | [![Status][badge-status]](#top) 13 | [![License][badge-license]](LICENSE) 14 | [![Help][badge-help]][eawsy-chat] 15 | [![Social][badge-social]][eawsy-twitter] 16 | 17 | [AWS Lambda][aws-lambda-home] lets you run code without provisioning or managing servers. [For now][aws-lambda-lng], it 18 | only supports Node.js, Java, C# and Python. This project provides Go support *without spawning a process*. 19 | 20 | [](#top) 21 | ## Preview 22 | 23 | ```sh 24 | go get -u -d github.com/eawsy/aws-lambda-go/... 25 | ``` 26 | 27 | ```go 28 | package main 29 | 30 | import ( 31 | "encoding/json" 32 | 33 | "github.com/eawsy/aws-lambda-go/service/lambda/runtime" 34 | ) 35 | 36 | func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) { 37 | return "Hello, World!", nil 38 | } 39 | 40 | func init() { 41 | runtime.HandleFunc(handle) 42 | } 43 | 44 | func main() {} 45 | ``` 46 | 47 | ```sh 48 | docker pull eawsy/aws-lambda-go 49 | docker run --rm -v $GOPATH:/go -v $PWD:/tmp eawsy/aws-lambda-go 50 | ``` 51 | 52 | ```sh 53 | aws lambda create-function \ 54 | --function-name preview-go \ 55 | --runtime python2.7 --handler handler.handle --zip-file fileb://handler.zip \ 56 | --role arn:aws:iam::AWS_ACCOUNT_NUMBER:role/lambda_basic_execution 57 | 58 | aws lambda invoke --function-name preview-go output.txt 59 | ``` 60 | 61 | ![Preview](_asset/eawsy-preview.png) 62 | 63 | [](#top) 64 | ## Documentation 65 | 66 | This [wiki][eawsy-wiki] is the main source of documentation for developers working with or contributing to the 67 | project. 68 | 69 | [](#top) 70 | ## About 71 | 72 | [![eawsy](_asset/eawsy-logo.png)][eawsy-home] 73 | 74 | This project is maintained and funded by Alsanium, SAS. 75 | 76 | [We][eawsy-home] :heart: [AWS][aws-home] and open source software. See [our other projects][eawsy-github], or 77 | [hire us][eawsy-hire-form] to help you build modern applications on AWS. 78 | 79 | [](#top) 80 | ## Contact 81 | 82 | We want to make it easy for you, users and contributers, to talk with us and connect with each others, to share ideas, 83 | solve problems and make help this project awesome. Here are the main channels we're running currently and we'd love to 84 | hear from you on them. 85 | 86 | ### Twitter 87 | 88 | [eawsyhq][eawsy-twitter] 89 | 90 | Follow and chat with us on Twitter. 91 | 92 | Share stories! 93 | 94 | ### Gitter 95 | 96 | [eawsy/bavardage][eawsy-gitter] 97 | 98 | This is for all of you. Users, developers and curious. You can find help, links, questions and answers from all the 99 | community including the core team. 100 | 101 | Ask questions! 102 | 103 | ### GitHub 104 | 105 | [pull requests][eawsy-pr] & [issues][eawsy-issues] 106 | 107 | You are invited to contribute new features, fixes, or updates, large or small; we are always thrilled to receive pull 108 | requests, and do our best to process them as fast as we can. 109 | 110 | Before you start to code, we recommend discussing your plans through the [eawsy/bavardage channel][eawsy-gitter], 111 | especially for more ambitious contributions. This gives other contributors a chance to point you in the right direction, 112 | give you feedback on your design, and help you find out if someone else is working on the same thing. 113 | 114 | Write code! 115 | 116 | [](#top) 117 | ## License 118 | 119 | This product is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this product 120 | except in compliance with the License. See [LICENSE](LICENSE) and [NOTICE](NOTICE) for more information. 121 | 122 | [](#top) 123 | ## Trademark 124 | 125 | Alsanium, eawsy, the "Created by eawsy" logo, and the "eawsy" logo are trademarks of Alsanium, SAS. or its affiliates in 126 | France and/or other countries. 127 | 128 | Amazon Web Services, the "Powered by Amazon Web Services" logo, and AWS Lambda are trademarks of Amazon.com, Inc. or its 129 | affiliates in the United States and/or other countries. 130 | 131 | [eawsy-home]: https://eawsy.com 132 | [eawsy-github]: https://github.com/eawsy 133 | [eawsy-chat]: https://gitter.im/eawsy/bavardage 134 | [eawsy-twitter]: https://twitter.com/eawsyhq 135 | [eawsy-godoc]: https://godoc.org/github.com/eawsy/aws-lambda-go/service/lambda/runtime 136 | [eawsy-wiki]: https://github.com/eawsy/aws-lambda-go/wiki 137 | [eawsy-hire-form]: https://docs.google.com/forms/d/e/1FAIpQLSfPvn1Dgp95DXfvr3ClPHCNF5abi4D1grveT5btVyBHUk0nXw/viewform 138 | [eawsy-pr]: https://github.com/eawsy/aws-lambda-go/issues?q=is:pr%20is:open 139 | [eawsy-issues]: https://github.com/eawsy/aws-lambda-go/issues?q=is:issue%20is:open 140 | [eawsy-twitter]: https://twitter.com/@eawsyhq 141 | [aws-home]: https://aws.amazon.com/ 142 | [aws-lambda-home]: https://aws.amazon.com/lambda/ 143 | [aws-lambda-lng]: http://docs.aws.amazon.com/lambda/latest/dg/lambda-app.html#lambda-app-author 144 | [badge-status]: http://img.shields.io/badge/status-stable-4CAF50.svg?style=flat-square 145 | [badge-license]: http://img.shields.io/badge/license-apache-FF5722.svg?style=flat-square 146 | [badge-help]: http://img.shields.io/badge/help-gitter-E91E63.svg?style=flat-square 147 | [badge-social]: http://img.shields.io/badge/social-twitter-03A9F4.svg?style=flat-square 148 | -------------------------------------------------------------------------------- /_asset/arrow-up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/arrow-up.png -------------------------------------------------------------------------------- /_asset/created-by-eawsy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/created-by-eawsy.png -------------------------------------------------------------------------------- /_asset/eawsy-context.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-context.png -------------------------------------------------------------------------------- /_asset/eawsy-errors-output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-errors-output.png -------------------------------------------------------------------------------- /_asset/eawsy-errors-result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-errors-result.png -------------------------------------------------------------------------------- /_asset/eawsy-handler.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-handler.png -------------------------------------------------------------------------------- /_asset/eawsy-helloworld-access.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-helloworld-access.png -------------------------------------------------------------------------------- /_asset/eawsy-helloworld-created.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-helloworld-created.png -------------------------------------------------------------------------------- /_asset/eawsy-helloworld-package.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-helloworld-package.png -------------------------------------------------------------------------------- /_asset/eawsy-helloworld-results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-helloworld-results.png -------------------------------------------------------------------------------- /_asset/eawsy-helloworld-runtime.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-helloworld-runtime.png -------------------------------------------------------------------------------- /_asset/eawsy-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-logo.png -------------------------------------------------------------------------------- /_asset/eawsy-logs-fatal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-logs-fatal.png -------------------------------------------------------------------------------- /_asset/eawsy-logs-panic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-logs-panic.png -------------------------------------------------------------------------------- /_asset/eawsy-logs-standard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-logs-standard.png -------------------------------------------------------------------------------- /_asset/eawsy-preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/eawsy-preview.png -------------------------------------------------------------------------------- /_asset/powered-by-aws.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifadev-archive/aws-lambda-go/d67ea54b7d71725c02ff43f2253ac991ed45cdd8/_asset/powered-by-aws.png -------------------------------------------------------------------------------- /_docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:latest 2 | 3 | RUN \ 4 | apt-get update \ 5 | && apt-get install -y --no-install-recommends pkg-config python-dev zip \ 6 | && rm -rf /var/lib/apt/lists/* 7 | 8 | WORKDIR /tmp 9 | 10 | ADD entrypoint /usr/local/bin/entrypoint 11 | 12 | ENTRYPOINT ["/usr/local/bin/entrypoint"] -------------------------------------------------------------------------------- /_docker/entrypoint.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "os/exec" 8 | "syscall" 9 | ) 10 | 11 | func main() { 12 | var f, h, p string 13 | var n bool 14 | 15 | flag.StringVar(&f, "function", "handler", "AWS Lambda function name") 16 | flag.StringVar(&h, "handler", "handle", "AWS Lambda handler name") 17 | flag.StringVar(&p, "package", "handler.zip", "AWS Lambda package name") 18 | flag.BoolVar(&n, "nopackage", false, "Only build and do not create AWS Lambda package") 19 | 20 | flag.Parse() 21 | 22 | if len(flag.Args()) > 0 { 23 | flag.Usage() 24 | } 25 | 26 | info, err := os.Stat(".") 27 | if err != nil { 28 | fmt.Println(err) 29 | os.Exit(1) 30 | } 31 | uid := int(info.Sys().(*syscall.Stat_t).Uid) 32 | gid := int(info.Sys().(*syscall.Stat_t).Gid) 33 | 34 | so := fmt.Sprintf("%s.so", f) 35 | 36 | os.RemoveAll(so) 37 | 38 | cmd := exec.Command("go", "build", "-buildmode=c-shared", "-ldflags=-w -s", "-tags", "proxy", "-o", so) 39 | env := os.Environ() 40 | env = append(env, fmt.Sprintf("CGO_CFLAGS=-DFUNCTION=%s -DHANDLER=%s", f, h)) 41 | cmd.Env = env 42 | cmd.Stdout = os.Stdout 43 | cmd.Stderr = os.Stderr 44 | if err := cmd.Run(); err != nil { 45 | fmt.Println(err) 46 | os.Exit(1) 47 | } 48 | 49 | os.Chown(so, uid, gid) 50 | 51 | if n { 52 | return 53 | } 54 | 55 | os.RemoveAll(p) 56 | 57 | cmd = exec.Command("zip", "-q", "-9", p, so) 58 | cmd.Stdout = os.Stdout 59 | cmd.Stderr = os.Stderr 60 | if err := cmd.Run(); err != nil { 61 | fmt.Println(err) 62 | os.Exit(1) 63 | } 64 | 65 | os.Chown(p, uid, gid) 66 | 67 | os.RemoveAll(so) 68 | } 69 | -------------------------------------------------------------------------------- /service/lambda/runtime/context.go: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2016 Alsanium, SAS. or its affiliates. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | package runtime 18 | 19 | // CognitoIdentity provides information about the Amazon Cognito identity 20 | // provider when Lambda function invoked through the AWS Mobile SDK. 21 | // 22 | // For more information about the exact values for a specific mobile platform, 23 | // see Identity Context in the AWS Mobile SDK for iOS Developer Guide, and 24 | // Identity Context in the AWS Mobile SDK for Android Developer Guide. 25 | // 26 | // http://aws.amazon.com/cognito/ 27 | // http://docs.aws.amazon.com/mobile/sdkforios/developerguide/lambda.html#identitycontext 28 | // http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/lambda.html#identity-context 29 | type CognitoIdentity struct { 30 | // Amazon Cognito identity ID 31 | IdentityID string `json:"cognito_identity_id"` 32 | 33 | // Amazon Cognito identity pool ID 34 | IdentityPoolID string `json:"cognito_identity_pool_id"` 35 | } 36 | 37 | // Client provides information about the client application when Lambda function 38 | // invoked through the AWS Mobile SDK. 39 | // 40 | // For more information about the exact values for a specific mobile platform, 41 | // see Client Context in the AWS Mobile SDK for iOS Developer Guide, and Client 42 | // Context in the AWS Mobile SDK for Android Developer Guide. 43 | // 44 | // http://docs.aws.amazon.com/mobile/sdkforios/developerguide/lambda.html#clientcontext 45 | // http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/lambda.html#client-context 46 | type Client struct { 47 | InstallationID string `json:"installation_id"` 48 | AppTitle string `json:"app_title"` 49 | AppVersionName string `json:"app_version_name"` 50 | AppVersionCode string `json:"app_version_code"` 51 | AppPackageName string `json:"app_package_name"` 52 | } 53 | 54 | // ClientContext provides information about the client application and device 55 | // when Lambda function invoked through the AWS Mobile SDK. 56 | // 57 | // For more information about the exact values for a specific mobile platform, 58 | // see Client Context in the AWS Mobile SDK for iOS Developer Guide, and Client 59 | // Context in the AWS Mobile SDK for Android Developer Guide. 60 | // 61 | // http://docs.aws.amazon.com/mobile/sdkforios/developerguide/lambda.html#clientcontext 62 | // http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/lambda.html#client-context 63 | type ClientContext struct { 64 | // Client information provided by AWS Mobile SDK. 65 | Client *Client `json:"client,omitempty"` 66 | 67 | // Custom values set by mobile client application. 68 | Custom map[string]string `json:"custom"` 69 | 70 | // Environment information provided by AWS Mobile SDK. 71 | Environment map[string]string `json:"env"` 72 | } 73 | 74 | // Context provides information about Lambda execution environment. 75 | // 76 | // For example, you can use the Context to determine the 77 | // CloudWatch log stream associated with the function or use the ClientContext 78 | // property of the Context to learn more about the application calling the 79 | // Lambda function (when invoked through the AWS Mobile SDK). 80 | type Context struct { 81 | // The name of the Lambda function that is executing. 82 | FunctionName string `json:"function_name"` 83 | 84 | // The version of the Lambda function that is executing. 85 | // If an alias is used to invoke the function, then FunctionVersion will be 86 | // the version the alias points to. 87 | FunctionVersion string `json:"function_version"` 88 | 89 | // The ARN used to invoke the Lambda function. 90 | // It can be function ARN or alias ARN. An unqualified ARN executes the 91 | // $LATEST version and aliases execute the function version they are pointing 92 | // to. 93 | InvokedFunctionARN string `json:"invoked_function_arn"` 94 | 95 | // The Memory limit, in MB, configured for the Lambda function. 96 | MemoryLimitInMB int `json:"memory_limit_in_mb,string"` 97 | 98 | // The AWS request ID associated with the request. 99 | // This is the ID returned to the client invoked the function. The request ID 100 | // can be used for any follow up enquiry with AWS support. Note that if Lambda 101 | // retries the function (for example, in a situation where the Lambda function 102 | // processing Amazon Kinesis records throw an exception), the request ID 103 | // remains the same. 104 | AWSRequestID string `json:"aws_request_id"` 105 | 106 | // The CloudWatch log group name of the Lambda function that is executing. 107 | // It can be empty if the IAM user provided does not have permission for 108 | // CloudWatch actions. 109 | LogGroupName string `json:"log_group_name"` 110 | 111 | // The CloudWatch log stream name of the Lambda function that is executing. 112 | // It can be empty if the IAM user provided does not have permission for 113 | // CloudWatch actions. 114 | LogStreamName string `json:"log_stream_name"` 115 | 116 | // The information about the Amazon Cognito identity provider when the Lambda 117 | // function invoked through the AWS Mobile SDK. 118 | // It can be nil. 119 | Identity *CognitoIdentity `json:"identity,omitempty"` 120 | 121 | // The information about the client application and device when the Lambda 122 | // function invoked through the AWS Mobile SDK. 123 | // It can be nil. 124 | ClientContext *ClientContext `json:"client_context,omitempty"` 125 | 126 | // RemainingTimeInMillis returns remaining execution time till the function 127 | // will be terminated, in milliseconds. 128 | // 129 | // The maximum time limit at which Lambda will terminate the function 130 | // execution is set at the time the Lambda function is created. Information 131 | // about the remaining time of function execution can be used to specify 132 | // function behavior when nearing the timeout. 133 | RemainingTimeInMillis func() int64 134 | } 135 | -------------------------------------------------------------------------------- /service/lambda/runtime/doc.go: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2016 Alsanium, SAS. or its affiliates. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | /* 18 | Package runtime provides a fast and clean way to execute Go on AWS Lambda. 19 | 20 | Take a tour at https://github.com/eawsy/aws-lambda-go 21 | */ 22 | package runtime 23 | -------------------------------------------------------------------------------- /service/lambda/runtime/handler.go: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2016 Alsanium, SAS. or its affiliates. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | package runtime 18 | 19 | import "encoding/json" 20 | 21 | var handler Handler 22 | 23 | // Handler responds to a Lambda function invocation. 24 | // 25 | // HandleLambda can optionally return a value or an error. What happens to the 26 | // returned value depends on the invocation type used when invoking the Lambda 27 | // function. If the Lambda function returns an error, AWS Lambda recognizes the 28 | // failure and sealizes the error information into JSON and returns it. How to 29 | // get the error information back depends to the invocation type that client 30 | // specifies at the time of function invocation. 31 | // 32 | // If HandleLambda panics, the handler (the caller of HandleLambda) recovers the 33 | // panic, logs a stack trace to the CloudWatch log stream, and terminate the 34 | // Lambda function execution. 35 | type Handler interface { 36 | HandleLambda(json.RawMessage, *Context) (interface{}, error) 37 | } 38 | 39 | // HandlerFunc type is an adapter to allow the use of ordinary functions as 40 | // Lambda handlers. If f is a function with the appropriate signature, 41 | // HandlerFunc(f) is a Handler that calls f. 42 | type HandlerFunc func(json.RawMessage, *Context) (interface{}, error) 43 | 44 | // HandleLambda calls f(evt, ctx) 45 | func (f HandlerFunc) HandleLambda(evt json.RawMessage, ctx *Context) (interface{}, error) { 46 | return f(evt, ctx) 47 | } 48 | 49 | // Handle registers the given handler. 50 | func Handle(h Handler) { 51 | handler = h 52 | } 53 | 54 | // HandleFunc registers the given handler function. 55 | func HandleFunc(h func(json.RawMessage, *Context) (interface{}, error)) { 56 | Handle(HandlerFunc(h)) 57 | } 58 | -------------------------------------------------------------------------------- /service/lambda/runtime/proxy.c: -------------------------------------------------------------------------------- 1 | // +build proxy 2 | 3 | // 4 | // Copyright 2016 Alsanium, SAS. or its affiliates. All rights reserved. 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | 19 | #include 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | struct handle_return { char* r0; char* r1; }; 27 | extern struct handle_return handle(char*, char*, char*); 28 | 29 | static PyObject *module_error = NULL; 30 | static PyObject* proxy_log_fn = NULL; 31 | static PyObject* proxy_get_remaining_time_in_millis_fn = NULL; 32 | 33 | void 34 | proxy_log(char* msg) 35 | { 36 | PyObject* tmp = PyObject_CallFunction(proxy_log_fn, "s", msg); 37 | Py_DECREF(tmp); 38 | free(msg); 39 | } 40 | 41 | long long 42 | proxy_get_remaining_time_in_millis() 43 | { 44 | PyObject* pms = PyObject_CallFunctionObjArgs(proxy_get_remaining_time_in_millis_fn, NULL); 45 | unsigned long ms = PyLong_AsLongLong(pms); 46 | Py_DECREF(pms); 47 | return ms; 48 | } 49 | 50 | static PyObject* py_json_module; 51 | static PyObject* py_os_module; 52 | 53 | static void 54 | PyDict_Free(PyObject* p) 55 | { 56 | PyObject* es = PyDict_Values(p); 57 | for (Py_ssize_t i = 0; i < PyList_Size(es); i++) { 58 | PyObject* e = PyList_GetItem(es, i); 59 | if (PyDict_Check(e)) { 60 | PyDict_Free(e); 61 | } else { 62 | Py_DECREF(e); 63 | } 64 | } 65 | Py_DECREF(es); 66 | Py_DECREF(p); 67 | } 68 | 69 | static void 70 | PyDict_SetSomeItemString(PyObject* p, char *key, PyObject* val) 71 | { 72 | if ((val != Py_None) && (!PyDict_Check(val) || (PyDict_Check(val) && PyDict_Size(val)))) { 73 | PyDict_SetItemString(p, key, val); 74 | } else { 75 | Py_DECREF(val); 76 | } 77 | } 78 | 79 | static void 80 | PyDict_CopyItemString(PyObject* p1, PyObject* p2, char *key) 81 | { 82 | PyDict_SetSomeItemString(p1, key, PyObject_GetAttrString(p2, key)); 83 | } 84 | 85 | static PyObject* 86 | proxy_marshal_ctx(PyObject* raw) 87 | { 88 | PyObject* ctx = PyDict_New(); 89 | 90 | PyDict_CopyItemString(ctx, raw, "function_name"); 91 | PyDict_CopyItemString(ctx, raw, "function_version"); 92 | PyDict_CopyItemString(ctx, raw, "invoked_function_arn"); 93 | PyDict_CopyItemString(ctx, raw, "memory_limit_in_mb"); 94 | PyDict_CopyItemString(ctx, raw, "aws_request_id"); 95 | PyDict_CopyItemString(ctx, raw, "log_group_name"); 96 | PyDict_CopyItemString(ctx, raw, "log_stream_name"); 97 | 98 | PyObject* raw_identity = PyObject_GetAttrString(raw, "identity"); 99 | PyObject* identity = PyDict_New(); 100 | if (raw_identity != Py_None) { 101 | PyDict_CopyItemString(identity, raw_identity, "cognito_identity_id"); 102 | PyDict_CopyItemString(identity, raw_identity, "cognito_identity_pool_id"); 103 | } 104 | PyDict_SetSomeItemString(ctx, "identity", identity); 105 | Py_DECREF(raw_identity); 106 | 107 | PyObject* raw_client_context = PyObject_GetAttrString(raw, "client_context"); 108 | PyObject* client_context = PyDict_New(); 109 | if (raw_client_context != Py_None) { 110 | PyObject* raw_client = PyObject_GetAttrString(raw_client_context, "client"); 111 | PyObject* client = PyDict_New(); 112 | if (raw_client != Py_None) { 113 | PyDict_CopyItemString(client, raw_client, "installation_id"); 114 | PyDict_CopyItemString(client, raw_client, "app_title"); 115 | PyDict_CopyItemString(client, raw_client, "app_version_name"); 116 | PyDict_CopyItemString(client, raw_client, "app_version_code"); 117 | PyDict_CopyItemString(client, raw_client, "app_package_name"); 118 | } 119 | PyDict_SetSomeItemString(client_context, "client", client); 120 | Py_DECREF(raw_client); 121 | 122 | PyDict_CopyItemString(client_context, raw_client_context, "env"); 123 | PyDict_CopyItemString(client_context, raw_client_context, "custom"); 124 | } 125 | PyDict_SetSomeItemString(ctx, "client_context", client_context); 126 | Py_DECREF(raw_client_context); 127 | 128 | PyObject* str = PyObject_CallMethod(py_json_module, "dumps", "O", ctx); 129 | 130 | PyDict_Free(ctx); 131 | 132 | return str; 133 | } 134 | 135 | static PyObject* 136 | proxy_marshal_env() 137 | { 138 | PyObject* environ = PyObject_GetAttrString(py_os_module, "environ"); 139 | PyObject* dict = PyObject_GetAttrString(environ, "__dict__"); 140 | PyObject* env = PyDict_GetItemString(dict, "data"); 141 | 142 | PyObject* str = PyObject_CallMethod(py_json_module, "dumps", "O", env); 143 | 144 | Py_DECREF(environ); 145 | Py_DECREF(dict); 146 | 147 | return str; 148 | } 149 | 150 | static PyObject* 151 | proxy_handle(PyObject* self, PyObject* args) 152 | { 153 | PyObject *west = NULL; 154 | PyObject *evt = NULL; 155 | PyObject *ctx = NULL; 156 | 157 | PyArg_ParseTuple(args, "OO", &evt, &ctx); 158 | 159 | proxy_log_fn = PyObject_GetAttrString(ctx, "log"); 160 | proxy_get_remaining_time_in_millis_fn = PyObject_GetAttrString(ctx, "get_remaining_time_in_millis"); 161 | 162 | PyObject* jevt = PyObject_CallMethod(py_json_module, "dumps", "O", evt); 163 | PyObject* jctx = proxy_marshal_ctx(ctx); 164 | PyObject* jenv = proxy_marshal_env(); 165 | 166 | char* sevt = PyString_AsString(jevt); 167 | char* sctx = PyString_AsString(jctx); 168 | char* senv = PyString_AsString(jenv); 169 | 170 | struct handle_return east = handle(sevt, sctx, senv); 171 | 172 | if (east.r0 != NULL) { 173 | PyObject* tmp = PyString_FromString(east.r0); 174 | west = PyObject_CallMethod(py_json_module, "loads", "O", tmp); 175 | Py_DECREF(tmp); 176 | free(east.r0); 177 | } else if (east.r1 != NULL) { 178 | PyErr_SetString(module_error, east.r1); 179 | free(east.r1); 180 | west = NULL; 181 | } else { 182 | Py_INCREF(Py_None); 183 | west = Py_None; 184 | } 185 | 186 | Py_DECREF(proxy_log_fn); 187 | Py_DECREF(proxy_get_remaining_time_in_millis_fn); 188 | Py_DECREF(jevt); 189 | Py_DECREF(jctx); 190 | Py_DECREF(jenv); 191 | 192 | return west; 193 | } 194 | 195 | #define STR(s) #s 196 | #define XSTR(s) STR(s) 197 | #define INIT_MODULE_SIG(name) PyMODINIT_FUNC init ## name (void) 198 | #define INIT_MODULE(name) INIT_MODULE_SIG(name) 199 | 200 | #ifndef FUNCTION 201 | #define FUNCTION handler 202 | #endif 203 | 204 | #ifndef HANDLER 205 | #define HANDLER handle 206 | #endif 207 | 208 | static PyMethodDef 209 | module_methods[] = { 210 | {XSTR(HANDLER), (PyCFunction)proxy_handle, METH_VARARGS}, 211 | {NULL, NULL} 212 | }; 213 | 214 | INIT_MODULE(FUNCTION) 215 | { 216 | py_json_module = PyImport_ImportModule("json"); 217 | py_os_module = PyImport_ImportModule("os"); 218 | PyObject *module = Py_InitModule(XSTR(FUNCTION), module_methods); 219 | module_error = PyErr_NewException(XSTR(FUNCTION)".error", NULL, NULL); 220 | Py_INCREF(module_error); 221 | PyModule_AddObject(module, "error", module_error); 222 | } 223 | 224 | #ifdef __cplusplus 225 | } 226 | #endif 227 | -------------------------------------------------------------------------------- /service/lambda/runtime/proxy.go: -------------------------------------------------------------------------------- 1 | // +build proxy 2 | 3 | // 4 | // Copyright 2016 Alsanium, SAS. or its affiliates. All rights reserved. 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | 19 | package runtime 20 | 21 | // #cgo pkg-config: python2 22 | // #cgo CFLAGS: --std=gnu11 23 | // extern long long proxy_get_remaining_time_in_millis(); 24 | // extern void proxy_log(char*); 25 | import "C" 26 | 27 | import ( 28 | "encoding/json" 29 | "fmt" 30 | "log" 31 | "os" 32 | "runtime" 33 | "time" 34 | ) 35 | 36 | var logger *ilogger 37 | 38 | type ilogger struct { 39 | id string 40 | } 41 | 42 | func (l *ilogger) Write(info []byte) (int, error) { 43 | now := time.Now().UTC().Format("2006-01-02T15:04:05.999Z") 44 | C.proxy_log(C.CString(fmt.Sprintf("%s\t%s\t%s", now, l.id, string(info)))) 45 | return len(info), nil 46 | } 47 | 48 | //export handle 49 | func handle(revt, rctx, renv *C.char) (rres *C.char, rerr *C.char) { 50 | defer func() { 51 | if err := recover(); err != nil { 52 | const size = 64 << 10 53 | buf := make([]byte, size) 54 | buf = buf[:runtime.Stack(buf, false)] 55 | log.Printf("%s\n%s", err, buf) 56 | rres = nil 57 | rerr = C.CString(fmt.Sprintf("%s", err)) 58 | } 59 | }() 60 | 61 | evt := json.RawMessage([]byte(C.GoString(revt))) 62 | ctx := &Context{} 63 | 64 | if err := json.Unmarshal([]byte(C.GoString(rctx)), ctx); err != nil { 65 | return nil, C.CString(err.Error()) 66 | } 67 | 68 | ctx.RemainingTimeInMillis = func() int64 { 69 | return int64(C.proxy_get_remaining_time_in_millis()) 70 | } 71 | 72 | logger.id = ctx.AWSRequestID 73 | 74 | var env map[string]string 75 | if err := json.Unmarshal([]byte(C.GoString(renv)), &env); err != nil { 76 | return nil, C.CString(err.Error()) 77 | } 78 | for k, v := range env { 79 | os.Setenv(k, v) 80 | } 81 | 82 | if res, err := handler.HandleLambda(evt, ctx); err != nil { 83 | return nil, C.CString(err.Error()) 84 | } else if res != nil { 85 | tmp, err := json.Marshal(res) 86 | if err != nil { 87 | return nil, C.CString(err.Error()) 88 | } 89 | return C.CString(string(tmp)), nil 90 | } 91 | return nil, nil 92 | } 93 | 94 | func init() { 95 | logger = &ilogger{"00000000-0000-0000-0000-000000000000"} 96 | log.SetFlags(0) 97 | log.SetOutput(logger) 98 | } 99 | --------------------------------------------------------------------------------