|  | 
|  | 1 | +# AWS Lambda Function Create Python example | 
|  | 2 | + | 
|  | 3 | +This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). | 
|  | 4 | + | 
|  | 5 | +Create an AWS Lambda function using the Client API (low-level) of Boto 3. | 
|  | 6 | + | 
|  | 7 | +## Requirements | 
|  | 8 | + | 
|  | 9 | +* You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. | 
|  | 10 | + | 
|  | 11 | +* The code was written for: | 
|  | 12 | +  | 
|  | 13 | + * Python 3 | 
|  | 14 | + * AWS SDK for Python (Boto3). | 
|  | 15 | + | 
|  | 16 | +* This example uses Client API (low-level) of Boto 3. | 
|  | 17 | + | 
|  | 18 | +* Install the AWS SDK for Python (Boto3). | 
|  | 19 | + | 
|  | 20 | + Install the latest Boto 3 release via pip: | 
|  | 21 | + | 
|  | 22 | + ```bash | 
|  | 23 | + pip install boto3 | 
|  | 24 | + ``` | 
|  | 25 | + | 
|  | 26 | +## Using the code | 
|  | 27 | + | 
|  | 28 | +* Configure your AWS access keys. | 
|  | 29 | + | 
|  | 30 | + **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. | 
|  | 31 | + | 
|  | 32 | + When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. | 
|  | 33 | + | 
|  | 34 | + Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: | 
|  | 35 | + | 
|  | 36 | + * The default credential profiles file | 
|  | 37 | +  | 
|  | 38 | + Set credentials in the AWS credentials profile file on your local system, located at: | 
|  | 39 | + | 
|  | 40 | + `~/.aws/credentials` on Linux, macOS, or Unix | 
|  | 41 | + | 
|  | 42 | + `C:\Users\USERNAME\.aws\credentials` on Windows | 
|  | 43 | + | 
|  | 44 | + This file should contain lines in the following format: | 
|  | 45 | + | 
|  | 46 | + ```bash | 
|  | 47 | + [default] | 
|  | 48 | + aws_access_key_id = <your_access_key_id> | 
|  | 49 | + aws_secret_access_key = <your_secret_access_key> | 
|  | 50 | + ``` | 
|  | 51 | + Substitute your own AWS credentials values for the values `<your_access_key_id>` and `<your_secret_access_key>`. | 
|  | 52 | + | 
|  | 53 | + * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` | 
|  | 54 | +  | 
|  | 55 | + Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. | 
|  | 56 | + | 
|  | 57 | + To set these variables on Linux, macOS, or Unix, use `export`: | 
|  | 58 | + | 
|  | 59 | + ```bash | 
|  | 60 | + export AWS_ACCESS_KEY_ID=<your_access_key_id> | 
|  | 61 | + export AWS_SECRET_ACCESS_KEY=<your_secret_access_key> | 
|  | 62 | + ``` | 
|  | 63 | + | 
|  | 64 | + To set these variables on Windows, use `set`: | 
|  | 65 | + | 
|  | 66 | + ```bash | 
|  | 67 | + set AWS_ACCESS_KEY_ID=<your_access_key_id> | 
|  | 68 | + set AWS_SECRET_ACCESS_KEY=<your_secret_access_key> | 
|  | 69 | + ``` | 
|  | 70 | + | 
|  | 71 | +* You need a `.py` file where the code of the Lambda function is located. | 
|  | 72 | + | 
|  | 73 | + You can use the code obtained from the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson). | 
|  | 74 | + | 
|  | 75 | + This application coverts the `.py` file in a `ZIP` file to deploy. | 
|  | 76 | + | 
|  | 77 | +* You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code. | 
|  | 78 | + | 
|  | 79 | +* You can change the values of the payload to the Lambda function in the code (payload): | 
|  | 80 | + | 
|  | 81 | +* You have to create an AWS role that has Lambda permissions. | 
|  | 82 | + | 
|  | 83 | +* Run the code. | 
|  | 84 | + | 
|  | 85 | + You must provide 4 parameters: | 
|  | 86 | +  | 
|  | 87 | + * `<FUNCTION_NAME>` = Lambda function name | 
|  | 88 | + * `<FUNCTION_FILE>` = The path to the `.py` file where the code of the Lambda function is located | 
|  | 89 | + * `<FUNCTION_ROLE>` = The role ARN that has Lambda permissions | 
|  | 90 | + * `<FUNCTION_HANDLER>` = The fully qualifed method name (Ex: lambda_function.lambda_handler) | 
|  | 91 | + | 
|  | 92 | + Run application: | 
|  | 93 | + | 
|  | 94 | + ```bash | 
|  | 95 | + python lambdacreate.py lambda-name lambda-file lambda-role lambda-handler | 
|  | 96 | + ``` | 
|  | 97 | + | 
|  | 98 | + You must use as a function role the ARN. | 
|  | 99 | + | 
|  | 100 | + Ex.: `arn:aws:iam::123456789012:role/service-role/lambda-basic-execution` | 
|  | 101 | + | 
|  | 102 | +* Test the application. | 
|  | 103 | + | 
|  | 104 | + You should see the response of the Lambda function. | 
|  | 105 | + | 
|  | 106 | + For example: | 
|  | 107 | + | 
|  | 108 | + * `Creating function ...` | 
|  | 109 | + * `Created function "FunctionName" with ARN: "arn:aws:lambda:eu-west-1:123456789012:function:FunctionName"` | 
|  | 110 | +
 | 
|  | 111 | + and the Lambda function created. | 
0 commit comments