AWS Lambda - Developer Guide

AWS Lambda

Developer Guide

AWS Lambda Developer Guide

AWS Lambda: Developer Guide

Copyright ? Amazon Web Services, Inc. and/or its affiliates. All rights reserved. Amazon's trademarks and trade dress may not be used in connection with any product or service that is not Amazon's, in any manner that is likely to cause confusion among customers, or in any manner that disparages or discredits Amazon. All other trademarks not owned by Amazon are the property of their respective owners, who may or may not be affiliated with, connected to, or sponsored by Amazon.

AWS Lambda Developer Guide

Table of Contents

What is AWS Lambda? ........................................................................................................................ 1 When should I use Lambda? ........................................................................................................ 1 Lambda features ........................................................................................................................ 2 Getting started with Lambda ....................................................................................................... 2 Related services ......................................................................................................................... 3 Accessing Lambda ...................................................................................................................... 3 Pricing for Lambda ..................................................................................................................... 4

Setting up ......................................................................................................................................... 5 AWS Account ............................................................................................................................. 5 AWS CLI .................................................................................................................................... 5 AWS SAM .................................................................................................................................. 5 AWS SAM CLI ............................................................................................................................ 6 Tools for container images .......................................................................................................... 6 Code authoring tools .................................................................................................................. 6

Getting started .................................................................................................................................. 8 Create a function ....................................................................................................................... 9 Create the function ............................................................................................................ 9 Invoke the Lambda function ................................................................................................ 9 Clean up ......................................................................................................................... 10 Create a function defined as a container image ............................................................................ 12 Prerequisites .................................................................................................................... 12 Create the container image ............................................................................................... 12 Upload the image to the Amazon ECR repository ................................................................. 13 Update the user permissions .............................................................................................. 14 Create a Lambda function defined as a container image ........................................................ 14 Invoke the Lambda function .............................................................................................. 14 Clean up ......................................................................................................................... 15

Lambda foundations ......................................................................................................................... 17 Concepts ................................................................................................................................. 18 Function .......................................................................................................................... 18 Trigger ............................................................................................................................ 18 Event .............................................................................................................................. 18 Execution environment ...................................................................................................... 19 Instruction set architecture ................................................................................................ 19 Deployment package ........................................................................................................ 19 Runtime .......................................................................................................................... 19 Layer .............................................................................................................................. 20 Extension ........................................................................................................................ 20 Concurrency ..................................................................................................................... 20 Qualifier .......................................................................................................................... 20 Destination ...................................................................................................................... 21 Features .................................................................................................................................. 22 Scaling ............................................................................................................................ 22 Concurrency controls ........................................................................................................ 23 Asynchronous invocation ................................................................................................... 25 Event source mappings ..................................................................................................... 26 Destinations ..................................................................................................................... 27 Function blueprints ........................................................................................................... 28 Testing and deployment tools ............................................................................................ 29 Application templates ....................................................................................................... 29 Programming model ................................................................................................................. 30 Architectures ............................................................................................................................ 31 Advantages of using arm64 architecture ............................................................................. 31 Function migration to arm64 architecture ........................................................................... 31

iii

AWS Lambda Developer Guide

