Browse Source

added explanation about the necessary role for the function and clarification of the use of credentials in localstack

master
Sebastian Rieger 2 years ago
parent
commit
20f012a42d
  1. 4
      example-projects/counter-demo/aws-lambda-localstack/aws-mockup-credentials/credentials
  2. 19
      example-projects/counter-demo/aws-lambda-localstack/start.py
  3. 5
      example-projects/counter-demo/aws-lambda-localstack/stop.py
  4. 28
      example-projects/counter-demo/aws-lambda/start.py
  5. 5
      example-projects/counter-demo/aws-lambda/stop.py

4
example-projects/counter-demo/aws-lambda-localstack/aws-mockup-credentials/credentials

@ -1,3 +1,7 @@
[default]
aws_access_key_id=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

19
example-projects/counter-demo/aws-lambda-localstack/start.py

@ -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"
# 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
# a bucket in S3 will be created to store the counter bucket names need to be world-wide unique ;)
# Hence we create a bucket name that contains your group number and the current year.
# 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
currentYear = date.today().year
globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
# region = 'eu-central-1'
region = 'us-east-1'
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:
# 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::919927306708:role/cloudcomp-s3-access'
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

5
example-projects/counter-demo/aws-lambda-localstack/stop.py

@ -10,13 +10,8 @@ from botocore.exceptions import ClientError
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
currentYear = date.today().year
globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
# region = 'eu-central-1'

28
example-projects/counter-demo/aws-lambda/start.py

@ -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
# 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
# a bucket in S3 will be created to store the counter bucket names need to be world-wide unique ;)
# Hence we create a bucket name that contains your group number and the current year.
# 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
currentYear = date.today().year
@ -22,13 +22,21 @@ globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-gr
region = 'us-east-1'
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:
# 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::919927306708:role/cloudcomp-s3-access'
# 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
@ -56,7 +64,7 @@ print("Getting AWS Academy LabRole ARN...")
print("------------------------------------")
response = iamClient.list_roles()
for role in response["Roles"]:
if role["RoleName"] == "LabRole":
if role["RoleName"] == roleName:
roleArn = role["Arn"]
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:
#
# https://348yxdily0.execute-api.eu-central-1.amazonaws.com/default/cloudcomp-counter-demo
#
# 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,
# #)

5
example-projects/counter-demo/aws-lambda/stop.py

@ -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
currentYear = date.today().year
globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
# region = 'eu-central-1'

Loading…
Cancel
Save