This folder shows an example of how to package code with dependencies and build steps and then deploy it as a Lambda
function using the lambda module. The actual Python code in index.py is a simple example that uses an external
library called requests to make HTTP requests and returns the results.
With AWS Lambda, your deployment package
(zip file) must contain ALL of the dependencies for your app already bundled within it. Moreover, since Lambda
functions run on Amazon Linux, all of those dependencies must be compiled specifically for Amazon Linux. This example
creates the deployment package as follows:
Build the code using Docker, using an Amazon Linux
image as the base image,
as shown in the example Dockerfile. This Docker image installs all of your dependencies and
source code into the /usr/src/lambda folder.
When developing and testing locally, you can run your Lambda code directly in the Docker image. You can
mount the
python/src directory from your host OS into /usr/src/lambda/src so that your local changes are visible
immediately in the container.
To deploy to AWS, you use docker cp to copy the /usr/src/lambda folder to a local path (see the build.sh
script)and then run terraform apply to zip up that local path and deploy it to AWS.
Open up the AWS Console UI, find the function, click the "Test" button,
and enter test data that looks something like this:
{
"url": "http://www.example.com"
}
Click "Save and test" and AWS will show you the log output and returned value in the browser.
Test locally
The code you write for a Lambda function is just regular code with a well-defined entrypoint (the "handler"), so you
can also run it locally by calling that entrypoint. The example Python app includes a test_harness.py file that is
configured to allow you to run your code locally. This test harness script is configured as the ENTRYPOINT for the
Docker container, so you can test locally as follows:
cd python
docker build -t lambda-build-example .
docker run -it --rm lambda-build-example http://www.example.com
To avoid having to do a docker build every time, you can do all subsequent docker run calls with your local src
folder mounted as a volume so that the Docker container always sees your latest source code:
docker run -it --rm -v src:/usr/src/lambda/src lambda-build-example http://www.example.com
Questions? Ask away.
We're here to talk about our services, answer any questions, give advice, or just to chat.
{"treedata":{"name":"root","toggled":true,"children":[{"name":".circleci","children":[{"name":"config.yml","path":".circleci/config.yml","sha":"9f1abdb0944959c44bbf482e151316c61bc7d145"}]},{"name":".gitignore","path":".gitignore","sha":"8a502c2bba2544ba8beae9a45fde2398da238cd7"},{"name":".pre-commit-config.yaml","path":".pre-commit-config.yaml","sha":"49ee828ed16f55335ac4dcc74331f190366b1858"},{"name":"CODEOWNERS","path":"CODEOWNERS","sha":"555c0c6e23a7502acbef94fb0b77bfa759ba11e8"},{"name":"LICENSE.txt","path":"LICENSE.txt","sha":"f4e3d9bd4717a044ed31ad847a300eee74371a78"},{"name":"README.md","path":"README.md","sha":"92ccefcdcf95442db0388cf7b9431dccd468a9c7"},{"name":"_docs","children":[{"name":"serverless-architecture.png","path":"_docs/serverless-architecture.png","sha":"b08ab629692424a68c403f89e055e9a1d21d05b5"}]},{"name":"examples","children":[{"name":"api-gateway-account-settings","children":[{"name":"README.md","path":"examples/api-gateway-account-settings/README.md","sha":"19c6f4fce86a3712b3bb3953391af98fe3ebeead"},{"name":"main.tf","path":"examples/api-gateway-account-settings/main.tf","sha":"5ca12622143707475cba6360ccd724b5f504bf73"},{"name":"outputs.tf","path":"examples/api-gateway-account-settings/outputs.tf","sha":"0c0df614d567c6467f57640f9b12d03dbe01ace0"},{"name":"variables.tf","path":"examples/api-gateway-account-settings/variables.tf","sha":"361234e5be3ef39caad0ec4043b34871d9bf28f7"}]},{"name":"lambda-build","children":[{"name":"README.md","path":"examples/lambda-build/README.md","sha":"922fd81fe36879d68d3b4d850ae131100590379c","toggled":true},{"name":"main.tf","path":"examples/lambda-build/main.tf","sha":"50fb1885ba61f8c08b5f121675573fd735da2573"},{"name":"outputs.tf","path":"examples/lambda-build/outputs.tf","sha":"28fc5ab8cc109b761b4697e68067f4eefc6ee0dd"},{"name":"python","children":[{"name":"Dockerfile","path":"examples/lambda-build/python/Dockerfile","sha":"9c7e1e7a83f4d13fc7ac75fc45912ad6f6f8eaa8"},{"name":"build.sh","path":"examples/lambda-build/python/build.sh","sha":"bf53f2bdde463e8bd2f1a4f93009ecb1c6c0c206"},{"name":"requirements.txt","path":"examples/lambda-build/python/requirements.txt","sha":"2185f65eb08d376b2abe2c7afe55b34189eb2261"},{"name":"src","children":[{"name":"__init.py__","path":"examples/lambda-build/python/src/__init.py__","sha":"1b58820b01935d58928ad905427bd838c5e80123"},{"name":"index.py","path":"examples/lambda-build/python/src/index.py","sha":"d0233c21dc7572caead57e7e60c8d75a13312ea0"},{"name":"test_harness.py","path":"examples/lambda-build/python/src/test_harness.py","sha":"73edf34edf789e6f57b0d8288cedb22ec9d37bc3"}]}]},{"name":"vars.tf","path":"examples/lambda-build/vars.tf","sha":"a3db97ba1c39d962d7c36064206535c618ce4083"}],"toggled":true},{"name":"lambda-dead-letter-queue","children":[{"name":"README.md","path":"examples/lambda-dead-letter-queue/README.md","sha":"9f32671666b2325749f658ac30ff8993ed989b82"},{"name":"main.tf","path":"examples/lambda-dead-letter-queue/main.tf","sha":"85d53b0de0be402b1a3c2489fcb904eeae802371"},{"name":"outputs.tf","path":"examples/lambda-dead-letter-queue/outputs.tf","sha":"c9f9f5be8eec0260e59b908a0c64d481522e7435"},{"name":"python","children":[{"name":"index.py","path":"examples/lambda-dead-letter-queue/python/index.py","sha":"fdb82c37d271ffb7c508ea1fb9a465cc6d0cc330"}]},{"name":"vars.tf","path":"examples/lambda-dead-letter-queue/vars.tf","sha":"942b373fd87b2e4f0cdae56b12ae6e678bd88133"}]},{"name":"lambda-edge","children":[{"name":"README.md","path":"examples/lambda-edge/README.md","sha":"3d5f9863348407a5a11d00240a5dc92e84a8e3df"},{"name":"main.tf","path":"examples/lambda-edge/main.tf","sha":"6602175eceec2395651dc18f16e257e2edaf5f5a"},{"name":"nodejs","children":[{"name":"index.js","path":"examples/lambda-edge/nodejs/index.js","sha":"927b0d194260ca2517369f584a0b49513433f8e9"}]},{"name":"outputs.tf","path":"examples/lambda-edge/outputs.tf","sha":"f26239ec90f6ceca5165be5d5b9f9fb26635120e"},{"name":"vars.tf","path":"examples/lambda-edge/vars.tf","sha":"696c7c319ca3c2d4b6ca10e79ca6ce8efbf57b51"}]},{"name":"lambda-keep-warm","children":[{"name":"README.md","path":"examples/lambda-keep-warm/README.md","sha":"d9828b74307b72a59f9c62c5578adc9a3dd7ae59"},{"name":"main.tf","path":"examples/lambda-keep-warm/main.tf","sha":"1a763b34dddc0b8872268f4de21f2ff43d075953"},{"name":"outputs.tf","path":"examples/lambda-keep-warm/outputs.tf","sha":"62a79ed810f3cde44b54d91505647e358752ff4a"},{"name":"src","children":[{"name":"index.js","path":"examples/lambda-keep-warm/src/index.js","sha":"a06e3ce1dddfe2692eeb5b2606873affc975bb76"}]},{"name":"vars.tf","path":"examples/lambda-keep-warm/vars.tf","sha":"1409b361ac77a8a5c64868de4dc06c716f2dcf66"}]},{"name":"lambda-s3-deployment-package","children":[{"name":"README.md","path":"examples/lambda-s3-deployment-package/README.md","sha":"a4adf84621831cdc686ad125a567957a8f8967c7"},{"name":"main.tf","path":"examples/lambda-s3-deployment-package/main.tf","sha":"4eff1ed9f5f4a9c380bd5ecf4b77485e0a0f3d15"},{"name":"outputs.tf","path":"examples/lambda-s3-deployment-package/outputs.tf","sha":"28fc5ab8cc109b761b4697e68067f4eefc6ee0dd"},{"name":"python","children":[{"name":"index.py","path":"examples/lambda-s3-deployment-package/python/index.py","sha":"eaa4418c2160ad4c4d3af22db1486792e6fee11f"},{"name":"test_harness.py","path":"examples/lambda-s3-deployment-package/python/test_harness.py","sha":"6c8f8c4c4a9ac1aedd12a5e0babae588565d22b4"}]},{"name":"vars.tf","path":"examples/lambda-s3-deployment-package/vars.tf","sha":"c21f2df459e0775158faa1dd9e86cb0666d688c4"}]},{"name":"lambda-s3","children":[{"name":"README.md","path":"examples/lambda-s3/README.md","sha":"037fbac52c44915b0fc66c47b0337441a64495ce"},{"name":"images","children":[{"name":"gruntwork-logo.png","path":"examples/lambda-s3/images/gruntwork-logo.png","sha":"a351cbef55bb12a9aa2652469416a388e9e276de"}]},{"name":"main.tf","path":"examples/lambda-s3/main.tf","sha":"9c050014482c43966f8e1369e4099ab71fb27001"},{"name":"outputs.tf","path":"examples/lambda-s3/outputs.tf","sha":"729892970d1ffc9122cef114133614945b9c7853"},{"name":"python","children":[{"name":"index.py","path":"examples/lambda-s3/python/index.py","sha":"9382fa2e2c3956f1c5ae1467de0f3942cad5bf34"},{"name":"test_harness.py","path":"examples/lambda-s3/python/test_harness.py","sha":"723f97fdacc7155bac0a3d9e12715a7bd6ba6c41"}]},{"name":"vars.tf","path":"examples/lambda-s3/vars.tf","sha":"abc518949b9d570263e8614834b0005aa58b40b9"}]},{"name":"lambda-service","children":[{"name":"app","children":[{"name":"app.js","path":"examples/lambda-service/app/app.js","sha":"faa4e1eef6ec837604c14a9629f6160a6e1c4b55"},{"name":"lambda.js","path":"examples/lambda-service/app/lambda.js","sha":"ee16375dac8d77ea09aed18a39bd6fdbd6aa5617"},{"name":"package-lock.json","path":"examples/lambda-service/app/package-lock.json","sha":"b03ed12db320f026e2fbae196732a7e0308f677c"},{"name":"package.json","path":"examples/lambda-service/app/package.json","sha":"0846df06aefae385cf8fa739e9f49e63fcff5bd9"}]},{"name":"edge","children":[{"name":"README.md","path":"examples/lambda-service/edge/README.md","sha":"c14b205d4d90fbdb6c82593d40fee332ffcde71e"},{"name":"main.tf","path":"examples/lambda-service/edge/main.tf","sha":"8ca5a5a43945cbbc5d3440f3f76f90d80ac08ba4"},{"name":"outputs.tf","path":"examples/lambda-service/edge/outputs.tf","sha":"4b25a3b03b793405c471061c9846f7e72be702b2"},{"name":"variables.tf","path":"examples/lambda-service/edge/variables.tf","sha":"56a26610f6fedfa2b6a7fef86024cf210e5a13cc"}]},{"name":"path-routing-with-catchall","children":[{"name":"README.md","path":"examples/lambda-service/path-routing-with-catchall/README.md","sha":"6dadf1f59d9114ce6f772204062921b3c7d02e72"},{"name":"main.tf","path":"examples/lambda-service/path-routing-with-catchall/main.tf","sha":"48ef0be10c027982d1e5dee7a3b129fd0729e141"},{"name":"outputs.tf","path":"examples/lambda-service/path-routing-with-catchall/outputs.tf","sha":"372fb177a6b33e440dfa397535f7c3a687b20e11"},{"name":"variables.tf","path":"examples/lambda-service/path-routing-with-catchall/variables.tf","sha":"3cb449136019e51d4d7671fd89384832d5e09e04"}]},{"name":"path-routing","children":[{"name":"README.md","path":"examples/lambda-service/path-routing/README.md","sha":"56dca58e89d6218992af22d948d022e8ce0d3639"},{"name":"main.tf","path":"examples/lambda-service/path-routing/main.tf","sha":"ad15519176a14ae9a15019797b77e9ce37fe5d6e"},{"name":"outputs.tf","path":"examples/lambda-service/path-routing/outputs.tf","sha":"372fb177a6b33e440dfa397535f7c3a687b20e11"},{"name":"variables.tf","path":"examples/lambda-service/path-routing/variables.tf","sha":"3cb449136019e51d4d7671fd89384832d5e09e04"}]},{"name":"regional","children":[{"name":"README.md","path":"examples/lambda-service/regional/README.md","sha":"8d7d29aef2a233f9b207b07ce6e75b0192a84cf9"},{"name":"main.tf","path":"examples/lambda-service/regional/main.tf","sha":"0444be8886bde7dd68181ba8f306f2ecc4f50cd0"},{"name":"outputs.tf","path":"examples/lambda-service/regional/outputs.tf","sha":"4b25a3b03b793405c471061c9846f7e72be702b2"},{"name":"variables.tf","path":"examples/lambda-service/regional/variables.tf","sha":"56a26610f6fedfa2b6a7fef86024cf210e5a13cc"}]}]},{"name":"lambda-vpc","children":[{"name":"README.md","path":"examples/lambda-vpc/README.md","sha":"99fb6c29716acffba545efea35d3dc4a32c2f022"},{"name":"javascript","children":[{"name":"index.js","path":"examples/lambda-vpc/javascript/index.js","sha":"1c4d37c47144c12acaa1306952c6e8512b201744"}]},{"name":"main.tf","path":"examples/lambda-vpc/main.tf","sha":"371557e767554ccd727bfa91a99ebff3fbd03d24"},{"name":"outputs.tf","path":"examples/lambda-vpc/outputs.tf","sha":"eb9cbc764168459df0fa63742281ef87b5919f2e"},{"name":"vars.tf","path":"examples/lambda-vpc/vars.tf","sha":"17ee714bfd63e99dc13f625e88cca231e4e47a78"}]},{"name":"scheduled-lambda-job","children":[{"name":"README.md","path":"examples/scheduled-lambda-job/README.md","sha":"d757b26ae306a94fc854fbbaaebedc1ae26d82b6"},{"name":"javascript","children":[{"name":"index.js","path":"examples/scheduled-lambda-job/javascript/index.js","sha":"92e0906a7d45397aedbda160a61f50321f7c23e4"}]},{"name":"main.tf","path":"examples/scheduled-lambda-job/main.tf","sha":"c497ae5f2bc6821bf04a2baf7cb1a8b1fc619a75"},{"name":"outputs.tf","path":"examples/scheduled-lambda-job/outputs.tf","sha":"eb9cbc764168459df0fa63742281ef87b5919f2e"},{"name":"vars.tf","path":"examples/scheduled-lambda-job/vars.tf","sha":"d3a08b377cc2cb908fb23af423b1700b19e3a684"}]}],"toggled":true},{"name":"modules","children":[{"name":"api-gateway-account-settings","children":[{"name":"README.md","path":"modules/api-gateway-account-settings/README.md","sha":"978178ec82a98604abf699c744c6907e285bcc9a"},{"name":"account-settings.png","path":"modules/api-gateway-account-settings/account-settings.png","sha":"f9e573d320f6d6f0ad0748e0d406589f38d54bd1"},{"name":"main.tf","path":"modules/api-gateway-account-settings/main.tf","sha":"fc89f04b5bdd849cac4209d171e90d94da0d3331"},{"name":"outputs.tf","path":"modules/api-gateway-account-settings/outputs.tf","sha":"ebfd7c838ab30a445df0481c4571ebde61747be0"},{"name":"variables.tf","path":"modules/api-gateway-account-settings/variables.tf","sha":"2276f0b5614c6161b5d2b95d979220c59edad0e3"}]},{"name":"api-gateway-proxy-methods","children":[{"name":"README.md","path":"modules/api-gateway-proxy-methods/README.md","sha":"e3ed8f93c1df0663f7e27118bc92bfeb4bd893a5"},{"name":"main.tf","path":"modules/api-gateway-proxy-methods/main.tf","sha":"80a016f8598790a77dd8b99f5e32c62e747a4b81"},{"name":"outputs.tf","path":"modules/api-gateway-proxy-methods/outputs.tf","sha":"c09e327abb5ba52aa563199f254ec54ee368c647"},{"name":"variables.tf","path":"modules/api-gateway-proxy-methods/variables.tf","sha":"831b1015342bdff3e4567b7b7d52c099981873b9"}]},{"name":"api-gateway-proxy","children":[{"name":"README.md","path":"modules/api-gateway-proxy/README.md","sha":"3e1ff7aa199b18d5c476561df69e67deda3e3462"},{"name":"core-concepts.md","path":"modules/api-gateway-proxy/core-concepts.md","sha":"4400e3a78f955b119662d9e809b30fde52967f0b"},{"name":"main.tf","path":"modules/api-gateway-proxy/main.tf","sha":"5452cdffdc50699df3a4e3cbe24057bc3a8c9de8"},{"name":"outputs.tf","path":"modules/api-gateway-proxy/outputs.tf","sha":"ceb7718363eaa90c50c910967e3a273689ea4a2b"},{"name":"variables.tf","path":"modules/api-gateway-proxy/variables.tf","sha":"537dbfffc1f88cdee612bbf0aa663a52a7c42d74"}]},{"name":"keep-warm","children":[{"name":"README.md","path":"modules/keep-warm/README.md","sha":"4509be5950804277b101d6ba22e0d7882df05b2c"},{"name":"main.tf","path":"modules/keep-warm/main.tf","sha":"f17bd082eace94a0060e2f8de8144e3262d25cb8"},{"name":"outputs.tf","path":"modules/keep-warm/outputs.tf","sha":"5fc4667c38e2694986e16b9808e9957ec0ad5d5b"},{"name":"src","children":[{"name":"index.js","path":"modules/keep-warm/src/index.js","sha":"16c923525b669ef04cfb039349ad6dd906720caa"}]},{"name":"vars.tf","path":"modules/keep-warm/vars.tf","sha":"af0fe8836a6bfd5f929a9534290c6e8f5943037a"}]},{"name":"lambda-edge","children":[{"name":"README.md","path":"modules/lambda-edge/README.md","sha":"d27fde0cb7e8dd52346a2e593c8e5a9470bec992"},{"name":"main.tf","path":"modules/lambda-edge/main.tf","sha":"d850e70fb3e983316ec14d911b07930c878b8dbd"},{"name":"outputs.tf","path":"modules/lambda-edge/outputs.tf","sha":"275b9eda581185081ec7b34ba330d9204c4789dd"},{"name":"vars.tf","path":"modules/lambda-edge/vars.tf","sha":"36240f5e192197b0db72d048e42e28392a998f3b"}]},{"name":"lambda","children":[{"name":"README.md","path":"modules/lambda/README.md","sha":"bdc61f16defdbd786418c105689f4dd01341a945"},{"name":"main.tf","path":"modules/lambda/main.tf","sha":"99e4d8c5e4111fe804e66b501f3708976a095b8d"},{"name":"outputs.tf","path":"modules/lambda/outputs.tf","sha":"d96731c19b62d217ea80a450ed8b74ee51d438de"},{"name":"vars.tf","path":"modules/lambda/vars.tf","sha":"6e91faf5e82ec8acecde5d5ffebb295df7e36f41"}]},{"name":"scheduled-lambda-job","children":[{"name":"README.md","path":"modules/scheduled-lambda-job/README.md","sha":"ea9c111f016d8e6546d8608dd7fc25433a1fc7d0"},{"name":"main.tf","path":"modules/scheduled-lambda-job/main.tf","sha":"06752cd8b2e0a9cecf61b9f79e0b4909c2b4be7e"},{"name":"outputs.tf","path":"modules/scheduled-lambda-job/outputs.tf","sha":"63274208aa97eceb03826f154973e010b1e8c539"},{"name":"vars.tf","path":"modules/scheduled-lambda-job/vars.tf","sha":"867285ceb2d1ce3c0352b7110ac7711b8fb0d7f6"}]}]},{"name":"owners.txt","path":"owners.txt","sha":"bc3c9b6136485d9fa12b39f55b2ebbdeaa03583f"},{"name":"terraform-cloud-enterprise-private-module-registry-placeholder.tf","path":"terraform-cloud-enterprise-private-module-registry-placeholder.tf","sha":"ae586c0fe830819580e1009d41a9074f16e65bed"},{"name":"test","children":[{"name":"README.md","path":"test/README.md","sha":"ef26d3851db2fff0b36dfa61379724c0db9ff281"},{"name":"api_gateway_account_settings_test.go","path":"test/api_gateway_account_settings_test.go","sha":"4cbc2475fcc73a526eb35d745f187e04e7056e85"},{"name":"go.mod","path":"test/go.mod","sha":"6a5ad33fdc495690994df6f77c9ded4fe1806535"},{"name":"go.sum","path":"test/go.sum","sha":"2520e272eefacd7af8ac9a7361dc4ec4cd120bab"},{"name":"gruntwork-logo.base64.txt","path":"test/gruntwork-logo.base64.txt","sha":"e94e107e3a8ea8c875a4489071137a38d279dc10"},{"name":"helpers_test.go","path":"test/helpers_test.go","sha":"3940f439d7312e75177f88b7f5ae7b457ffa66b4"},{"name":"lambda_build_test.go","path":"test/lambda_build_test.go","sha":"284ed155e95097334426ba6fb4f1fc669bb775c2"},{"name":"lambda_dead_letter_queue_test.go","path":"test/lambda_dead_letter_queue_test.go","sha":"3145c3ef1257c29cc9a460d69a95924fb1b2a441"},{"name":"lambda_edge_test.go","path":"test/lambda_edge_test.go","sha":"7696e715c0a98d17618231f3de543708c5feb38b"},{"name":"lambda_keep_warm_test.go","path":"test/lambda_keep_warm_test.go","sha":"045939ca6431bced95d37822e42a1294a0504590"},{"name":"lambda_s3_deployment_package_test.go","path":"test/lambda_s3_deployment_package_test.go","sha":"5a7f0de5d6f121aa3b2e33d6ad729e737da9f674"},{"name":"lambda_s3_reserved_test.go","path":"test/lambda_s3_reserved_test.go","sha":"62f81d1f78b6d10eb32e5bc2c6d5fedaf1803cf2"},{"name":"lambda_s3_test.go","path":"test/lambda_s3_test.go","sha":"212fc05ec22552e1b3b9ce74c216f8ad35dd828f"},{"name":"lambda_service_test.go","path":"test/lambda_service_test.go","sha":"153ab07ee89f9925eb531ca7b462d6427cbf60a0"},{"name":"lambda_vpc_test.go","path":"test/lambda_vpc_test.go","sha":"1070caac685fc019590a90b15480f9806c20f29d"},{"name":"scheduled_lambda_job_test.go","path":"test/scheduled_lambda_job_test.go","sha":"7a436697712dc8222c2850be10e1fe2e2e727df9"},{"name":"validation","children":[{"name":"validate_all_modules_and_examples_test.go","path":"test/validation/validate_all_modules_and_examples_test.go","sha":"918793390f1c56e1961fabfc6747054bd95089f7"}]}]}]},"detailsContent":"<h1 class=\"preview__body--title\" id=\"lambda-build-example\">Lambda Build example</h1><div class=\"preview__body--border\"></div><p>This folder shows an example of how to package code with dependencies and build steps and then deploy it as a Lambda\nfunction using the <code>lambda</code> module. The actual Python code in <code>index.py</code> is a simple example that uses an external\nlibrary called <a href=\"http://docs.python-requests.org/en/master/\" class=\"preview__body--description--blue\" target=\"_blank\">requests</a> to make HTTP requests and returns the results.</p>\n<p>With AWS Lambda, your <a href=\"http://docs.aws.amazon.com/lambda/latest/dg/deployment-package-v2.html\" class=\"preview__body--description--blue\" target=\"_blank\">deployment package</a>\n(zip file) must contain ALL of the dependencies for your app already bundled within it. Moreover, since Lambda\nfunctions run on Amazon Linux, all of those dependencies must be compiled specifically for Amazon Linux. This example\ncreates the deployment package as follows:</p>\n<ol>\n<li>\n<p>Build the code using <a href=\"https://www.docker.com/\" class=\"preview__body--description--blue\" target=\"_blank\">Docker</a>, using an <a href=\"http://docs.aws.amazon.com/AmazonECR/latest/userguide/amazon_linux_container_image.html\" class=\"preview__body--description--blue\" target=\"_blank\">Amazon Linux\nimage</a> as the base image,\nas shown in the example <a href=\"/repos/v0.14.3/package-lambda/examples/lambda-build/python/Dockerfile\" class=\"preview__body--description--blue\">Dockerfile</a>. This Docker image installs all of your dependencies and\nsource code into the <code>/usr/src/lambda</code> folder.</p>\n</li>\n<li>\n<p>When developing and testing locally, you can run your Lambda code directly in the Docker image. You can\n<a href=\"https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-host-directory-as-a-data-volume\" class=\"preview__body--description--blue\" target=\"_blank\">mount</a> the\n<code>python/src</code> directory from your host OS into <code>/usr/src/lambda/src</code> so that your local changes are visible\nimmediately in the container.</p>\n</li>\n<li>\n<p>To deploy to AWS, you use <code>docker cp</code> to copy the <code>/usr/src/lambda</code> folder to a local path (see the <a href=\"/repos/v0.14.3/package-lambda/examples/lambda-build/python/build.sh\" class=\"preview__body--description--blue\">build.sh\nscript</a>)and then run <code>terraform apply</code> to zip up that local path and deploy it to AWS.</p>\n</li>\n</ol>\n<h2 class=\"preview__body--subtitle\" id=\"how-do-you-deploy-this-example\">How do you deploy this example?</h2>\n<p>First, build the deployment package:</p>\n<ol>\n<li>Install <a href=\"https://www.docker.com/\" class=\"preview__body--description--blue\" target=\"_blank\">Docker</a>.</li>\n<li><code>./python/build.sh</code></li>\n</ol>\n<p>Next, deploy the code with Terraform:</p>\n<ol>\n<li>Install <a href=\"https://www.terraform.io/\" class=\"preview__body--description--blue\" target=\"_blank\">Terraform</a>.</li>\n<li>Open <code>vars.tf</code>, set the environment variables specified at the top of the file, and fill in any other variables that\ndon't have a default.</li>\n<li>Run <code>terraform get</code>.</li>\n<li>Run <code>terraform plan</code>.</li>\n<li>If the plan looks good, run <code>terraform apply</code>.</li>\n</ol>\n<h2 class=\"preview__body--subtitle\" id=\"how-do-you-test-the-lambda-function\">How do you test the Lambda function?</h2>\n<p>There are two ways to test the Lambda function once it's deployed:</p>\n<ol>\n<li><a href=\"#test-in-aws\" class=\"preview__body--description--blue\">Test in AWS</a></li>\n<li><a href=\"#test-locally\" class=\"preview__body--description--blue\">Test locally</a></li>\n</ol>\n<h3 class=\"preview__body--subtitle\" id=\"test-in-aws\">Test in AWS</h3>\n<p>Open up the <a href=\"https://console.aws.amazon.com/lambda/home\" class=\"preview__body--description--blue\" target=\"_blank\">AWS Console UI</a>, find the function, click the "Test" button,\nand enter test data that looks something like this:</p>\n<pre>{\n <span class=\"hljs-attr\">\"url\"</span>: <span class=\"hljs-string\">\"http://www.example.com\"</span>\n}\n</pre>\n<p>Click "Save and test" and AWS will show you the log output and returned value in the browser.</p>\n<h3 class=\"preview__body--subtitle\" id=\"test-locally\">Test locally</h3>\n<p>The code you write for a Lambda function is just regular code with a well-defined entrypoint (the "handler"), so you\ncan also run it locally by calling that entrypoint. The example Python app includes a <code>test_harness.py</code> file that is\nconfigured to allow you to run your code locally. This test harness script is configured as the <code>ENTRYPOINT</code> for the\nDocker container, so you can test locally as follows:</p>\n<pre>cd python\ndocker build -t <span class=\"hljs-built_in\">lambda</span>-build-<span class=\"hljs-built_in\">example</span> .\ndocker run -it --rm <span class=\"hljs-built_in\">lambda</span>-build-<span class=\"hljs-built_in\">example</span> http://www.<span class=\"hljs-built_in\">example</span>.com\n</pre>\n<p>To avoid having to do a <code>docker build</code> every time, you can do all subsequent <code>docker run</code> calls with your local <code>src</code>\nfolder mounted as a volume so that the Docker container always sees your latest source code:</p>\n<pre>docker run -it --rm -v <span class=\"hljs-string\">src:</span><span class=\"hljs-regexp\">/usr/</span>src<span class=\"hljs-regexp\">/lambda/</span>src lambda-build-example <span class=\"hljs-string\">http:</span><span class=\"hljs-comment\">//www.example.com</span>\n</pre>\n","repoName":"package-lambda","repoRef":"v0.14.1","serviceDescriptor":{"serviceName":"AWS Lambda","serviceRepoName":"package-lambda","serviceRepoOrg":"gruntwork-io","cloudProviders":["aws"],"description":"Deploy Lambda functions with Terraform. Supports uploading deployment packages, configuring environment variables, and scheduled functions.","imageUrl":"lambda.png","licenseType":"subscriber","technologies":["Terraform","Bash"],"compliance":[],"tags":[""]},"serviceCategoryName":"Serverless","fileName":"README.md","filePath":"/examples/lambda-build","title":"Repo Browser: AWS Lambda","description":"Browse the repos in the Gruntwork Infrastructure as Code Library."}