changed lambda counter demo to work with AWS educate accounts
This commit is contained in:
		@@ -5,7 +5,7 @@ import boto3
 | 
			
		||||
 | 
			
		||||
def lambda_handler(event, context):
 | 
			
		||||
    s3_client = boto3.client('s3')
 | 
			
		||||
    response = s3_client.get_object(Bucket='vertsys-counter', Key='eu-central-1')
 | 
			
		||||
    response = s3_client.get_object(Bucket='cloudcomp-counter', Key='us-east-1')
 | 
			
		||||
 | 
			
		||||
    counter = int(response['Body'].read().decode('utf-8'))
 | 
			
		||||
 | 
			
		||||
@@ -20,13 +20,13 @@ def lambda_handler(event, context):
 | 
			
		||||
 | 
			
		||||
    if incr is not 0:
 | 
			
		||||
        counter = counter + incr
 | 
			
		||||
        response = s3_client.put_object(Bucket='vertsys-counter', Key='eu-central-1', Body=str(counter))
 | 
			
		||||
        response = s3_client.put_object(Bucket='cloudcomp-counter', Key='us-east-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'
 | 
			
		||||
              '<form method=POST action="">\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'
 | 
			
		||||
 
 | 
			
		||||
@@ -7,10 +7,15 @@ import zipfile
 | 
			
		||||
#
 | 
			
		||||
################################################################################################
 | 
			
		||||
 | 
			
		||||
region = 'eu-central-1'
 | 
			
		||||
functionName = 'cloudcomp-counter-lambda-demo'
 | 
			
		||||
roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
 | 
			
		||||
# you need to create a bucket in S3, here in this demo it is called "cloudcomp-counter", but
 | 
			
		||||
# bucket names need to be world wide unique ;) The demo looks for a file that is named
 | 
			
		||||
# "us-east-1" (same as our default region) in the bucket and expects a number in it to increase
 | 
			
		||||
 | 
			
		||||
# region = 'eu-central-1'
 | 
			
		||||
region = 'us-east-1'
 | 
			
		||||
functionName = 'cloudcomp-counter-lambda-demo'
 | 
			
		||||
# roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
 | 
			
		||||
roleName = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access'
 | 
			
		||||
 | 
			
		||||
################################################################################################
 | 
			
		||||
#
 | 
			
		||||
@@ -21,6 +26,7 @@ roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-r
 | 
			
		||||
 | 
			
		||||
client = boto3.setup_default_session(region_name=region)
 | 
			
		||||
lClient = boto3.client('lambda')
 | 
			
		||||
apiClient = boto3.client("apigatewayv2")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
print("Deleting old function...")
 | 
			
		||||
@@ -40,6 +46,7 @@ zf = zipfile.ZipFile('lambda-deployment-archive.zip', 'w', zipfile.ZIP_DEFLATED)
 | 
			
		||||
zf.write('lambda_function.py')
 | 
			
		||||
zf.close()
 | 
			
		||||
 | 
			
		||||
lambdaFunctionARN = ""
 | 
			
		||||
with open('lambda-deployment-archive.zip', mode='rb') as file:
 | 
			
		||||
    zipfileContent = file.read()
 | 
			
		||||
 | 
			
		||||
@@ -53,3 +60,19 @@ with open('lambda-deployment-archive.zip', mode='rb') as file:
 | 
			
		||||
        Handler='lambda_function.lambda_handler',
 | 
			
		||||
        Publish=True,
 | 
			
		||||
    )
 | 
			
		||||
    lambdaFunctionARN = response['FunctionArn']
 | 
			
		||||
 | 
			
		||||
print("creating API gateway...")
 | 
			
		||||
print("------------------------------------")
 | 
			
		||||
 | 
			
		||||
#apiArn = ""
 | 
			
		||||
response = apiClient.create_api(
 | 
			
		||||
    Name=functionName + '-api',
 | 
			
		||||
    ProtocolType='HTTP',
 | 
			
		||||
    Target=lambdaFunctionARN
 | 
			
		||||
)
 | 
			
		||||
#apiArn=response['']
 | 
			
		||||
 | 
			
		||||
#response = lClient.create_event_source_mapping(
 | 
			
		||||
#    EventSourceArn=apiArn,
 | 
			
		||||
#)
 | 
			
		||||
@@ -7,10 +7,11 @@ import zipfile
 | 
			
		||||
#
 | 
			
		||||
################################################################################################
 | 
			
		||||
 | 
			
		||||
region = 'eu-central-1'
 | 
			
		||||
# region = 'eu-central-1'
 | 
			
		||||
region = 'us-east-1'
 | 
			
		||||
functionName = 'cloudcomp-counter-lambda-demo'
 | 
			
		||||
roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
 | 
			
		||||
 | 
			
		||||
# roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
 | 
			
		||||
roleName = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access'
 | 
			
		||||
 | 
			
		||||
################################################################################################
 | 
			
		||||
#
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user