Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cloud-computing-msc-ai-examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sebastian Rieger
cloud-computing-msc-ai-examples
Commits
376d287b
Commit
376d287b
authored
10 months ago
by
Sebastian Rieger
Browse files
Options
Downloads
Patches
Plain Diff
enabled API gateway in AWS lambda as it is now available for AWS academy
parent
259afec1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
example-projects/counter-demo/aws-lambda/start.py
+33
-22
33 additions, 22 deletions
example-projects/counter-demo/aws-lambda/start.py
example-projects/counter-demo/aws-lambda/stop.py
+13
-2
13 additions, 2 deletions
example-projects/counter-demo/aws-lambda/stop.py
with
46 additions
and
24 deletions
example-projects/counter-demo/aws-lambda/start.py
+
33
−
22
View file @
376d287b
...
...
@@ -28,6 +28,8 @@ 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
#
# 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::919927306708:role/cloudcomp-s3-access'
# roleArn = 'arn:aws:iam::488766701848:role/LabRole'
...
...
@@ -68,6 +70,16 @@ for role in response["Roles"]:
roleArn
=
role
[
"
Arn
"
]
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
(
"
------------------------------------
"
)
try
:
...
...
@@ -84,8 +96,8 @@ try:
currentBucket
=
s3Resource
.
Bucket
(
globallyUniqueS3GroupBucketName
)
cleanup_s3_bucket
(
currentBucket
)
currentBucket
.
delete
()
except
Client
Error
as
e
:
print
(
e
)
except
s3
Client
.
exceptions
.
NoSuchBucket
:
print
(
'
Bucket not available. No need to delete it.
'
)
print
(
"
creating S3 bucket (must be globally unique)...
"
)
print
(
"
------------------------------------
"
)
...
...
@@ -124,11 +136,25 @@ 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
"
# API gateway to get an HTTP endpoint that we can access directly in the browser,
# which will 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
,
CredentialsArn
=
roleArn
)
print
(
"
Lambda Function and S3 Bucket to store the counter are created.
\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
"
"
\n
"
"
{
\n
"
...
...
@@ -137,19 +163,4 @@ print("Lambda Function and S3 Bucket to store the counter are available. Sadly,
"
\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
"
"
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
# )
"
and cons are.
\n
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
example-projects/counter-demo/aws-lambda/stop.py
+
13
−
2
View file @
376d287b
...
...
@@ -36,8 +36,19 @@ client = boto3.setup_default_session(region_name=region)
s3Client
=
boto3
.
client
(
'
s3
'
)
s3Resource
=
boto3
.
resource
(
'
s3
'
)
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
(
"
------------------------------------
"
)
try
:
...
...
@@ -54,5 +65,5 @@ try:
currentBucket
=
s3Resource
.
Bucket
(
globallyUniqueS3GroupBucketName
)
cleanup_s3_bucket
(
currentBucket
)
currentBucket
.
delete
()
except
Client
Error
as
e
:
print
(
e
)
except
s3
Client
.
exceptions
.
NoSuchBucket
:
print
(
'
Bucket not available. No need to delete it.
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment