diff --git a/example-projects/counter-demo/aws-lambda-localstack/start.py b/example-projects/counter-demo/aws-lambda-localstack/start.py index 47a188e..61cb570 100644 --- a/example-projects/counter-demo/aws-lambda-localstack/start.py +++ b/example-projects/counter-demo/aws-lambda-localstack/start.py @@ -27,9 +27,9 @@ functionName = 'cloudcomp-counter-lambda-demo' # see ARN for AWS Academy LabRole function here: # https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/roles/details/LabRole?section=permissions -# roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3' -# roleName = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access' -roleName = 'arn:aws:iam::488766701848:role/LabRole' +# roleArn = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3' +# roleArn = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access' +roleArn = 'arn:aws:iam::488766701848:role/LabRole' ################################################################################################ # @@ -104,7 +104,7 @@ with open('lambda-deployment-archive.zip', mode='rb') as file: response = lClient.create_function( FunctionName=functionName, Runtime='python3.9', - Role=roleName, + Role=roleArn, Code={ 'ZipFile': zipfileContent }, @@ -118,18 +118,9 @@ with open('lambda-deployment-archive.zip', mode='rb') as file: ) lambdaFunctionARN = response['FunctionArn'] -print("Lambda Function and S3 Bucket to store the counter are available. Sadly, AWS Academy labs do not allow\n" - "creating an API gateway to be able to access the Lambda function directly via HTTP from the browser, as\n" - "shown in https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo.\n" +print("Lambda Function and S3 Bucket to store the counter are available.\n" "\n" - "However you can now run invoke-function.py to view an increment the counter. You can also use \n" - "the test button in the Lambda AWS console. In this case you need to send the content\n" - "\n" - "{\n" - " \"input\": \"1\"\n" - "}\n" - "\n" - "to increment the counter by 1.\n" + "You can now run invoke-function.py to view an increment the counter.\n" "Try to understand how Lambda can be used to cut costs regarding cloud services and what its pros\n" "and cons are.\n") @@ -147,6 +138,6 @@ response = apiClient.create_api( ProtocolType='HTTP', Target=lambdaFunctionARN ) -apiArn=response +apiArn = response print("API Endpoint can be reached at: http://" + apiArn["ApiEndpoint"]) diff --git a/example-projects/counter-demo/aws-lambda/start.py b/example-projects/counter-demo/aws-lambda/start.py index fe998a8..0bef800 100644 --- a/example-projects/counter-demo/aws-lambda/start.py +++ b/example-projects/counter-demo/aws-lambda/start.py @@ -25,9 +25,9 @@ functionName = 'cloudcomp-counter-lambda-demo' # see ARN for AWS Academy LabRole function here: # https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/roles/details/LabRole?section=permissions -# roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3' -# roleName = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access' -roleName = 'arn:aws:iam::488766701848:role/LabRole' +# roleArn = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3' +# roleArn = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access' +roleArn = 'arn:aws:iam::488766701848:role/LabRole' ################################################################################################ # @@ -46,11 +46,20 @@ def cleanup_s3_bucket(s3_bucket): client = boto3.setup_default_session(region_name=region) +iamClient = boto3.client('iam') s3Client = boto3.client('s3') s3Resource = boto3.resource('s3') lClient = boto3.client('lambda') apiClient = boto3.client("apigatewayv2") +print("Getting AWS Academy LabRole ARN...") +print("------------------------------------") +response = iamClient.list_roles() +for role in response["Roles"]: + if role["RoleName"] == "LabRole": + roleArn = role["Arn"] + print(roleArn) + print("Deleting old function...") print("------------------------------------") try: @@ -93,7 +102,7 @@ with open('lambda-deployment-archive.zip', mode='rb') as file: response = lClient.create_function( FunctionName=functionName, Runtime='python3.9', - Role=roleName, + Role=roleArn, Code={ 'ZipFile': zipfileContent },