· 7 years ago · Jan 16, 2019, 04:16 PM
1#from local mint 19.
2$mkdir ak
3$cd ak
4$mkdir aws_functions
5$cd aws_functions
6$mkdir tsa_services
7$cd tsa_services
8$nano -c tsa_services.py
9def handler(event, context):
10return event['a'] + event['b']
11cntrl+o
12y+enter
13crtl+x
14$zip tsa_services.zip tsa_services.py
15$aws iam create-group --group-name lambda_group
16# {
17# "Group": {
18# "Path": "/",
19# "CreateDate": "2019-01-15T05:16:59Z",
20# "GroupId": "",
21# "Arn": "arn:aws:iam:::group/lambda_group",
22# "GroupName": "lambda_group"
23# }
24# }
25$aws iam create-user --user-name lambda_user
26#{
27# "User": {
28# "UserName": "lambda_user",
29# "Path": "/",
30# "CreateDate": "2019-01-15T05:18:27Z",
31# "UserId": "",
32# "Arn": "arn:aws:iam::"
33# }
34#}
35$aws iam add-user-to-group --user-name lambda_user --group-name lambda_group
36$aws iam create-access-key --user-name lambda_user
37#{
38# "AccessKey": {
39# "UserName": "lambda_user",
40# "Status": "Active",
41# "CreateDate": "2019-01-15T05:21:55Z",
42# "SecretAccessKey": "",
43# "AccessKeyId": ""
44# }
45#}
46$nano -c lambda_policy.json
47{
48"Version": "2012-10-17",
49"Statement": [{
50"Effect": "Allow",
51"Action": [
52"iam:*",
53"lambda:*"
54],
55"Resource": "*"
56}]
57}
58ctrl + o
59cntrl + x
60$aws iam put-user-policy --user-name lambda_user --policy-name lambda_all \
61--policy-document file://lambda_policy.json
62$aws configure --profile lambda_user
63>keyid
64>secretkey
65>default region: us-west-2
66>defaut output formatL:json
67$aws ec2 describe-regions --profile lambda_user
68[returns unauthorized; proving you have configured aws cli to be using lambda_user account]
69#basic lambda_lambda_role works; but for this project we will use lambda-api-rds-ec2-cl9-full
70$nano -c basic_lambda_role.json
71{
72"Version": "2012-10-17",
73"Statement": [{
74"Effect": "Allow",
75"Principal": { "AWS" : "*" },
76"Action": "sts:AssumeRole"
77}]
78}
79ctrl + o
80ctrl + x
81$aws iam create-role --role-name basic_lambda_role \
82--assume-role-policy-document file://basic_lambda_role.json
83$aws lambdacreate-function \
84--region us-west-2 \
85--function-name tsa_services \
86--zip-file fileb://tsa_services.zip \
87--role \
88--handler tsa_services.handler \
89--runtime python3.7 \
90--profile lambda_user
91#{
92# "TracingConfig": {
93# "Mode": "PassThrough"
94# },
95# "CodeSha256": "",
96# "FunctionName": "tsa_services",
97# "CodeSize": 235,
98# "RevisionId": "",
99# "MemorySize": 128,
100# "FunctionArn": "",
101# "Version": "$LATEST",
102# "Role": "",
103# "Timeout": 3,
104# "LastModified": "2019-01-15T05:32:15.005+0000",
105# "Handler": "tsa_services.handler",
106# "Runtime": "python3.7",
107# "Description": ""
108#}
109#went to console
110#made test event
111#{
112# "a": "get ",
113# "b": "some"
114#}
115#output is 'get some'
116#this establishs lambda from cli;
117#next i wanted to work with installing non-native dependencies
118#we need to use pymyql to connect to rds {i think, i used a different package for lcl connectio
119# but the documentation i have seen online uses this package}
120$mkdir package
121$cd package
122$pip install pymysql --target option
123#Collecting pymysql
124# Using cached https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl
125#Installing collected packages: pymysql
126#Successfully installed pymysql-0.9.3
127$zip -r9 ../tsa_services.zip .
128$cd ..
129$zip -g tsa_services.zip tsa_services.py
130$aws lambda-update-function-code \
131--function-name tsa_services
132--zip-file fileb://tesa_services.zip
133#{
134# "TracingConfig": {
135# "Mode": "PassThrough"
136# },
137# "CodeSha256": "",
138# "FunctionName": "tsa_services",
139# "CodeSize": 114315,
140# "RevisionId": "",
141# "MemorySize": 128,
142# "FunctionArn": "",
143# "Version": "$LATEST",
144# "Role": "",
145# "Timeout": 3,
146# "LastModified": "",
147# "Handler": "tsa_services.handler",
148# "Runtime": "python3.7",
149# "Description": ""
150#}
151#everything looks like it works from cli.
152#but when I got to test it
153## Response:
154## {
155## "errorMessage": "Unable to import module 'tsa_services'"
156## }
157## Request ID:
158## ""
159## Function Logs:
160## START RequestId: Version: $LATEST
161## END
162## REPORT