Skip to content
Snippets Groups Projects
Commit a22eb50b authored by Sebastian Rieger's avatar Sebastian Rieger
Browse files

initial version of counter lambda function

parent 33ab50e2
No related branches found
No related tags found
No related merge requests found
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))
# 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
}
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,
)
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.')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment