Skip to content

Commit 98dbc08

Browse files
committed
feat: upload the scripts
0 parents  commit 98dbc08

File tree

2,066 files changed

+225450
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,066 files changed

+225450
-0
lines changed

.gitignore

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Node artifact files
2+
node_modules/
3+
4+
# Compiled Java class files
5+
*.class
6+
7+
# Compiled Python bytecode
8+
*.py[cod]
9+
10+
# Log files
11+
*.log
12+
13+
# Package files
14+
*.jar
15+
16+
# Maven
17+
target/
18+
19+
# JetBrains IDE
20+
.idea/
21+
22+
# Unit test reports
23+
TEST*.xml
24+
25+
# Generated by MacOS
26+
.DS_Store
27+
28+
# Generated by Windows
29+
Thumbs.db
30+
31+
# Applications
32+
*.app
33+
*.exe
34+
*.war
35+
36+
# Large media files
37+
*.mp4
38+
*.tiff
39+
*.avi
40+
*.flv
41+
*.mov
42+
*.wmv
43+
44+
# Laravel
45+
/node_modules
46+
/public/build
47+
/public/hot
48+
/public/storage
49+
/storage/*.key
50+
/vendor
51+
.env
52+
.env.backup
53+
.env.production
54+
.phpunit.result.cache
55+
Homestead.json
56+
Homestead.yaml
57+
auth.json
58+
npm-debug.log
59+
yarn-error.log
60+
/.fleet
61+
/.idea
62+
/.vscode
63+
64+
# This Project
65+
/storage/debugbar
66+
/etc
67+
/public/uploads/avatar
68+
/public/uploads/banner
69+
/public/uploads/config
70+
/public/uploads/form
71+
/public/uploads/office
72+
/public/uploads/page
73+
/public/uploads/social_media
74+
/public/uploads/tmp

README.md

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Example python script hosted on AWS Lambda #
2+
3+
## What is this? ##
4+
5+
Python script running on AWS Lambda with functions:
6+
7+
- Connect to PostgreSQL database
8+
- Generate CSV files
9+
- Upload the CSV files to AWS S3
10+
11+
Developed in Dec 2024.
12+
13+
## Requirements ##
14+
15+
- AWS account
16+
- AWS Access Key ID (can be obtained at IAM > Users)
17+
- AWS Secret Access Key (can be obtained at IAM > Users)
18+
- Region (e.g. ap-southeast-1)
19+
- Role / ARN (e.g. arn:aws:iam::123456789012:role/lambda-role)
20+
- DB_HOST
21+
- DB_PORT
22+
- DB_NAME
23+
- DB_USER
24+
- DB_PASSWORD
25+
- S3_BUCKET
26+
- S3_KEY_PREFIX (e.g. reports)
27+
28+
## Installation ##
29+
30+
This project utilizes `awscli` to configure AWS, manage function code and function configuration. So, before clone this project, make sure you have `awscli` installed on your machine.
31+
32+
```bash
33+
# Install the AWS CLI
34+
pip install awscli
35+
36+
# Verify the awscli installation
37+
aws --version
38+
```
39+
40+
Then configure the AWS account.
41+
42+
```bash
43+
aws configure
44+
```
45+
46+
Input your details and input `json` as default output format.
47+
48+
### 1) Clone this project ###
49+
50+
You may use this script by clone this project using git command or download as .zip file then extract it.
51+
52+
### 2) Understand the project structure ###
53+
54+
```text
55+
This Project
56+
57+
├── package/ # folder to be uploaded to AWS Lambda
58+
│ ├── boto3
59+
│ ├── lambda_function.py # python script that we created to run on Lambda
60+
│ ├── psycopg2
61+
│ ├── requirements.txt # contains dependencies - install it using the command
62+
│ │ # "pip install -r requirements.txt -t ."
63+
│ ├── ... (other dependencies)
64+
65+
├── event.json # used for testing running scripts
66+
└── README.md
67+
```
68+
69+
### 3) Prepare the ZIP file to be uploaded to AWS Lambda ###
70+
71+
After prepare the dependencies and the main script, compress it to .zip file
72+
73+
```bash
74+
# make sure you are in the "package" directory
75+
cd package
76+
77+
# zip all the files
78+
zip -r ../lambda_function.zip .
79+
```
80+
81+
### 4) Create a new function in Lambda ###
82+
83+
For creating a new function in Lambda, we must have `AWS Role/ARN` (for creating Role/ARN, please see "Create AWS New Role/ARN" section below)
84+
85+
```bash
86+
# make sure you are outside the "package" directory, you are in the root directory of this project
87+
cd ..
88+
89+
# create a new Lambda function
90+
aws lambda create-function \
91+
--function-name GenerateReports \
92+
--runtime python3.11 \
93+
--role arn:aws:iam::123456789012:role/lambda-role \
94+
--handler lambda_function.lambda_handler \
95+
--zip-file fileb://lambda_function.zip
96+
```
97+
98+
Note:
99+
100+
- `GenerateReports` is function name
101+
- `python3.11` is runtime Python version
102+
- `role` is your AWS role/ARN
103+
- `handler` is your function name
104+
- `zip-file` is your ZIP file
105+
106+
### 5) Set ENV in Lambda ###
107+
108+
```bash
109+
aws lambda update-function-configuration \
110+
--function-name GenerateReports \
111+
--environment "Variables={DB_HOST=localhost,DB_PORT=5432,DB_NAME=xxx_db,DB_USER=user,DB_PASSWORD=xxx,S3_BUCKET=bucket-name,S3_KEY_PREFIX=reports}"
112+
```
113+
114+
### 6) Update configuration in Lambda ###
115+
116+
```bash
117+
aws lambda update-function-configuration \
118+
--function-name GenerateReports \
119+
--timeout 900
120+
```
121+
122+
### 7) Running test ###
123+
124+
```bash
125+
aws lambda invoke \
126+
--function-name GenerateReports \
127+
--payload file://event.json \
128+
response.json
129+
```
130+
131+
It will generate `response.json`, then you can check the response details.
132+
133+
### 8) Update code (if needed) ###
134+
135+
```bash
136+
aws lambda update-function-code \
137+
--function-name GenerateReports \
138+
--zip-file fileb://lambda_function.zip
139+
```
140+
141+
## Dependecies ##
142+
143+
- psycopg2-binary
144+
- boto3
145+
146+
## *IMPORTANT NOTE ##
147+
148+
We need `psycopg2-binary` for connect to PostgreSQL, to install it - please use from [https://github.com/jkehler/awslambda-psycopg2](https://github.com/jkehler/awslambda-psycopg2). Clone it first, then copy the files based on your Python version (in this project we will use Python 3.11) then paste it into the “package” directory.
149+
150+
## Create AWS New Role/ARN ##
151+
152+
1. Login to AWS Management Console
153+
2. Go to IAM service
154+
3. Choose “Roles” on the sidebar menu, then click “Create role”
155+
4. Choose “AWS Service” as trusted entity
156+
5. Choose “Lambda” from service list, then click “Next”
157+
6. For Permissions, add ”AmazonS3FullAccess” so this role can access to S3, then click “Next”
158+
7. Input the role name, like “lambda-s3-role”
159+
8. Click “Create role”

event.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

package/bin/jp.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
2+
3+
import sys
4+
import json
5+
import argparse
6+
from pprint import pformat
7+
8+
import jmespath
9+
from jmespath import exceptions
10+
11+
12+
def main():
13+
parser = argparse.ArgumentParser()
14+
parser.add_argument('expression')
15+
parser.add_argument('-f', '--filename',
16+
help=('The filename containing the input data. '
17+
'If a filename is not given then data is '
18+
'read from stdin.'))
19+
parser.add_argument('--ast', action='store_true',
20+
help=('Pretty print the AST, do not search the data.'))
21+
args = parser.parse_args()
22+
expression = args.expression
23+
if args.ast:
24+
# Only print the AST
25+
expression = jmespath.compile(args.expression)
26+
sys.stdout.write(pformat(expression.parsed))
27+
sys.stdout.write('\n')
28+
return 0
29+
if args.filename:
30+
with open(args.filename, 'r') as f:
31+
data = json.load(f)
32+
else:
33+
data = sys.stdin.read()
34+
data = json.loads(data)
35+
try:
36+
sys.stdout.write(json.dumps(
37+
jmespath.search(expression, data), indent=4, ensure_ascii=False))
38+
sys.stdout.write('\n')
39+
except exceptions.ArityError as e:
40+
sys.stderr.write("invalid-arity: %s\n" % e)
41+
return 1
42+
except exceptions.JMESPathTypeError as e:
43+
sys.stderr.write("invalid-type: %s\n" % e)
44+
return 1
45+
except exceptions.UnknownFunctionError as e:
46+
sys.stderr.write("unknown-function: %s\n" % e)
47+
return 1
48+
except exceptions.ParseError as e:
49+
sys.stderr.write("syntax-error: %s\n" % e)
50+
return 1
51+
52+
53+
if __name__ == '__main__':
54+
sys.exit(main())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pip

0 commit comments

Comments
 (0)