Skip to content

Commit 0dc8979

Browse files
committed
First commit
1 parent 61c5578 commit 0dc8979

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

awslambdaupdate-capi/README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# AWS Lambda Function Update Python example
2+
3+
This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services).
4+
5+
Update 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+
* The AWS Lambda function to update must already exist.
72+
73+
You can use the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson).
74+
75+
* You need a `.py` file where the code of the Lambda function is located.
76+
77+
You can use the code obtained from the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson).
78+
79+
This application compresses the `.py` file in a `ZIP` file to deploy.
80+
81+
* You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code.
82+
83+
* You have to create an AWS role that has Lambda permissions.
84+
85+
* Run the code.
86+
87+
You must provide 4 parameters:
88+
89+
* `<FUNCTION_NAME>` = Lambda function name
90+
* `<FUNCTION_FILE>` = The path to the `.py` file where the code of the Lambda function is located
91+
* `<FUNCTION_ROLE>` = The role ARN that has Lambda permissions
92+
* `<FUNCTION_HANDLER>` = The fully qualifed method name (Ex: lambda_function.lambda_handler)
93+
94+
Run application:
95+
96+
```bash
97+
python lambdaupdate.py lambda-name lambda-file lambda-role lambda-handler
98+
```
99+
100+
You must use as a function role the ARN.
101+
102+
Ex.: `arn:aws:iam::123456789012:role/service-role/lambda-basic-execution`
103+
104+
* Test the application.
105+
106+
You should see the response of the Lambda function.
107+
108+
For example:
109+
110+
* `Updating function ...`
111+
* `Updated function "FunctionName"`
112+
113+
And the Lambda function updated.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# lamdda_function.py
2+
# It handles a simple AWS Lambda function that shows the content (text) of the call
3+
# to the lambda function and returns a message including this content.
4+
5+
def lambda_handler(event, context):
6+
return 'Hello ' + event
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# lambdaupdate.py
4+
# It is an example that handles Lambda functions on AWS.
5+
# It uses Client API (low-level) of Boto3.
6+
# Update a Lambda function.
7+
# You must provide 1 parameter:
8+
# FUNCTION_NAME = Lambda function name
9+
# FUNCTION_FILE = The path to the .py file 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: lambda_function.lambda_handler)
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 lambdaupdate.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('Updating function ...')
63+
64+
deployment_package = create_lambda_deployment_package(function_file)
65+
66+
# Update Lambda function configuration
67+
response = lambda_client.update_function_configuration(
68+
FunctionName=function_name,
69+
Description='Updated by the Lambda Python API',
70+
Runtime='python3.8',
71+
Role=function_role,
72+
Handler=function_handler)
73+
74+
print('Response (Conf.):')
75+
print(response)
76+
function_arn_conf = response['FunctionArn']
77+
78+
# Update Lambda function code
79+
response = lambda_client.update_function_code(
80+
FunctionName=function_name,
81+
ZipFile=deployment_package,
82+
Publish=True)
83+
84+
print('Response (Code):')
85+
print(response)
86+
function_arn_code = response['FunctionArn']
87+
88+
print('\nUpdated function "' + function_name + '"')
89+
print('The function ARN (Conf.) is ' + function_arn_conf)
90+
print('The function ARN (Code) is ' + function_arn_code)
91+
92+
except botocore.exceptions.ClientError as e:
93+
if e.response['Error']['Code'] == "AccessDeniedException":
94+
print("Error: Access Denied!!")
95+
elif e.response['Error']['Code'] == "ValidationException":
96+
print("Error: Name Not Valid!!")
97+
else:
98+
raise
99+
100+
return
101+
102+
103+
# This is the standard boilerplate that calls the main() function.
104+
if __name__ == '__main__':
105+
main()

0 commit comments

Comments
 (0)