Skip to content

Commit 44dfd70

Browse files
committed
First commit
1 parent 5e5f58c commit 44dfd70

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

awslambdacreate-capi/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# lambdacreate.py
4+
# It is an example that handles Lambda functions on AWS.
5+
# It uses Client API (low-level) of Boto3.
6+
# Create a Lambda function.
7+
# You must provide 1 parameter:
8+
# FUNCTION_NAME = Lambda function name
9+
# FUNCTION_FILE = The path to the JAR or ZIP firl where the code of the Lambda function is located
10+
# FUNCTION_ROLE = The role ARN that has Lambda permissions
11+
# FUNCTION_HANDLER = The fully qualifed method name (Ex: example.Handler::handleRequest)
12+
13+
import sys
14+
import io
15+
import zipfile
16+
import boto3
17+
import botocore
18+
19+
def create_lambda_deployment_package(function_file_name):
20+
# Creates a Lambda deployment package in ZIP format in an in-memory buffer.
21+
# This buffer can be passed directly to AWS Lambda when creating the function.
22+
# Parameters:
23+
# function_file_name (string): The name of the file that contains the Lambda handler function.
24+
# Return:
25+
# The deployment package.
26+
27+
buffer = io.BytesIO()
28+
with zipfile.ZipFile(buffer, 'w') as zipped:
29+
zipped.write(function_file_name)
30+
buffer.seek(0)
31+
return buffer.read()
32+
33+
34+
def main():
35+
36+
REGION = 'eu-west-1' # AWS region
37+
38+
# Make a list of command line arguments, omitting the [0] element
39+
# which is the script itself.
40+
args = sys.argv[1:]
41+
if len(args) < 4:
42+
print('Not enough parameters.\n'\
43+
'Proper Usage is: python lambdainvoke.py '\
44+
'<FUNCTION_NAME> <FUNCTION_FILE<> <FUNCTION_ROLE> <FUNCTION_HANDLER>')
45+
sys.exit(1)
46+
47+
function_name = args[0]
48+
function_file = args[1]
49+
function_role = args[2]
50+
function_handler = args[3]
51+
52+
print('Lambda function name: ' + function_name)
53+
print('Lambda function file: ' + function_file)
54+
print('Lambda function role: ' + function_role)
55+
print('Lambda function handler: ' + function_handler)
56+
57+
# Create a Lambda Client
58+
lambda_client = boto3.client('lambda', region_name=REGION)
59+
60+
# Create Lambda function
61+
try:
62+
print('Creating function ...')
63+
64+
deployment_package = create_lambda_deployment_package(function_file)
65+
66+
response = lambda_client.create_function(
67+
FunctionName=function_name,
68+
Description='Created by the Lambda Python API',
69+
Runtime='python3.8',
70+
Role=function_role,
71+
Handler=function_handler,
72+
Code={'ZipFile': deployment_package},
73+
Publish=True)
74+
75+
print('Response:')
76+
print(response)
77+
function_arn = response['FunctionArn']
78+
print('\nCreated function "' + function_name + '" with ARN: "' + function_arn + '"')
79+
80+
except botocore.exceptions.ClientError as e:
81+
if e.response['Error']['Code'] == "AccessDeniedException":
82+
print("Error: Access Denied!!")
83+
elif e.response['Error']['Code'] == "ValidationException":
84+
print("Error: Name Not Valid!!")
85+
else:
86+
raise
87+
88+
return
89+
90+
# This is the standard boilerplate that calls the main() function.
91+
if __name__ == '__main__':
92+
main()

0 commit comments

Comments
 (0)