Skip to content

Commit 8b5aac0

Browse files
committed
First commit
1 parent f337740 commit 8b5aac0

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

awslambdalist-capi/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# AWS Lambda Function List Python example
2+
3+
This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services).
4+
5+
List information of a 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 Python 3 and AWS SDK for Python (Boto3).
12+
13+
* This example uses Client API (low-level) of Boto 3.
14+
15+
* Install the AWS SDK for Python (Boto3).
16+
17+
Install the latest Boto 3 release via pip:
18+
19+
```bash
20+
pip install boto3
21+
```
22+
23+
## Using the code
24+
25+
* Configure your AWS access keys.
26+
27+
**Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access.
28+
29+
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.
30+
31+
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:
32+
33+
* The default credential profiles file
34+
35+
Set credentials in the AWS credentials profile file on your local system, located at:
36+
37+
`~/.aws/credentials` on Linux, macOS, or Unix
38+
39+
`C:\Users\USERNAME\.aws\credentials` on Windows
40+
41+
This file should contain lines in the following format:
42+
43+
```bash
44+
[default]
45+
aws_access_key_id = <your_access_key_id>
46+
aws_secret_access_key = <your_secret_access_key>
47+
```
48+
Substitute your own AWS credentials values for the values `<your_access_key_id>` and `<your_secret_access_key>`.
49+
50+
* Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
51+
52+
Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
53+
54+
To set these variables on Linux, macOS, or Unix, use `export`:
55+
56+
```bash
57+
export AWS_ACCESS_KEY_ID=<your_access_key_id>
58+
export AWS_SECRET_ACCESS_KEY=<your_secret_access_key>
59+
```
60+
61+
To set these variables on Windows, use `set`:
62+
63+
```bash
64+
set AWS_ACCESS_KEY_ID=<your_access_key_id>
65+
set AWS_SECRET_ACCESS_KEY=<your_secret_access_key>
66+
```
67+
68+
* You can create a Lambda function on AWS.
69+
70+
You can use the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson).
71+
72+
* You can select the bucket region changing the value of `REGION` variable in the code.
73+
74+
* Run the code.
75+
76+
You must provide 1 parameter:
77+
78+
* `<FUNCTION_NAME>` = Lambda function name
79+
80+
Run application:
81+
82+
```bash
83+
python lambdalist.py function-name
84+
```
85+
86+
You can use as Lambda function name 2 options:
87+
88+
* Function name
89+
90+
Ex.: `HelloJsonPython`
91+
92+
* ARN value
93+
94+
Ex.: `arn:aws:lambda:eu-west-1:123456789012:function:HelloJsonPython`
95+
96+
You can retrieve the ARN value by looking at the function in the AWS Console.
97+
98+
* Test the application.
99+
100+
You should see the response with the information of the Lambda function.

awslambdalist-capi/lambdalist.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# lambdalist.py
4+
# It is an example that handles Lambda functions on AWS.
5+
# It uses Client API (low-level) of Boto3.
6+
# List information of a Lambda function.
7+
# You must provide 1 parameter:
8+
# FUNCTION_NAME = Lambda function name
9+
10+
import sys
11+
import boto3
12+
import botocore
13+
14+
def main():
15+
16+
REGION = 'eu-west-1' # AWS region
17+
18+
# Make a list of command line arguments, omitting the [0] element
19+
# which is the script itself.
20+
args = sys.argv[1:]
21+
if len(args) < 1:
22+
print('Not enough parameters.\nProper Usage is: python lambdalist.py <FUNCTION_NAME>')
23+
sys.exit(1)
24+
25+
function_name = args[0]
26+
print('Lambda function name: ' + function_name)
27+
28+
# Create an S3 Client
29+
lambdaclient = boto3.client('lambda', region_name=REGION)
30+
31+
# Invoke lambda function
32+
try:
33+
print('Listing function ...')
34+
35+
config = lambdaclient.get_function_configuration(
36+
FunctionName=function_name
37+
)
38+
39+
print('Function name: ', config['FunctionName'])
40+
print(' - ARN: ', config['FunctionArn'])
41+
print(' - Runtime: ', config['Runtime'])
42+
print(' - Role: ', config['Role'])
43+
print(' - Handler: ', config['Handler'])
44+
print(' - Description: ', config['Description'])
45+
print(' - Timeout: ', config['Timeout'])
46+
print(' - MemorySize: ', config['MemorySize'])
47+
print(' - LastModified: ', config['LastModified'])
48+
print(' - Description: ', config['Description'])
49+
print(' - CodeSize: ', config['CodeSize'])
50+
print(' - Version: ', config['Version'])
51+
52+
except botocore.exceptions.ClientError as e:
53+
if e.response['Error']['Code'] == "ResourceNotFoundException":
54+
print("Error: Function Not Found!!")
55+
elif e.response['Error']['Code'] == "AccessDeniedException":
56+
print("Error: Access Denied!!")
57+
elif e.response['Error']['Code'] == "ValidationException":
58+
print("Error: Name Not Valid!!")
59+
else:
60+
raise
61+
62+
return
63+
64+
65+
# This is the standard boilerplate that calls the main() function.
66+
if __name__ == '__main__':
67+
main()

0 commit comments

Comments
 (0)