enabled API gateway in AWS lambda as it is now available for AWS academy
This commit is contained in:
		@@ -28,6 +28,8 @@ functionName = 'cloudcomp-counter-lambda-demo'
 | 
				
			|||||||
# see ARN for AWS Academy LabRole function here:
 | 
					# 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
 | 
					# https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/roles/details/LabRole?section=permissions
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					# e.g.: (309000625112, 919927306708, 488766701848 would in your case be your AWS Account ID, see Lab Details)
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
# roleArn = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
 | 
					# 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::919927306708:role/cloudcomp-s3-access'
 | 
				
			||||||
# roleArn = 'arn:aws:iam::488766701848:role/LabRole'
 | 
					# roleArn = 'arn:aws:iam::488766701848:role/LabRole'
 | 
				
			||||||
@@ -68,6 +70,16 @@ for role in response["Roles"]:
 | 
				
			|||||||
        roleArn = role["Arn"]
 | 
					        roleArn = role["Arn"]
 | 
				
			||||||
        print(roleArn)
 | 
					        print(roleArn)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print("Searching for old API gateway...")
 | 
				
			||||||
 | 
					print("------------------------------------")
 | 
				
			||||||
 | 
					for api in apiClient.get_apis()["Items"]:
 | 
				
			||||||
 | 
					    if api["Name"] == functionName + '-api':
 | 
				
			||||||
 | 
					        print("Deleting old API gateway...")
 | 
				
			||||||
 | 
					        print("------------------------------------")
 | 
				
			||||||
 | 
					        response = apiClient.delete_api(
 | 
				
			||||||
 | 
					            ApiId=api["ApiId"],
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print("Deleting old function...")
 | 
					print("Deleting old function...")
 | 
				
			||||||
print("------------------------------------")
 | 
					print("------------------------------------")
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
@@ -84,8 +96,8 @@ try:
 | 
				
			|||||||
    currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
 | 
					    currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
 | 
				
			||||||
    cleanup_s3_bucket(currentBucket)
 | 
					    cleanup_s3_bucket(currentBucket)
 | 
				
			||||||
    currentBucket.delete()
 | 
					    currentBucket.delete()
 | 
				
			||||||
except ClientError as e:
 | 
					except s3Client.exceptions.NoSuchBucket:
 | 
				
			||||||
    print(e)
 | 
					    print('Bucket not available. No need to delete it.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print("creating S3 bucket (must be globally unique)...")
 | 
					print("creating S3 bucket (must be globally unique)...")
 | 
				
			||||||
print("------------------------------------")
 | 
					print("------------------------------------")
 | 
				
			||||||
@@ -124,11 +136,25 @@ with open('lambda-deployment-archive.zip', mode='rb') as file:
 | 
				
			|||||||
    )
 | 
					    )
 | 
				
			||||||
    lambdaFunctionARN = response['FunctionArn']
 | 
					    lambdaFunctionARN = response['FunctionArn']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print("Lambda Function and S3 Bucket to store the counter are available. Sadly, AWS Academy labs do not allow\n"
 | 
					# API gateway to get an HTTP endpoint that we can access directly in the browser,
 | 
				
			||||||
      "creating an API gateway to be able to access the Lambda function directly via HTTP from the browser, as\n"
 | 
					# which will call our function, as in the provided demo:
 | 
				
			||||||
      "shown in https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo.\n"
 | 
					# https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print("creating API gateway...")
 | 
				
			||||||
 | 
					print("------------------------------------")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					response = apiClient.create_api(
 | 
				
			||||||
 | 
					    Name=functionName + '-api',
 | 
				
			||||||
 | 
					    ProtocolType='HTTP',
 | 
				
			||||||
 | 
					    Target=lambdaFunctionARN,
 | 
				
			||||||
 | 
					    CredentialsArn=roleArn
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print("Lambda Function and S3 Bucket to store the counter are created.\n"
 | 
				
			||||||
      "\n"
 | 
					      "\n"
 | 
				
			||||||
      "However you can now run invoke-function.py to view an increment the counter. You can also use \n"
 | 
					      "You can access the API gateway and increment the counter using the created Lambda function\n"
 | 
				
			||||||
 | 
					      "at: " + response["ApiEndpoint"] + " \n"
 | 
				
			||||||
 | 
					      "You can also 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"
 | 
					      "the test button in the Lambda AWS console. In this case you need to send the content\n"
 | 
				
			||||||
      "\n"
 | 
					      "\n"
 | 
				
			||||||
      "{\n"
 | 
					      "{\n"
 | 
				
			||||||
@@ -137,19 +163,4 @@ print("Lambda Function and S3 Bucket to store the counter are available. Sadly,
 | 
				
			|||||||
      "\n"
 | 
					      "\n"
 | 
				
			||||||
      "to increment the counter by 1.\n"
 | 
					      "to increment the counter by 1.\n"
 | 
				
			||||||
      "Try to understand how Lambda can be used to cut costs regarding cloud services and what its pros\n"
 | 
					      "Try to understand how Lambda can be used to cut costs regarding cloud services and what its pros\n"
 | 
				
			||||||
      "and cons are.\n")
 | 
					      "and cons are.\n")
 | 
				
			||||||
 | 
					 | 
				
			||||||
# sadly, AWS Academy Labs don't allow API gateways
 | 
					 | 
				
			||||||
# API gateway would allow getting an HTTP endpoint that we could access directly in the browser,
 | 
					 | 
				
			||||||
# that would call our function, as in the provided demo:
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# print("creating API gateway...")
 | 
					 | 
				
			||||||
# print("------------------------------------")
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# response = apiClient.create_api(
 | 
					 | 
				
			||||||
#     Name=functionName + '-api',
 | 
					 | 
				
			||||||
#     ProtocolType='HTTP',
 | 
					 | 
				
			||||||
#     Target=lambdaFunctionARN
 | 
					 | 
				
			||||||
# )
 | 
					 | 
				
			||||||
@@ -36,8 +36,19 @@ client = boto3.setup_default_session(region_name=region)
 | 
				
			|||||||
s3Client = boto3.client('s3')
 | 
					s3Client = boto3.client('s3')
 | 
				
			||||||
s3Resource = boto3.resource('s3')
 | 
					s3Resource = boto3.resource('s3')
 | 
				
			||||||
lClient = boto3.client('lambda')
 | 
					lClient = boto3.client('lambda')
 | 
				
			||||||
 | 
					apiClient = boto3.client("apigatewayv2")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print("Searching for old API gateway...")
 | 
				
			||||||
 | 
					print("------------------------------------")
 | 
				
			||||||
 | 
					for api in apiClient.get_apis()["Items"]:
 | 
				
			||||||
 | 
					    if api["Name"] == functionName + '-api':
 | 
				
			||||||
 | 
					        print("Deleting old API gateway...")
 | 
				
			||||||
 | 
					        print("------------------------------------")
 | 
				
			||||||
 | 
					        response = apiClient.delete_api(
 | 
				
			||||||
 | 
					            ApiId=api["ApiId"],
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
print("Deleting old function...")
 | 
					print("Deleting old function...")
 | 
				
			||||||
print("------------------------------------")
 | 
					print("------------------------------------")
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
@@ -54,5 +65,5 @@ try:
 | 
				
			|||||||
    currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
 | 
					    currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
 | 
				
			||||||
    cleanup_s3_bucket(currentBucket)
 | 
					    cleanup_s3_bucket(currentBucket)
 | 
				
			||||||
    currentBucket.delete()
 | 
					    currentBucket.delete()
 | 
				
			||||||
except ClientError as e:
 | 
					except s3Client.exceptions.NoSuchBucket:
 | 
				
			||||||
    print(e)
 | 
					    print('Bucket not available. No need to delete it.')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user