diff --git a/example-projects/counter-demo/aws-lambda/invoke-function.py b/example-projects/counter-demo/aws-lambda/invoke-function.py new file mode 100644 index 0000000..67ba095 --- /dev/null +++ b/example-projects/counter-demo/aws-lambda/invoke-function.py @@ -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)) + diff --git a/example-projects/counter-demo/aws-lambda/lambda_function.py b/example-projects/counter-demo/aws-lambda/lambda_function.py new file mode 100644 index 0000000..254fbbd --- /dev/null +++ b/example-projects/counter-demo/aws-lambda/lambda_function.py @@ -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 = ('TCPTimeCounter REST Service\n' + '\n' + '

HS Fulda - TCPTimeCounter REST Service

\n' + '

HTML-Output: ' + str(counter) + '

\n' + '
\n' + '\n' + '
\n' + # '
Lambda Event:
' + repr(event) + '\n' + # '
Lambda Context:
' + repr(context) + '\n' + '\n') + + return { + 'statusCode': 200, + 'headers': { + 'Content-Type': 'text/html', + }, + 'body': output + } diff --git a/example-projects/counter-demo/aws-lambda/start.py b/example-projects/counter-demo/aws-lambda/start.py new file mode 100644 index 0000000..d440f4f --- /dev/null +++ b/example-projects/counter-demo/aws-lambda/start.py @@ -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, + ) diff --git a/example-projects/counter-demo/aws-lambda/stop.py b/example-projects/counter-demo/aws-lambda/stop.py new file mode 100644 index 0000000..2636e95 --- /dev/null +++ b/example-projects/counter-demo/aws-lambda/stop.py @@ -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.')