Function code compatibility with arm64 architecture ............................................................ 31 Suggested migration steps ................................................................................................ 32 Configuring the instruction set architecture ......................................................................... 31 Function scaling ....................................................................................................................... 34 Deployment packages ............................................................................................................... 39 Container images ............................................................................................................. 39 .zip file archives ............................................................................................................... 39 Layers ............................................................................................................................. 40 Using other AWS services .................................................................................................. 40 Lambda console ....................................................................................................................... 42 Applications ..................................................................................................................... 42 Functions ........................................................................................................................ 42 Code signing .................................................................................................................... 42 Layers ............................................................................................................................. 42 Edit code using the console editor ...................................................................................... 42 Lambda CLI ............................................................................................................................. 49 Prerequisites .................................................................................................................... 49 Create the execution role .................................................................................................. 49 Create the function .......................................................................................................... 50 List the Lambda functions in your account .......................................................................... 53 Retrieve a Lambda function ............................................................................................... 53 Clean up ......................................................................................................................... 54 Permissions ..................................................................................................................................... 55 Execution role .......................................................................................................................... 56 Creating an execution role in the IAM console ...................................................................... 56 Grant least privilege access to your Lambda execution role .................................................... 57 Managing roles with the IAM API ....................................................................................... 57 AWS managed policies for Lambda features ........................................................................ 58 Resource-based policies ............................................................................................................. 60 Granting function access to AWS services ............................................................................ 61 Granting function access to other accounts .......................................................................... 62 Granting layer access to other accounts .............................................................................. 63 Cleaning up resource-based policies .................................................................................... 64 User policies ............................................................................................................................ 65 Function development ...................................................................................................... 65 Layer development and use ............................................................................................... 68 Cross-account roles ........................................................................................................... 69 Condition keys for VPC settings ........................................................................................ 69 Resources and conditions .......................................................................................................... 70 Function resource names ................................................................................................... 71 Function actions ............................................................................................................... 73 Event source mapping actions ............................................................................................ 75 Layer actions ................................................................................................................... 75 Permissions boundaries ............................................................................................................. 76 Configuring functions ....................................................................................................................... 78 Creating functions (.zip packages) .............................................................................................. 79 Creating a function (console) ............................................................................................. 79 Using the console code editor ............................................................................................ 80 Updating function code (console) ....................................................................................... 80 Change the runtime or runtime version (console) ................................................................. 80 Change the architecture (console) ....................................................................................... 81 Using the Lambda API ...................................................................................................... 82 AWS CloudFormation ........................................................................................................ 82 Creating layers ......................................................................................................................... 83 Creating layer content ...................................................................................................... 83 Compiling the .zip file archive for your layer ........................................................................ 83 Including library dependencies in a layer ............................................................................. 83

iv

AWS Lambda Developer Guide

Language-specific instructions ........................................................................................... 85 Creating a layer ................................................................................................................ 85 Deleting a layer version .................................................................................................... 86 Configuring layer permissions ............................................................................................ 87 Using AWS CloudFormation with layers ............................................................................... 87 Creating functions (container images) ......................................................................................... 89 Function version $LATEST ................................................................................................. 89 Container image deployment ............................................................................................. 90 Amazon ECR permissions ................................................................................................. 90 Override the container settings .......................................................................................... 90 Creating a function (console) ............................................................................................. 90 Updating the function code (console) ................................................................................. 91 Overriding the image parameters (console) .......................................................................... 92 Using the Lambda API ...................................................................................................... 92 AWS CloudFormation ........................................................................................................ 93 Configuring function options ..................................................................................................... 94 Function versions ............................................................................................................. 94 Using the function overview .............................................................................................. 94 Configuring functions (console) .......................................................................................... 94 Configuring functions (API) ................................................................................................ 82 Configuring function memory (console) ............................................................................... 96 Accepting function memory recommendations (console) ....................................................... 96 Configuring triggers (console) ............................................................................................ 97 Testing functions (console) ................................................................................................ 97 Environment variables ............................................................................................................... 98 Configuring environment variables ..................................................................................... 98 Configuring environment variables with the API ................................................................... 99 Example scenario for environment variables ........................................................................ 99 Retrieve environment variables ........................................................................................ 100 Defined runtime environment variables ............................................................................. 101 Securing environment variables ........................................................................................ 102 Sample code and templates ............................................................................................. 104 Versions ................................................................................................................................ 105 Creating function versions ............................................................................................... 105 Managing versions with the Lambda API ........................................................................... 105 Using versions ................................................................................................................ 106 Granting permissions ...................................................................................................... 106 Aliases ................................................................................................................................... 107 Creating a function alias (Console) .................................................................................... 107 Managing aliases with the Lambda API ............................................................................. 107 Using aliases .................................................................................................................. 108 Resource policies ............................................................................................................ 108 Alias routing configuration .............................................................................................. 108 Managing functions ........................................................................................................................ 111 Reserved concurrency .............................................................................................................. 112 Configuring reserved concurrency ..................................................................................... 112 Configuring concurrency with the Lambda API ................................................................... 114 Provisioned concurrency .......................................................................................................... 115 Configuring provisioned concurrency ................................................................................. 115 Optimizing latency with provisioned concurrency ............................................................... 117 Managing provisioned concurrency with Application Auto Scaling ......................................... 119 Network ................................................................................................................................ 123 Execution role and user permissions ................................................................................. 123 Configuring VPC access (console) ...................................................................................... 124 Configuring VPC access (API) ............................................................................................ 124 Using IAM condition keys for VPC settings ......................................................................... 125 Internet and service access for VPC-connected functions ..................................................... 128

v

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download