added explanation about the necessary role for the function and clarification of the use of credentials in localstack
This commit is contained in:
		@@ -1,3 +1,7 @@
 | 
				
			|||||||
[default]
 | 
					[default]
 | 
				
			||||||
aws_access_key_id=test
 | 
					aws_access_key_id=test
 | 
				
			||||||
aws_secret_access_key=test
 | 
					aws_secret_access_key=test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# not necessary at all... you can use any aws_access_key, aws_secret_key and session to talk to
 | 
				
			||||||
 | 
					# localstack as it is a mockup/emulation of AWS. So you can even reuse your AWS Academy credentials
 | 
				
			||||||
 | 
					# to also connect to localstack
 | 
				
			||||||
@@ -9,28 +9,35 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# main change from real AWS Academy access to local emulated localstack is to change the endpoint
 | 
				
			||||||
 | 
					# other than that the same tools (boto3, aws-cli, aws-cdk etc. can be used)
 | 
				
			||||||
endpoint_url = "http://localhost.localstack.cloud:4566"
 | 
					endpoint_url = "http://localhost.localstack.cloud:4566"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# you need to create a bucket in S3, here in this demo it is called "cloudcomp-counter", but
 | 
					# a bucket in S3 will be created to store the counter bucket names need to be world-wide unique ;)
 | 
				
			||||||
# bucket names need to be world wide unique ;) The demo looks for a file that is named
 | 
					# Hence we create a bucket name that contains your group number and the current year.
 | 
				
			||||||
# "us-east-1" (same as our default region) in the bucket and expects a number in it to increase
 | 
					# The counter will be stores as key (file) "us-east-1" in the bucket (same name as our default region)
 | 
				
			||||||
 | 
					# in the bucket and expects a number in it to increase
 | 
				
			||||||
groupNr = 22
 | 
					groupNr = 22
 | 
				
			||||||
currentYear = date.today().year
 | 
					currentYear = date.today().year
 | 
				
			||||||
 | 
					 | 
				
			||||||
globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
 | 
					globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# region = 'eu-central-1'
 | 
					# region = 'eu-central-1'
 | 
				
			||||||
region = 'us-east-1'
 | 
					region = 'us-east-1'
 | 
				
			||||||
functionName = 'cloudcomp-counter-lambda-demo'
 | 
					functionName = 'cloudcomp-counter-lambda-demo'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# The Lambda function will run using privileges of a role, that allows the function to access/create
 | 
				
			||||||
 | 
					# resources in AWS (in this case read/write to S3). In AWS Academy you need to use the role that
 | 
				
			||||||
 | 
					# use created for your student account in the lab (see lab readme).
 | 
				
			||||||
# 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
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
# 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'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# For localstack you can use any role Arn and every secret and access key. Hence you can also use
 | 
				
			||||||
 | 
					# existing AWS Academy credentials to connect to localstack
 | 
				
			||||||
 | 
					
 | 
				
			||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# boto3 code
 | 
					# boto3 code
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,13 +10,8 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
endpoint_url = "http://localhost.localstack.cloud:4566"
 | 
					endpoint_url = "http://localhost.localstack.cloud:4566"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 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
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
groupNr = 22
 | 
					groupNr = 22
 | 
				
			||||||
currentYear = date.today().year
 | 
					currentYear = date.today().year
 | 
				
			||||||
 | 
					 | 
				
			||||||
globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
 | 
					globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# region = 'eu-central-1'
 | 
					# region = 'eu-central-1'
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,10 +9,10 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# you need to create a bucket in S3, here in this demo it is called "cloudcomp-counter", but
 | 
					# a bucket in S3 will be created to store the counter bucket names need to be world-wide unique ;)
 | 
				
			||||||
# bucket names need to be world wide unique ;) The demo looks for a file that is named
 | 
					# Hence we create a bucket name that contains your group number and the current year.
 | 
				
			||||||
# "us-east-1" (same as our default region) in the bucket and expects a number in it to increase
 | 
					# The counter will be stores as key (file) "us-east-1" in the bucket (same name as our default region)
 | 
				
			||||||
 | 
					# in the bucket and expects a number in it to increase
 | 
				
			||||||
groupNr = 22
 | 
					groupNr = 22
 | 
				
			||||||
currentYear = date.today().year
 | 
					currentYear = date.today().year
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -22,13 +22,21 @@ globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-gr
 | 
				
			|||||||
region = 'us-east-1'
 | 
					region = 'us-east-1'
 | 
				
			||||||
functionName = 'cloudcomp-counter-lambda-demo'
 | 
					functionName = 'cloudcomp-counter-lambda-demo'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# The Lambda function will run using privileges of a role, that allows the function to access/create
 | 
				
			||||||
 | 
					# resources in AWS (in this case read/write to S3). In AWS Academy you need to use the role that
 | 
				
			||||||
 | 
					# use created for your student account in the lab (see lab readme).
 | 
				
			||||||
# 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
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
# 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'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# standard name for role in AWS Academy lab created by vocareum is "LabRole". See README of the
 | 
				
			||||||
 | 
					# lab. The following code will lookup the AWS Resource Name (ARN) (sort of the ID for this role)
 | 
				
			||||||
 | 
					# that has the following name:
 | 
				
			||||||
 | 
					roleName = "LabRole"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# boto3 code
 | 
					# boto3 code
 | 
				
			||||||
@@ -56,7 +64,7 @@ print("Getting AWS Academy LabRole ARN...")
 | 
				
			|||||||
print("------------------------------------")
 | 
					print("------------------------------------")
 | 
				
			||||||
response = iamClient.list_roles()
 | 
					response = iamClient.list_roles()
 | 
				
			||||||
for role in response["Roles"]:
 | 
					for role in response["Roles"]:
 | 
				
			||||||
    if role["RoleName"] == "LabRole":
 | 
					    if role["RoleName"] == roleName:
 | 
				
			||||||
        roleArn = role["Arn"]
 | 
					        roleArn = role["Arn"]
 | 
				
			||||||
        print(roleArn)
 | 
					        print(roleArn)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -136,18 +144,12 @@ print("Lambda Function and S3 Bucket to store the counter are available. Sadly,
 | 
				
			|||||||
# that would call our function, as in the provided demo:
 | 
					# that would call our function, as in the provided demo:
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo
 | 
					# https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
# print("creating API gateway...")
 | 
					# print("creating API gateway...")
 | 
				
			||||||
# print("------------------------------------")
 | 
					# print("------------------------------------")
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# #apiArn = ""
 | 
					 | 
				
			||||||
# response = apiClient.create_api(
 | 
					# response = apiClient.create_api(
 | 
				
			||||||
#     Name=functionName + '-api',
 | 
					#     Name=functionName + '-api',
 | 
				
			||||||
#     ProtocolType='HTTP',
 | 
					#     ProtocolType='HTTP',
 | 
				
			||||||
#     Target=lambdaFunctionARN
 | 
					#     Target=lambdaFunctionARN
 | 
				
			||||||
# )
 | 
					# )
 | 
				
			||||||
# #apiArn=response['']
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# #response = lClient.create_event_source_mapping(
 | 
					 | 
				
			||||||
# #    EventSourceArn=apiArn,
 | 
					 | 
				
			||||||
# #)
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,13 +8,8 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 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
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
groupNr = 22
 | 
					groupNr = 22
 | 
				
			||||||
currentYear = date.today().year
 | 
					currentYear = date.today().year
 | 
				
			||||||
 | 
					 | 
				
			||||||
globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
 | 
					globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# region = 'eu-central-1'
 | 
					# region = 'eu-central-1'
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user