initial version of counter lambda function
This commit is contained in:
parent
33ab50e291
commit
a22eb50b9d
41
example-projects/counter-demo/aws-lambda/invoke-function.py
Normal file
41
example-projects/counter-demo/aws-lambda/invoke-function.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import boto3
|
||||||
|
import json
|
||||||
|
|
||||||
|
################################################################################################
|
||||||
|
#
|
||||||
|
# Configuration Parameters
|
||||||
|
#
|
||||||
|
################################################################################################
|
||||||
|
|
||||||
|
region = 'eu-central-1'
|
||||||
|
functionName = 'cloudcomp-counter-lambda-demo'
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################################
|
||||||
|
#
|
||||||
|
# boto3 code
|
||||||
|
#
|
||||||
|
################################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
client = boto3.setup_default_session(region_name=region)
|
||||||
|
lClient = boto3.client('lambda')
|
||||||
|
|
||||||
|
|
||||||
|
print("Invoking function...")
|
||||||
|
print("------------------------------------")
|
||||||
|
try:
|
||||||
|
response = lClient.invoke(
|
||||||
|
FunctionName=functionName,
|
||||||
|
Payload='{ "input": "1" }'
|
||||||
|
)
|
||||||
|
except lClient.exceptions.ResourceNotFoundException:
|
||||||
|
print('Function not available. No need to delete it.')
|
||||||
|
|
||||||
|
streamingBody = response['Payload']
|
||||||
|
result = streamingBody.read()
|
||||||
|
|
||||||
|
print(json.dumps(response, indent=4, sort_keys=True, default=str))
|
||||||
|
|
||||||
|
print('Payload:\n' + str(result))
|
||||||
|
|
42
example-projects/counter-demo/aws-lambda/lambda_function.py
Normal file
42
example-projects/counter-demo/aws-lambda/lambda_function.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# import json
|
||||||
|
import base64
|
||||||
|
import boto3
|
||||||
|
|
||||||
|
|
||||||
|
def lambda_handler(event, context):
|
||||||
|
s3_client = boto3.client('s3')
|
||||||
|
response = s3_client.get_object(Bucket='vertsys-counter', Key='eu-central-1')
|
||||||
|
|
||||||
|
counter = int(response['Body'].read().decode('utf-8'))
|
||||||
|
|
||||||
|
debug = ""
|
||||||
|
incr = 0
|
||||||
|
if 'body' in event:
|
||||||
|
body = str(base64.b64decode(event['body']).decode("utf-8"))
|
||||||
|
if body.startswith('input'):
|
||||||
|
incr = int(body.rsplit('=')[1])
|
||||||
|
elif 'input' in event:
|
||||||
|
incr = int(event['input'])
|
||||||
|
|
||||||
|
if incr is not 0:
|
||||||
|
counter = counter + incr
|
||||||
|
response = s3_client.put_object(Bucket='vertsys-counter', Key='eu-central-1', Body=str(counter))
|
||||||
|
|
||||||
|
output = ('<html><head><title>TCPTimeCounter REST Service</title>\n'
|
||||||
|
'<meta http-equiv="refresh" content="5"/></head><body>\n'
|
||||||
|
'<h2>HS Fulda - TCPTimeCounter REST Service</h2>\n'
|
||||||
|
'<p><b>HTML-Output:</b> ' + str(counter) + '</p></body>\n'
|
||||||
|
'<form method=POST action="/default/cloudcomp-counter-demo">\n'
|
||||||
|
'<input type="hidden" name="input" value="1">\n'
|
||||||
|
'<input type="submit" value="Increment"></form>\n'
|
||||||
|
# '<hr><b>Lambda Event:</b><br>' + repr(event) + '\n'
|
||||||
|
# '<hr><b>Lambda Context:</b><br>' + repr(context) + '\n'
|
||||||
|
'</body></html>\n')
|
||||||
|
|
||||||
|
return {
|
||||||
|
'statusCode': 200,
|
||||||
|
'headers': {
|
||||||
|
'Content-Type': 'text/html',
|
||||||
|
},
|
||||||
|
'body': output
|
||||||
|
}
|
55
example-projects/counter-demo/aws-lambda/start.py
Normal file
55
example-projects/counter-demo/aws-lambda/start.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import boto3
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
################################################################################################
|
||||||
|
#
|
||||||
|
# Configuration Parameters
|
||||||
|
#
|
||||||
|
################################################################################################
|
||||||
|
|
||||||
|
region = 'eu-central-1'
|
||||||
|
functionName = 'cloudcomp-counter-lambda-demo'
|
||||||
|
roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################################
|
||||||
|
#
|
||||||
|
# boto3 code
|
||||||
|
#
|
||||||
|
################################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
client = boto3.setup_default_session(region_name=region)
|
||||||
|
lClient = boto3.client('lambda')
|
||||||
|
|
||||||
|
|
||||||
|
print("Deleting old function...")
|
||||||
|
print("------------------------------------")
|
||||||
|
try:
|
||||||
|
response = lClient.delete_function(
|
||||||
|
FunctionName=functionName,
|
||||||
|
)
|
||||||
|
except lClient.exceptions.ResourceNotFoundException:
|
||||||
|
print('Function not available. No need to delete it.')
|
||||||
|
|
||||||
|
|
||||||
|
print("creating new function...")
|
||||||
|
print("------------------------------------")
|
||||||
|
|
||||||
|
zf = zipfile.ZipFile('lambda-deployment-archive.zip', 'w', zipfile.ZIP_DEFLATED)
|
||||||
|
zf.write('lambda_function.py')
|
||||||
|
zf.close()
|
||||||
|
|
||||||
|
with open('lambda-deployment-archive.zip', mode='rb') as file:
|
||||||
|
zipfileContent = file.read()
|
||||||
|
|
||||||
|
response = lClient.create_function(
|
||||||
|
FunctionName=functionName,
|
||||||
|
Runtime='python3.8',
|
||||||
|
Role=roleName,
|
||||||
|
Code={
|
||||||
|
'ZipFile': zipfileContent
|
||||||
|
},
|
||||||
|
Handler='lambda_function.lambda_handler',
|
||||||
|
Publish=True,
|
||||||
|
)
|
33
example-projects/counter-demo/aws-lambda/stop.py
Normal file
33
example-projects/counter-demo/aws-lambda/stop.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import boto3
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
################################################################################################
|
||||||
|
#
|
||||||
|
# Configuration Parameters
|
||||||
|
#
|
||||||
|
################################################################################################
|
||||||
|
|
||||||
|
region = 'eu-central-1'
|
||||||
|
functionName = 'cloudcomp-counter-lambda-demo'
|
||||||
|
roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################################
|
||||||
|
#
|
||||||
|
# boto3 code
|
||||||
|
#
|
||||||
|
################################################################################################
|
||||||
|
|
||||||
|
|
||||||
|
client = boto3.setup_default_session(region_name=region)
|
||||||
|
lClient = boto3.client('lambda')
|
||||||
|
|
||||||
|
|
||||||
|
print("Deleting old function...")
|
||||||
|
print("------------------------------------")
|
||||||
|
try:
|
||||||
|
response = lClient.delete_function(
|
||||||
|
FunctionName=functionName,
|
||||||
|
)
|
||||||
|
except lClient.exceptions.ResourceNotFoundException:
|
||||||
|
print('Function not available. No need to delete it.')
|
Loading…
x
Reference in New Issue
Block a user